Search code examples
.netc#-4.0office-interop

How can I use Microsoft.Office.Interop.Word to create a Mailing Lable Word document?


Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oDocument = new Microsoft.Office.Interop.Word.Document();
oDocument = oWord.Documents.Add();
Microsoft.Office.Interop.Word.MailingLabel oLable = oWord.MailingLabel;

oDocument = oLable.CreateNewDocument();
oDocument.SaveAs(SaveFileDialog1.InitialDirectory.ToString() + SaveFileDialog1.FileName);
oWord.Quit();

I can get an empty Word Lable document from the code above, how can I input data into the document? Anybody can give me an example of doing this?

Thanks


Solution

  • This is how you write content in file.

            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document oDocument = new Microsoft.Office.Interop.Word.Document();
            oDocument = oWord.Documents.Add();
    
            BigInteger r = 1;
            for (int i = 1; i <= 64; i++)
            {
                r = r * i;             
            }
            Console.Write(r.ToString());
    
            oDocument.Content.SetRange(0, 0);
            oDocument.Content.Text = r.ToString();
    
            oDocument.SaveAs(@"d:\mydoc.docx");
            oWord.Quit();
    

    you can find more info here : http://www.c-sharpcorner.com/UploadFile/muralidharan.d/how-to-create-word-document-using-C-Sharp/