在我的代碼中,我啓動了2個單詞實例。一個我設置爲我的「模板」的實例,即尚未運行的郵件合併文檔。另一個實例用於空白文檔,我將使用該文檔將每個頁面從執行的郵件合併文檔複製/粘貼到文檔中,然後單獨保存該文檔。在顯示執行的Word文檔時隱藏模板Word文檔?
我使這兩個Word實例可見,然後打開我的「模板」和空白文檔,使用每個實例的個人保存。接下來,我打開MailMerge操作的數據源。
當我執行郵件合併操作,我留下了我的屏幕上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
{
}
}
謝謝。明天有空時我會試試這個。現在我結束了處理完成時關閉所有文檔,然後運行單個實例重新打開保存的完整郵件合併以供顯示。 –