none
How can I pass vb.net variables into Word 2013 VBA code RRS feed

  • Question

  • I have a vb.net solution in VS2013 and I want to pass vb.net variables into a Word global shared template and call the Word vba code  how can I do this.

    in my vb.code

    Dim wordDocument as WordDocument 
    (WordDocument has been defined as Public Class)
    
    ‘call the vba code
    wordDocument.Document.Application.Run(“MyVBACodeHere”)

    I thought it maybe something like this..

    wordDocument.Document.Application.Run(“MyVBACodeHere”, variable1, variable2, variable3)

    and in my Word template .vba code

    A word global shared template located in

    C:\Program Files (x86)\Microsoft Office\Office15\STARTUP\GlobaShared.dotm

    run the code using the variables...

    Public MyVBACodeHere
    
    ...use variables variable1, variable2, variable3 here
    

    how can I do this

    TIA




    • Edited by dd_ Friday, July 31, 2015 5:40 AM
    Friday, July 31, 2015 5:34 AM

Answers

  • Hi Tia,

    First, you need to declare the sub or procedure in the template which have parameters. For example, here is a simple sub that receive a string parameter in the 'Normal' template:

    Sub SayHello(name As String)
    Application.Selection.Range.Text = "Hello " + name
    End Sub

    It is similar as in VBA to call the sub in VB.Net. You can refer to code below to automat the Word in VB.Net to call the sub in the temlate.

    Note, to run this methoed successfully, the Word application must have the reference for the Word template which have the VBA sub.

     Imports Microsoft.Office.Interop.Word
    Public Class CallMacro
        Public Sub Main()
            Dim app As New Application
            app.Visible = True
            Dim doc As Document = app.Documents.Add()
            doc.Application.Run("SayHello", "jack")
        End Sub
    End Class

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, August 3, 2015 2:30 AM
    Moderator

All replies

  • Hi dd_

    Yes, your approach should work.

    You need to declare your VBA macro with arguments:

    Public Sub MyVBACodeHere(var1, var2)


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 31, 2015 3:38 PM
    Moderator
  • thank you Cindy

    so how would I call the macro from vb.net. im guessing Ill need to set & pass parameters from vb.net to vba

    Sunday, August 2, 2015 11:25 PM
  • Hi Tia,

    First, you need to declare the sub or procedure in the template which have parameters. For example, here is a simple sub that receive a string parameter in the 'Normal' template:

    Sub SayHello(name As String)
    Application.Selection.Range.Text = "Hello " + name
    End Sub

    It is similar as in VBA to call the sub in VB.Net. You can refer to code below to automat the Word in VB.Net to call the sub in the temlate.

    Note, to run this methoed successfully, the Word application must have the reference for the Word template which have the VBA sub.

     Imports Microsoft.Office.Interop.Word
    Public Class CallMacro
        Public Sub Main()
            Dim app As New Application
            app.Visible = True
            Dim doc As Document = app.Documents.Add()
            doc.Application.Run("SayHello", "jack")
        End Sub
    End Class

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, August 3, 2015 2:30 AM
    Moderator
  • thank you Fei

    --dd

    Monday, August 10, 2015 4:56 AM