Passing Variables From Workbook To A Word Macro

  • Hey all,


    I've got a macro in an excel sheet, which when invoked should -
    - pick up data from the sheet and create an email based on specific cell values
    - open a specific word document and run a macro in the word document that would add a few user-specified number of rows in a table and then print it out


    What i would also like to do is, before printing, paste variables passed from excel in the document


    Everything works perfectly except for the passing variables from the excel macro to the word macro part. I cant seem to figure out the syntax to do that. I always send up with Error 450 "Wrong number of arguments or invalid property assignment "
    (the word document prints, as long as i dont pass the variables)


    EXCEL MACRO SECTION -

    Code
    ...
        Set appWD = CreateObject("Word.Application")
        appWD.Visible = True
         
        appWD.Documents.Open Filename:="C:\Attendence Record.doc"
        appWD.Run "PrintTable", "var1", "var2", "var3"
    ...


    So, how do I pass these variables to the PrintTable macro in word?

  • Re: Passing Variables From Workbook To A Word Macro


    The syntax seems correct. Check the number and type of the arguments.
    HTH
    Daniel

    Regards.
    Daniel

  • Re: Passing Variables From Workbook To A Word Macro


    I've dim'ed the three variables as strings in the macro, and they take their values from different cells in the workbook.


    I assume the Microsoft Word 11.0 reference is adequate?


    Is there any other way of passing the variables?

  • Re: Passing Variables From Workbook To A Word Macro


    Pretty simple code (i'm not sure if i'm missing something here)


  • Re: Passing Variables From Workbook To A Word Macro


    You should have :

    Code
    Sub PrintTable(arg1 as String, arg2 as String, arg3 as String)
    Selection.MoveUp Unit:=wdLine,  Count:=3 
    Selection.TypeText Text:=arg1
    Selection.MoveDown Unit:=wdLine, Count:=1 
    Selection.TypeText Text:=arg2
    Selection.MoveDown Unit:=wdLine, Count:=1 
    Selection.TypeText Text:=arg3
     
    Application.PrintOut... 
    End Sub

    Regards.
    Daniel

  • Re: Passing Variables From Workbook To A Word Macro


    Ah, that worked perfectly! I should've realized that earlier. I'm such a fool.


    Thanks a lot Daniel!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!