Create a new Word document using VB.Net

 
This article describes how to create a new Word document using VB.Net.
follow these steps:
1. Start a new Windows application in vb.net language.
2. Add a reference to the Microsoft Word Object Library
3. Add a button to Form1 from Toolbox.
4. Double-click Button1, create a Button click event.
5. Write code on the click event of button:

 Private Sub CreateWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
 
        objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objDoc = objWord.Documents.Add
        'Insert some text in document
        Dim objPara As Word.Paragraph
        objPara = objDoc.Content.Paragraphs.Add
        objPara.Range.Text = "Welcome to Author Code"
        objPara.Range.Font.Bold = True
        objPara.Format.SpaceAfter = 30
        objPara.Range.InsertParagraphAfter()
 
    End Sub
  • Muhamad

    How to save the file ?

    • Use the saveas() method to save the file. like as :

      objdoc.SaveAs2(“c:document.docx”, WdSaveFormat.wdFormatDocument)

      thanks

      • Jalal Alatoom

        thanks
        but what about vs 2013 and office 2013
        this is not working

  • NOORULLA KHAN

    Man You’re just awesome nice Example ,well i am a new bean to vb net,so I’ve three fields in ms access database namely”Name,Rollno,Image(Long Binarydata),I want to create or Generate the word doc. in vb net ,so it’s really urgent man kindly assist me with your valuable code for my problem.Thank you so much for your time to read my Comment

    • Main Question is that where are you facing the problem..To insert image in the document or converting the binary data into image..

      Answer is simple firstly read the values of all three fields and then change the binary data into the Image and then paste that image object in to the selected document.range.

      Share your code so that i can help you..

      thanks

      • NOORULLA KHAN

        Hirendra Thank you so much for your agile reply.I have a Datagridview and all those three fields has been showed in the Datagridview , I have a button Called ” Export to Word”. So genuinely I don’t have any code to put In front of You ,I am so sorry dude.So any Idea Or suggestions Or Alternatives .
        And I am successfully Converting the Single Image into LongBinarydata and even Inserting into ms access database,And while validation also I am Converting binary data to Image and displaying in the Picturebox of a Windows form .
        but when matter comes to Convert all the binarydata images to .Jpg and showing it in the word doc. m loosing ma Confidence .
        So kindly please help me out .It would be great help ever in life.
        Thank you.

        • Don’t worry…I will help you..Give the some time to write the sample code. I will inform you when it will complete.

          One more question :

          Are you showing the image in the datagridview and you want to directly export all content of this datagridview (including the image) to the word document on the click of ” Export to Word”?

  • NOORULLA KHAN

    Hirendra Thanks a Lot for your agile reply,thank you so much ….
    Yes exactly what you said .I want to show the Image in the Datagridview then with all the Content I wanna export to MS Word.
    I’ve one more request bro if you have any other Alternatives to export content to the MS Word Including Image you can please help EXCEPT simply exporting to the Datagridview and then exporting to MS Word….once Again thank you So much..

  • jyoti

    that code was great…. thanks alot