实时版本的项目不打印word文档

时间:2013-08-05 18:10:25

标签: c# asp.net visual-studio-2010 ms-word office-interop

我创建了一个程序,它使用word文档并使用代码将它从visual studio打印到我的默认打印机:

//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 个答案:

答案 0 :(得分:0)

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