I have created a program that takes a word document and prints it from visual studio to my default printer using the code:
//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;
}
However, the function only prints the document when I have not published the project. The document fails to open and it sends the function into the else statement:
else
{
litError.Visible = true;
litError.Text = "File Does Not Exist";
return false;
}
The file string I was trying to use is:
fileName = AppDomain.CurrentDomain.BaseDirectory + @"LetterImages\SpringOrientationDomesticConfirmation2013.docx";
What would be the correct string to open the file?
Is there another function or method that needs to be added for a document to print properly from the web?
You need to have Office installed on the server that is running the code.