none
Field MacroButton - call a method in my C# AddIn RRS feed

  • Question

  • Hi;

    I'm 99% certain this is doable and 95% certain I even wrote code once to test this (that worked). But I can't find this anywhere on my system or via Google. So...

    Is there a way to set up a methonf in my AddIn (C# IExtensibility, not VSTO) so that a MacroButton field can call it?

    thanks - dave


    Very funny video - What's your Metaphor?
    Wednesday, May 4, 2011 1:34 PM

Answers

  • Hello Dave,

    Yes, you can add a macro to a document, but first you must allow programatical access to VBA projects in Word, the same in Excel.

    Here are a couple of code snippets I'm using for adding standard macro's to documents. In these snippets I add only small macros so the VBA code is generated programatically, otherwise I import code modules from the server by code.

    First, check if there is a macro with the same name already:

    ' Count existing macros
        nNumber = ActiveDocument.VBProject.VBComponents.Count
           
        For n = 1 To nNumber    ' Check if a macro with the same name exist in active doc/dot.
            If sModuleName = ActiveDocument.VBProject.VBComponents(n).Name Then
                bModuleExist = True
                With ActiveDocument.VBProject.VBComponents(sModuleName)
                    nLines = .CodeModule.CountOfLines
                    If nLines > 0 Then
                        If InStr(1, .CodeModule.Lines(1, nLines), sMacroName) Then
                            MsgBox "Det finns redan ett makro med namnet " & Chr(34) & _
                                sMacroName & " i modulen " & sModuleName & Chr(34) & Chr(13) & _
                                "Var god och använd ett annat namn.", vbInformation, "Sign On "
                                    '*********** macro exist already ******************************       
                            Exit Sub
                        End If
                    End If
                End With
            End If
        Next n

    The source code for the macro is generated and stored in the variable sVBLines

        Application.ShowVisualBasicEditor = True   '  ***************** You can skip this line *******************
        If Not bModuleExist Then
            Set oVBComp = ActiveDocument.VBProject.VBComponents.Add(vbext_ct_StdModule)
            oVBComp.Name = sModuleName
            oVBComp.CodeModule.InsertLines 1, sVBLines
            oVBComp.Activate
        Else
            Set oVBComp = ActiveDocument.VBProject.VBComponents(sModuleName)
            nLines = oVBComp.CodeModule.CountOfLines + 1
            If nLines = 0 Then nLines = 1
            oVBComp.CodeModule.InsertLines nLines, vbCrLf & sVBLines
        End If

    Have fun,
    Lars-Eric

    • Marked as answer by DavidThi808 Friday, May 6, 2011 2:34 PM
    Thursday, May 5, 2011 9:42 PM

All replies

  • Hi Dave

    Not directly,no....

    You'd need to set a reference to the DLL in the Word VBA project then have a Word macro linked to the MacroButton field that in turn calls the method in the DLL. The DLL would have to have an interface, be COMVisible, etc.


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 4, 2011 2:16 PM
    Moderator
  • Hi Cindy;

    Ok, that will still work. A couple of questions:

    1. Is there a way to have my AddIn add my VBA project to a document? I want to avoid the issue of users having to install a seperate VBA app on their system.
    2. Any suggestions about how to construct this, namespaces, etc so I don't step on any other VBA projects that might be in the template?

    I think all I need is a single proc (string url) that then calls my COMVisible Cproc (url).

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, May 5, 2011 8:35 PM
  • Hello Dave,

    Yes, you can add a macro to a document, but first you must allow programatical access to VBA projects in Word, the same in Excel.

    Here are a couple of code snippets I'm using for adding standard macro's to documents. In these snippets I add only small macros so the VBA code is generated programatically, otherwise I import code modules from the server by code.

    First, check if there is a macro with the same name already:

    ' Count existing macros
        nNumber = ActiveDocument.VBProject.VBComponents.Count
           
        For n = 1 To nNumber    ' Check if a macro with the same name exist in active doc/dot.
            If sModuleName = ActiveDocument.VBProject.VBComponents(n).Name Then
                bModuleExist = True
                With ActiveDocument.VBProject.VBComponents(sModuleName)
                    nLines = .CodeModule.CountOfLines
                    If nLines > 0 Then
                        If InStr(1, .CodeModule.Lines(1, nLines), sMacroName) Then
                            MsgBox "Det finns redan ett makro med namnet " & Chr(34) & _
                                sMacroName & " i modulen " & sModuleName & Chr(34) & Chr(13) & _
                                "Var god och använd ett annat namn.", vbInformation, "Sign On "
                                    '*********** macro exist already ******************************       
                            Exit Sub
                        End If
                    End If
                End With
            End If
        Next n

    The source code for the macro is generated and stored in the variable sVBLines

        Application.ShowVisualBasicEditor = True   '  ***************** You can skip this line *******************
        If Not bModuleExist Then
            Set oVBComp = ActiveDocument.VBProject.VBComponents.Add(vbext_ct_StdModule)
            oVBComp.Name = sModuleName
            oVBComp.CodeModule.InsertLines 1, sVBLines
            oVBComp.Activate
        Else
            Set oVBComp = ActiveDocument.VBProject.VBComponents(sModuleName)
            nLines = oVBComp.CodeModule.CountOfLines + 1
            If nLines = 0 Then nLines = 1
            oVBComp.CodeModule.InsertLines nLines, vbCrLf & sVBLines
        End If

    Have fun,
    Lars-Eric

    • Marked as answer by DavidThi808 Friday, May 6, 2011 2:34 PM
    Thursday, May 5, 2011 9:42 PM
  • Thank you - that is exactly what I am looking for.
    Very funny video - What's your Metaphor?
    Friday, May 6, 2011 2:35 PM