Loading
Ozgrid Excel Help & Best Practices Forums

Excel Video Tutorials / Excel Dashboards Reports



Results 1 to 3 of 3

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


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