Announcement

Collapse
No announcement yet.

Creating a Word Document with Excel VBA

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a Word Document with Excel VBA



    Hello,

    I am working on a project where I am creating analysis using a subroutine in excel/vba and I would like to have the subroutine generate a report that is a word document. I have never used excel/vba to create and modify a word document and don't know how to approach it. I imagine that I will have a template.doc that I can access and save as another file. Does anyone have an example of creating and modify a word document in this manner. Any help is greatly appreciated.

  • #2
    Re: Creating a Word Document with Excel VBA

    Hi

    Welcome to Ozgrid forum. This is not my code but it can be adapt to what you want.

    Code:
    Sub CreateNewWordDoc()
    ' to test this code, paste it into an Excel module
    ' add a reference to the Word-library
    ' create a new folder named C:\Foldername or edit the filnames in the code
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim i As Integer
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
        Set wrdDoc = wrdApp.Documents.Add
        ' or
        'Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
        ' sample word operations
        With wrdDoc
            For i = 1 To 100
                .Content.InsertAfter "Here is a sample test line #" & i
                .Content.InsertParagraphAfter
            Next i
            If Dir("C:\Foldername\MyNewWordDoc.doc") <> "" Then
                Kill "C:\Foldername\MyNewWordDoc.doc"
            End If
            .SaveAs ("C:\Foldername\MyNewWordDoc.doc")
            .Close ' close the document
        End With
        wrdApp.Quit ' close the Word application
        Set wrdDoc = Nothing
        Set wrdApp = Nothing
    End Sub

    Comment


    • #3
      Re: Creating a Word Document with Excel VBA

      Thanks jetted.

      This is working well for me. I am going to do some looking around for more formatting options. If I have any specific questions after I do some looking around I will post another question. Thanks for your help.

      Comment


      • #4
        Re: Creating a Word Document with Excel VBA

        I have a similar problem. I have an excel sheet with multiple columns. Would like to populate that table format in a word doc. Any pointers?

        Comment


        • #5
          Re: Creating a Word Document with Excel VBA

          Why not simply create the report on a sheet within the workbook?
          Hope that Helps

          Roy

          New users should read the Forum Rules before posting

          For free Excel tools & articles visit my web site

          If I have helped you and you feel like putting your hand in your pocket please make a donation to Children in Need

          RoyUK's Web Site

          royUK's Database Form

          Where to paste code from the Forum

          About me.

          Comment


          • #6
            Re: Creating a Word Document with Excel VBA

            The simplest way:

            Code:
            Sub M_snb()
                With CreateObject("Word.Document")
                    .Windows(1).Visible = True
                    .Content = "Example"
                End With
            End Sub

            Comment


            • #7
              Re: Creating a Word Document with Excel VBA

              Hi there!
              Sorry to ask this, but I am having trouble creating a word document named after the active cell and based on a specific template. After this document is created, the document should open.
              Does anyone have a code for that?
              Thank you very much!!

              Comment


              • #8


                Re: Creating a Word Document with Excel VBA

                hfalstom

                Welcome to the Forum. Please take the time to read our Forum Rules

                Please start your own post. Posting in another member's Thread is known as hijacking and is not allowed here. By all means add a link to a Thread that may be related to your question.
                Hope that Helps

                Roy

                New users should read the Forum Rules before posting

                For free Excel tools & articles visit my web site

                If I have helped you and you feel like putting your hand in your pocket please make a donation to Children in Need

                RoyUK's Web Site

                royUK's Database Form

                Where to paste code from the Forum

                About me.

                Comment

                Working...
                X