2013-08-05 46 views
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 != "&nbsp" && 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"; 

什么是正确的字符串来打开文件?

是否还需要添加另一个功能或方法才能让文档从网上正确打印?

+1

是的,MS Office需要安装在您运行它的计算机上。但我很确定你得到的错误会告诉你,对吗? –

+0

我看了一会儿,发现文件没有正确打开。我更新了问题 – Austin

+1

哦,我不知道你的文档位于何处。在错误消息中,用您尝试打开的文件名(包括AppDomain.CurrentDomain.BaseDirectory位)替换单词“文件”。还要确保你真的不会在'catch'块中结束。在那里输出一些诊断信息。 –

回答

0

您需要在运行代码的服务器上安装Office。

+0

我有参考文献中的Microsoft.Office.Interop.Word项目,这是你的意思还是我需要将Office实际安装到服务器上? – Austin