Loading
Ozgrid Excel Help & Best Practices Forums

Excel Video Tutorials / Excel Dashboards Reports



Results 1 to 8 of 8

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,289

    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


  7. #7
    Join Date
    20th September 2014
    Posts
    9

    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!!

    Excel Video Tutorials / Excel Dashboards Reports


  8. #8
    Join Date
    26th January 2003
    Location
    Derbyshire,UK
    Posts
    18,289

    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

    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.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 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