2016-05-30 11 views
0

Hey ich habe Probleme mit meinem Code, die nicht wirklich arbeiten möchten, hat mit Word-Interop funktioniert, aber wenn ich es in Excel ändern, bringt es mir eine Fehlermeldung in Private void CreateWordDocument (Excel .document Adoc und excelapp.document.open)Probleme mit finden und ersetzen Excel C#

private void FindAndReplace(Microsoft.Office.Interop.Excel.Application wordApp, object findText, object replaceWithText) 
    { 
     object matchCase = true; 
     object matchWholeWord = true; 
     object matchWildCards = false; 
     object matchSoundLike = false; 
     object nmatchAllForms = false; 
     object forward = true; 
     object format = false; 
     object matchKashida = false; 
     object matchDiactitics = false; 
     object matchAlefHamza = false; 
     object matchControl = false; 
     object read_only = false; 
     object visible = true; 
     object replace = 2; 
     object wrap = 1; 

     wordApp.Selection.Find.Execute(ref findText, 
        ref matchCase, ref matchWholeWord, 
        ref matchWildCards, ref matchSoundLike, 
        ref nmatchAllForms, ref forward, 
        ref wrap, ref format, ref replaceWithText, 
        ref replace, ref matchKashida, 
        ref matchDiactitics, ref matchAlefHamza, 
        ref matchControl); 
    } 



    string pathImage = null; 
    private void CreateWordDocument(object filename, object savaAs, object image) 
    { 
     List<int> processesbeforegen = getRunningProcesses(); 
     object missing = Missing.Value; 


     Excel.Application excelApp = new Excel.Application(); 

     Excel.Document aDoc = null; 

     if (File.Exists((string)filename)) 
     { 
      DateTime today = DateTime.Now; 

      object readOnly = false; //default 
      object isVisible = false; 

      excelApp.Visible = false; 

      aDoc = excelApp.Document.Open(ref filename, ref missing, ref readOnly, 
             ref missing, ref missing, ref missing, 
             ref missing, ref missing, ref missing, 
             ref missing, ref missing, ref missing, 
             ref missing, ref missing, ref missing, ref missing); 

      aDoc.Activate(); 

      //Find and replace: 
      this.FindAndReplace(excelApp, "$$firstname$$", txtFirstName.Text); 


     } 
     else 
     { 
      MessageBox.Show("file dose not exist."); 
      return; 
     } 

     //Save as: filename 
     aDoc.SaveAs2(ref savaAs, ref missing, ref missing, ref missing, 
       ref missing, ref missing, ref missing, 
       ref missing, ref missing, ref missing, 
       ref missing, ref missing, ref missing, 
       ref missing, ref missing, ref missing); 

     //Close Document: 
     //aDoc.Close(ref missing, ref missing, ref missing); 

     MessageBox.Show("File created."); 
     List<int> processesaftergen = getRunningProcesses(); 
     killProcesses(processesbeforegen, processesaftergen); 
    } 


    public List<int> getRunningProcesses() 
    { 
     List<int> ProcessIDs = new List<int>(); 
     //here we're going to get a list of all running processes on 
     //the computer 
     foreach (Process clsProcess in Process.GetProcesses()) 
     { 
      if (Process.GetCurrentProcess().Id == clsProcess.Id) 
       continue; 
      if (clsProcess.ProcessName.Contains("WINEXCEL")) 
      { 
       ProcessIDs.Add(clsProcess.Id); 
      } 
     } 
     return ProcessIDs; 
    } 


    private void killProcesses(List<int> processesbeforegen, List<int> processesaftergen) 
    { 
     foreach (int pidafter in processesaftergen) 
     { 
      bool processfound = false; 
      foreach (int pidbefore in processesbeforegen) 
      { 
       if (pidafter == pidbefore) 
       { 
        processfound = true; 
       } 
      } 

      if (processfound == false) 
      { 
       Process clsProcess = Process.GetProcessById(pidafter); 
       clsProcess.Kill(); 
      } 
     } 
    } 
    //Méthode Enabled Controles: 
    private void tEnabled(bool state) 
    { 
     txtFirstName.Enabled = state; 


    } 
    //Load the Template Document: 
    private void button1_Click(object sender, EventArgs e) 
    { 
     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      tFilename.Text = openFileDialog1.FileName; 
      tEnabled(true); 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      CreateWordDocument(tFilename.Text, saveFileDialog1.FileName, pathImage); 
      tEnabled(false); 
      //printDocument1.DocumentName = SaveDoc.FileName; 
     } 
    } 
} 

}

+1

Hey. Willkommen bei Stackoverflow. Bitte lesen Sie http://stackoverflow.com/help/how-to-ask und versuchen Sie, Ihre Frage so zu überarbeiten, dass wir genau wissen, was Sie erreichen wollen und wo Sie stecken geblieben sind. Um genauer zu sein: "Probleme" und "Fehlermeldungen" helfen nicht viel. Erzählen Sie zumindest, was die Ausnahmen sagten und wo sie genau erhoben wurden. – nozzleman

+0

Off-Topic, aber .NET 4+ macht 'ref missing' obsolet – MickyD

Antwort

0

Wort Interop und Excel Interop sind sehr unterschiedlich. Sie können "Word" nicht einfach in "Excel" ändern. Sie werden wahrscheinlich das Arbeitsblatt auswählen, einen Bereich von Zellen definieren, den Bereich durchlaufen müssen usw. Viele Snippets bei Google.

+0

Ahh danke für die Zeit Jungs Ich vermutete, dass ich so etwas tun müsste –

Verwandte Themen