Loading
Ozgrid Excel Help & Best Practices Forums

Excel Video Tutorials / Excel Dashboards Reports



Results 1 to 6 of 6

Thread: Creating a Word Document with Excel VBA

  1. #1
    Join Date
    17th May 2006
    Posts
    2

    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.

    Excel Video Tutorials / Excel Dashboards Reports


  2. #2
    Join Date
    16th June 2004
    Location
    Canada Quebec
    Posts
    260

    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.

    VB:
    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 
    
    

    Excel Video Tutorials / Excel Dashboards Reports


  3. #3
    Join Date
    17th May 2006
    Posts
    2

    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.

    Excel Video Tutorials / Excel Dashboards Reports


  4. #4
    Join Date
    3rd July 2014
    Posts
    1

    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?

    Excel Video Tutorials / Excel Dashboards Reports


  5. #5
    Join Date
    26th January 2003
    Location
    Derbyshire,UK
    Posts
    18,039

    Re: Creating a Word Document with Excel VBA

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

    Roy

    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.

  6. #6
    Join Date
    8th March 2010
    Posts
    1,651

    Re: Creating a Word Document with Excel VBA

    The simplest way:

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

    Excel Video Tutorials / Excel Dashboards Reports


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
porno