2016-05-30 60 views
0

嘿即時通訊與我的代碼,不真正想工作的問題是一直在與Word互操作,但是當我將其更改爲Excel它不斷返回給我一個錯誤消息私人無效CreateWordDocument(Excel .document ADOC和excelapp.document.open)查找和替換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

嘿。歡迎來到Stackoverflow。請閱讀http://stackoverflow.com/help/how-to-ask並嘗試以確保我們確切知道您要完成什麼以及您卡在哪裏的方式來徹底改變您的問題。更具體地說:「問題」和「錯誤消息」並沒有多大幫助。至少說出例外情況以及他們在哪裏得到了準確的提升。 – nozzleman

+0

題外話,但.NET 4+使'ref missing'過時 – MickyD

回答

0

的Word互操作性和Excel互操作有很大的不同。你不能把'Word'改成'Excel'。你可能將不得不選擇工作表,定義一系列單元格,遍歷範圍等等。谷歌上有很多片段。

+0

啊謝謝那些我懷疑我會這樣做的人 –