Below is the code for the first button event handler:
private Word.ApplicationClass WordApp = new Word.ApplicationClass();
private void button2_Click(object sender, EventArgs e)
{
//System.Diagnostics.Process.Start("e:\\conv");
// Use the open file dialog to choose a word document
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
// set the file name from the open file dialog
object fileName = openFileDialog1.FileName;
object readOnly = false;
object isVisible = true;
// Here is the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
// Make word visible, so you can see what's happening
WordApp.Visible = true;
// Open the document that was chosen by the dialog
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);
// Activate the document so it shows up in front
aDoc.Activate();
// Add the copyright text and a line break
WordApp.Selection.TypeText("Copyright C# Corner");
WordApp.Selection.TypeParagraph();
}
}
Below is the code for creating a document from scratch:
private void button2_Click(object sender, System.EventArgs e){// Use the custom dialog to get the title of the document from the userTitleQuery theQueryDialog = new TitleQuery();if (theQueryDialog.ShowDialog() == DialogResult.OK){// vba code generated from recorded macro to "remind me" how to do it.//**********************************************************//
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter// Selection.Font.Bold = wdToggle// Selection.TypeText Text:="Creating a Title"// Documents.Add Template:="C:\My Documents\CSharp Book Project\Normal.dot", // NewTemplate:=False, DocumentType:=0// **********************************************************// Set up all the parameters as generic objects so we can pass them in Documents.Addobject missing = System.Reflection.Missing.Value;object fileName = "normal.dot"; // template file nameobject newTemplate = false; object docType = 0;object isVisible = true;// Create a new Document, by calling the Add function in the Documents collectionWord.Document aDoc = WordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);// need to see the created document, so make it visibleWordApp.Visible = true;aDoc.Activate();// Global Constant enumerations are members of Word and can be assigned to Properties// Set alignment to the center of the documentWordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;// Toggle the title to a Bold FontWordApp.Selection.Font.Bold = (int)Word.WdConstants.wdToggle;// Type the Text of the Title that was inputted by the user in the Custom DialogWordApp.Selection.TypeText(theQueryDialog.Title);}}
No comments:
Post a Comment