Results 1 to 2 of 2

Thread: Controlling Word from VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112

    Question

    Anyone know how to open a document in Word(97) and then run a set of macros in it?

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Add the Microsoft Word 8.0 Object library (using References) to your VB project. Then;

    Code:
    'start a Word application object
    Dim objWord As New Word.Application
    Dim objDoc As Word.Document
    
    'if you want Word to be visible during operation use this line
    objWord.Visible = True
    
    'load the document
    Set objDoc = objWord.Documents.Open("[filename]")
    
    'run the Macro
    objWord.Run "Macro1"
    
    'then to close the document (without saving changes)
    'use objDoc.Save or .SaveAs to save the document.
    objDoc.Close False
    
    'and quit Word...
    objWord.Quit
    Set objWord = Nothing
    Make sure you have installed the "Help for Visual Basic" options for Office '97 - it's not installed by default. This will give you the comprehensive help file that you can use with the Word objects.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

Posting Permissions

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



Click Here to Expand Forum to Full Width