0

我创建了一个程序,它获取一个 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";

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

是否需要添加其他功能或方法才能从 Web 正确打印文档?

4

1 回答 1

0

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

于 2013-08-05T18:22:06.093 回答