Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling Macros (vb6)

    Hello all, I am using VB6 and Word 2002,

    I open a word document using code in my VB aplication, now I would like to call a macro that is part of the word document and pass it some variables from the VB application, how would I go about doing this. Lets suppose that the file is called test1.doc, the macro is called "testmacro" and the variables are "var1" and "var2". Thanks for the help.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calling Macros (vb6)

    Why don't you post the code you've tried and tell us what didn't work. Then someone can help you fix it.
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Macros (vb6)

    I don't have any code because I don't know how to write it!

    The only code I have is the shell32 function to open the word document and that is it!!

    In other words, HOW do I call the macro in the workbook from a VB application.

    thanks

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Macros (vb6)

    You don't use shell to invoke a Word macro.
    You need to create a Word object and execute VBA via the Word object.

    For a Word VBA book, see the list of Word VBA books at my URL below.

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calling Macros (vb6)

    Here's a sample project for you.
    1) Create a new VB app & place a command button on the form.
    2) Use the Projects | References menu to add the Microsoft Word Object Library. Save your project.
    3) Create a word document, Read Me.doc in the project directory.
    4) Record a macro, Macro1 in the Read Me document. Close & save the document.
    5) Return to VB, double-click the button and replace the code outline with this code:
    <pre>Option Explicit

    Private Sub Command1_Click()
    Dim wrdApp As New Word.Application
    wrdApp.Documents.Open App.Path & "Read Me.doc"
    wrdApp.Visible = True
    With wrdApp.Selection
    .MoveRight Unit:=wdWord, Count:=2
    .Font.Bold = True
    .TypeText Text:="ACTIONS TAKEN "
    .Font.Bold = False
    End With
    wrdApp.Run MacroName:="NewMacros.Macro1"
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Macros (vb6)

    Okay, that works good, now how do I do the same thing in excel, so far I have

    Dim excApp As New Excel.Application

    'this part open up the Excel document
    excApp.Workbooks.Open App.Path & "my file.xls"
    excApp.Visible = True

    excApp.Run MacroName:="Module1.Macro1"

    but how do I send that macro a variable from my VB program. Call the varaibakle strMy_var in your example please

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Macros (vb6)

    Take a look at the help for Application.Run in either Word or Excel.

  8. #8
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Calling Macros (vb6)

    excApp.Run MacroName:="Module1.Macro1", varg1:=strMy_var

    Does that help? --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  9. #9
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Calling Macros (vb6)

    yup. thanks, that is what I needed!!!!

Posting Permissions

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