I am trying print a scorecard for a game in a c# application. The approach that I am trying to follow is - Use a template word file and perform string replacements and additions where needed to add team names, player info and scores.
Here is the code snippet that I have used so far. But it doesn't seem to work perfectly.
object readOnly = false; //default
object isVisible = false;
object addToRecentFiles = false;
wordApp.Visible = false;
aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
ref addToRecentFiles, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
this.FindAndReplace(wordApp, "pname", tFirstname.Text);
this.FindAndReplace(wordApp, "teamname", tLastname.Text);
this.FindAndReplace(wordApp, "tel", tPhone.Text);
this.FindAndReplace(wordApp, "Company", tCompany.Text);
this.FindAndReplace(wordApp, "Date", DateTime.Now.ToShortDateString());
object copies = "1";
object pages = "1";
object range = Word.WdPrintOutRange.wdPrintCurrentPage;
object items = Word.WdPrintOutItem.wdPrintDocumentContent;
object pageType = Word.WdPrintOutPages.wdPrintAllPages;
object oTrue = true;
object oFalse = false;
Word.Document document = aDoc;
object nullobj = Missing.Value;
int dialogResult = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref nullobj);
wordApp.Visible = false;
if (dialogResult == 1)
{
document.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);
}
aDoc.Close(ref missing, ref missing, ref missing);
//File.Delete(tempPath);
MessageBox.Show("File created.");
List<int> processesaftergen = getRunningProcesses();
killProcesses(processesbeforegen, processesaftergen);
Some of the issues that I have faced -
- Document is opened in other process.
- The template itself gets modified.
- Print does not work perfectly.
I also tried experimenting with docx library. But as far as I know it does not provide print functionality.