2013-12-13 68 views
1

在我的代码中,我启动了2个单词实例。一个我设置为我的“模板”的实例,即尚未运行的邮件合并文档。另一个实例用于空白文档,我将使用该文档将每个页面从执行的邮件合并文档复制/粘贴到文档中,然后单独保存该文档。在显示执行的Word文档时隐藏模板Word文档?

我使这两个Word实例可见,然后打开我的“模板”和空白文档,使用每个实例的个人保存。接下来,我打开MailMerge操作的数据源。

当我执行邮件合并操作,我留下了我的屏幕上3个文件可见:

  1. 原来的“模板”邮件合并
  2. 我是使用针对单个文件的空白文档在复制/粘贴邮件合并部分时保存。
  3. 执行的邮件合并文档标题为“Form Letters1”。

我的代码处理一个while()循环,并复制“Form Letters1”的每个部分并将其粘贴到我的标题为“NewDocument.doc”的文档中。然后,我的foreach()循环会在生成“NewDocument.doc”的文件名并保存之前更新数据库跟踪表。

“Form Letters1”中的单个部分保存后,我选择新保存的文档中的所有内容,并将其清除以处理“Form Letters1”中的下一部分。

当“Form Letters1”的所有单独部分已被复制/粘贴并保存为他们自己的文档时,我隐藏了我的“NewDocument.doc”与oNewWord.Visible = false;

这里我的问题是,在屏幕上,我仍然同时显示邮件合并的“模板”文档,以及“表单邮件1”执行邮件合并文档。

有什么办法让我隐藏模板并保持“Form Letters1”可见?在我的消息框出现之前,我尝试设置oWord.Visible = false;,但是隐藏了“模板”和“表单信件1”(执行的邮件合并文档,我需要让用户查看和打印)。

public void MergeSplitAndReview() 
    { 
     try 
     { 
      //MergeDocLibrary mdl = new MergeDocLibrary(); 
      //mdl.mergeDocument(docSource, docLoc); 

      // Mail Merge Template 
      Word.Application oWord = new Word.Application(); 
      Word.Document oWrdDoc = new Word.Document(); 
      // New Document Instance 
      Word.Application oNewWord = new Word.Application(); 
      Word.Document oNewWrdDoc = new Word.Document(); 

      object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; 

      // Documents must be visible for code to Activate() 
      oWord.Visible = true; 
      oNewWord.Visible = true; 

      Object oTemplatePath = docLoc; 
      Object oMissing = System.Reflection.Missing.Value; 

      // Open Mail Merge Template 
      oWrdDoc = oWord.Documents.Open(oTemplatePath); 

      // Open New Document (Empty) 
      // Note: I tried programmatically starting a new word document instead of opening an exisitng "blank", 
      //  bu when the copy/paste operation occurred, formatting was way off. The blank document below was 
      //  generated by taking a copy of the FullMailMerge.doc, clearing it out, and saving it, thus providing 
      //  a kind of formatted "template". 
      string newDocument = projectDirectory + "\\NewDocument.doc"; 
      oNewWrdDoc = oNewWord.Documents.Open(newDocument); 

      // Open Mail Merge Datasource 
      oWrdDoc.MailMerge.OpenDataSource(docSource, oMissing, oMissing, oMissing, 
       oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 

      // Execute Mail Merge (Opens Completed Mail Merge Documents Titled "Letters1") 
      oWrdDoc.MailMerge.Execute(); 

      // Save the processed Mail Merge Document for Archiving 
      // oWord.ActiveDocument.SaveAs2(docTempDir + "FullMailMerge.doc"); 

      int docCnt = oWord.ActiveDocument.Sections.Count - 1; 
      int cnt = 0; 
      while (cnt != docCnt) 
      { 
       cnt++; 
       string newFilename = ""; 

       // Copy Desired Section from Mail Merge 
       oWord.ActiveDocument.Sections[cnt].Range.Copy(); 
       // Set focus to the New Word Doc instance 
       oNewWord.Activate(); 
       // Paste copied range to New Word Doc 
       oNewWord.ActiveDocument.Range(0, 0).Paste(); 

       foreach (ListViewItem lvI in lvData.Items) 
       { 
        if (lvI.Checked) // Get first checked lvI in lvData to use for generating filename 
        { 
         updateAddrChngHistory(lvI.SubItems[16].Text); 

         string fileSys = lvI.SubItems[12].Text.ToUpper(); 
         string memNo = lvI.SubItems[0].Text; 

         newFilename = fileSys + "%" + memNo + "%" + "" + "%" + "" + "%" + "CORRESPONDENCE%OUTGOING - ACKNOWLEDGEMENT%" + DateTime.Now.ToString("yyyy-MM-dd-hh.mm.ss.ffffff") + ".doc"; 

         lvI.Remove(); // Delete from listview the lvI used for newFilename 
         break;  // Break out of foreach loop 
        } 
       } 

       // Save New Word Doc 
       oNewWord.ActiveDocument.SaveAs2(docTempDir + newFilename); 
       // Clear New Word Doc 
       oNewWord.ActiveDocument.Content.Select(); 
       oNewWord.Selection.TypeBackspace(); 
      } 
      // Show only the Full Mail Merge Doc. Have user press OK when finished to close documents. 
      // Set 'False' in PROD, 'True' in DEV 
      // oWord.Visible = false; 
      // Hides my new word instance used to save each individual section of the full Mail Merge Doc 
      oNewWord.Visible = false; 
      MessageBox.Show(new Form() { TopMost = true }, "Click OK when finsihed."); 

      oNewWord.ActiveDocument.Close(doNotSaveChanges); // Close the Individual Record Document 
      oNewWord.Quit();         // Close Word Instance for Individual Record 
      oWord.ActiveDocument.Close(doNotSaveChanges); // Close the Full Mail Merge Document (Currently ALSO closes the Template document) 
      oWord.Quit(doNotSaveChanges);     // Close the Mail Merge Template 
      MessageBox.Show("Mail Merge Completed, Individual Documents Saved, Instances Closed."); 
     } 
     catch (Exception ex) 
     { 
      LogException(ex); 
      MessageBox.Show("Source:\t" + ex.Source + "\nMessage: \t" + ex.Message + "\nData:\t" + ex.Data); 
      // Close all Word processes 
      Process[] processes = Process.GetProcessesByName("winword"); 
      foreach (var process in processes) 
      { 
       process.Close(); 
      } 
     } 
     finally 
     { 

     } 
    } 

回答

1

你应该能够显示/隐藏使用以下VBA的e.g.the相当于单个文件,:

oWord.ActiveDocument.Windows(1).Visible = false 

(置换合适的文档对象,而不是的ActiveDocument)。

+0

谢谢。明天有空时我会试试这个。现在我结束了处理完成时关闭所有文档,然后运行单个实例重新打开保存的完整邮件合并以供显示。 –