2017-03-04 166 views
1

我正在使用C#和Word Interop來自動化郵件合併。我想用默認選項將每個合併文檔打印到默認打印機,但不希望每次都顯示打印選項對話框。我怎樣才能做到這一點?當我在客戶機上試用此代碼時,PrintOut方法被調用時會顯示一個打印選項對話框。這裏是我的代碼:如何在C#中打印Word文檔而不顯示打印對話框

Word.Application oWord = new Word.Application(); 
oWord.Visible = false; 
Word.Document oWordDoc = new Word.Document(); 
object oTemplatePath = Path.Combine(baseDir, fileName); 
oWordDoc = oWord.Documents.Add((ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); 
object copies = "1"; 
object pages = ""; 
object range = Word.WdPrintOutRange.wdPrintAllDocument; 
object items = Word.WdPrintOutItem.wdPrintDocumentContent; 
object pageType = Word.WdPrintOutPages.wdPrintAllPages; 
object oTrue = true; 
object oFalse = false; 
oWordDoc.PrintOut(ref oTrue, ref oFalse, ref range, ref oMissing, ref oMissing, ref oMissing, ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
+0

您是否嘗試過設置[oWord.ActivePrinter](https://msdn.microsoft.com/en-us/library/office/ff821995.aspx)? – stuartd

+0

@stuartd:我還沒有嘗試過,但這是需要在多臺客戶端計算機上運行的東西,而且我無法知道它們的打印機名稱。這個想法是我的功能使用默認打印機與所有默認選項。 –

回答

0

我使用這個,它適用於單詞文檔。

 Process p = new Process(); 
     p.StartInfo = new ProcessStartInfo(); 
     p.CreateNoWindow = true; 
     p.Verb = "print"; 
     p.FileName = file; 

     p.Start(); 
相關問題