在显示已执行的Word Doc时隐藏模板Word Doc?

时间:2013-12-13 16:16:08

标签: c# .net winforms interop mailmerge

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

我将两个Word实例都可见,然后打开我的“模板”和空白文档,以便使用每个实例进行单独保存。接下来,我打开MailMerge操作的数据源。

执行邮件合并操作后,我的屏幕上显示3个文档:

  1. 邮件合并的原始“模板”
  2. 复制/粘贴邮件合并部分时,我用于保存单个文件的空白文档。
  3. 执行的邮件合并文件“Form Letters1”。
  4. 我的代码处理一个while()循环并复制“Form Letters1”的每个部分并将其粘贴到我的文档“NewDocument.doc”中。我的foreach()循环然后在生成“NewDocument.doc”文件名并保存之前更新数据库跟踪表。

    一旦保存了“表格字母1”中的单个部分,我选择新保存文档中的所有内容并清除它以处理“表格字母1”中的下一部分。

    当“表格信件1”的所有单独部分都被复制/粘贴并保存为他们自己的文件时,我用oNewWord.Visible = false;隐藏了我的“NewDocument.doc”。

    我的问题是,在屏幕上,我仍然显示邮件合并的“模板”文档,以及“Form Letters1”执行的邮件合并文档。

    我有什么方法可以隐藏模板并保持“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 个答案:

答案 0 :(得分:1)

您应该能够使用例如以下VBA的等效内容来显示/隐藏单个文档:

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

(替换相应的文档对象而不是ActiveDocument)。