0
我創建了一個程序,它接受一個word文檔,並使用代碼打印它從Visual Studio到我的默認打印機Word文檔:項目的現場版本不打印
//Check to see if the file exists
if (File.Exists(fileName.ToString()))
{
object readOnly = false;
object isVisible = false;
//Setup Word.Application class
Word.Application wordApp = new Word.Application();
Word.Document aDoc = null;
//Set Word to invisible
wordApp.Visible = false;
//Open the word document
aDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
try
{
//Activate Document
aDoc.Activate();
//Find Place Holders and replace them with values
//Dear ___
if (Convert.ToInt32(GuestNumber) == 0)
this.FindAndReplace(wordApp, "<NameAddressed>", NameAddressed);
//Dear ____ and Family
else
this.FindAndReplace(wordApp, "<NameAddressed>", NameAddressed + " and Family");
//Which Session they are attending
this.FindAndReplace(wordApp, "<SessionInfo>", SessionInfo);
//How many people are coming with them
this.FindAndReplace(wordApp, "<NumberGuests>", GuestNumber);
//How much they owe
this.FindAndReplace(wordApp, "<Balance>", Balance);
//Mailing Information
//First Last
if (Convert.ToInt32(GuestNumber) == 0)
this.FindAndReplace(wordApp, "<FullName>", FullName);
//First Last and Family
else
this.FindAndReplace(wordApp, "<FullName>", FullName + " and Family");
//Number St/Rd/etc.
this.FindAndReplace(wordApp, "<Address1>", Address1);
if (Address2 != " " && Address2 != "" && Address2 != " ")
this.FindAndReplace(wordApp, "<Address1>", Address1 + "\n\r" + Address2);
else
this.FindAndReplace(wordApp, "<Address1>", Address1);
//City
this.FindAndReplace(wordApp, "<City>", City);
//State
this.FindAndReplace(wordApp, "<State>", State);
//Zip Code
this.FindAndReplace(wordApp, "<Zip>", Zip);
}
catch (Exception ex)
{
//with the warning below, the default is correct.
aDoc.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref NoSave, ref missing, ref missing);
ClientScript.RegisterStartupScript(this.GetType(), "error", "javascript:;alert('" + ex.Message + "')");
return false;
}
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;
//Prints out the new word document
aDoc.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing, ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue, ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
//Close the document - you have to do this
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
aDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
// Make sure all of the documents are gone from the queue
while (wordApp.BackgroundPrintingStatus > 0)
System.Threading.Thread.Sleep(250);
wordApp.Quit(ref NoSave, ref missing, ref missing);
}
else
{
litError.Visible = true;
litError.Text = "File Does Not Exist";
return false;
}
但是,函數只打印文檔當我還沒有發表該項目。 該文檔無法打開,並將其發送功能到else語句:
else
{
litError.Visible = true;
litError.Text = "File Does Not Exist";
return false;
}
我試圖使用的文件字符串是:
fileName = AppDomain.CurrentDomain.BaseDirectory + @"LetterImages\SpringOrientationDomesticConfirmation2013.docx";
什麼是正確的字符串來打開文件?
是否還需要添加另一個功能或方法才能讓文檔從網上正確打印?
是的,MS Office需要安裝在您運行它的計算機上。但我很確定你得到的錯誤會告訴你,對嗎? –
我看了一會兒,發現文件沒有正確打開。我更新了問題 – Austin
哦,我不知道你的文檔位於何處。在錯誤消息中,用您嘗試打開的文件名(包括AppDomain.CurrentDomain.BaseDirectory位)替換單詞「文件」。還要確保你真的不會在'catch'塊中結束。在那裏輸出一些診斷信息。 –