none
COMException error Unable to run the specified macro - passing variables from/to vb.net and vba RRS feed

  • Question

  • I wasnt sure whether to post this in the Visual Basic forum or Office dev forum.

    in VS2013 vbnet a VSTO Word addin is developed.

    Workflow...

    This vb.net code calls a macro to run vba code in a Word 2013 template. The code ...

     worddocument.Document.Application.Run(MacroName:=("GetNetDocsInfo", nddocid, ndVersion, ndFileName, ndAuthor, ndClient, ndMatterID, ndDescription, ndCabinet)


    vba code in Word 2013  template .dotm loaded  from C:\Program Files (x86)\Microsoft Office\Office15\STARTUP 


    Public Sub GetNetDocsInfo(nddocid As String, ndVersion As String, ndFileName As String, ndAuthor As String, ndClient As String, ndMatterID As Integer, ndDescription As String, ndCabinet As String)
    
    
        Dim ndo As Object
        Set ndo = CreateObject("ndOffice.EchoingDataService")
        Dim docInfo As Object
        Set docInfo = ndo.GetDocumentInfo(Application.ActiveDocument.FullName)
        
        ' If document originated from NetDocuments
        If Not docInfo Is Nothing Then
            
            '' standard fields
            ndCabinet = docInfo.cabinet
            nddocid = docInfo.Identifier   
            ndFileName = CleanFileName(docInfo.FileName)      
            ndVersion = CStr(docInfo.Version)
            
            Dim profileAttribute As Object
            
            For Each profileAttribute In docInfo.ProfileAttributes
            
           
                MsgBox profileAttribute.ID & "-" & profileAttribute.name & "-" & profileAttribute.value
            
            
                '' mandatry nd fields
                If LCase(profileAttribute.name) = "Author" Then
                    ndAuthor = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Document Date" Then
                    ndDateCreated = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "eDOCS No" Then
                     eDocsNo = profileAttribute.value
                End If
            
                If LCase(profileAttribute.name) = "eDOCS Parent Folder" Then
                     eDocsParentFolder = profileAttribute.value
                End If
                
                ' subject
                If LCase(profileAttribute.name) = "Description" Then
                     ndDescription = profileAttribute.value
                End If
            
                '' client matter cabinet
                If LCase(profileAttribute.name) = "Client" Then
                    ndClient = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Matter" Then
                    ndMatterID = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Classification" Then
                    Classification = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Sub Classification" Then
                    SubClassification = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Physical Exists" Then
                    blnPhysicalExists = profileAttribute.value
                End If
                  
                If LCase(profileAttribute.name) = "QA Completed" Then
                    blnQACompleted = profileAttribute.value
                End If
           
                '' non matter cabinet
                If LCase(profileAttribute.name) = "ToP Category" Then
                    TopCategory = profileAttribute.value
                End If
                
                If LCase(profileAttribute.name) = "Team or Project" Then
                    TeamOrProject = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Activity" Then
                    Activity = profileAttribute.value
                End If
           
           
              Next profileAttribute
                       
     End If
     
    End Sub


    ...back to vb.net to continue to process the  data variables obtain in  vba


    when I step through the code this error displays. 

    System.Runtime.InteropServices.COMException was caught
      ErrorCode=-2147352573
      HelpLink=wdmain11.chm#37401
      HResult=-2147352573
      Message=Unable to run the specified macro
      Source=Microsoft Word
      StackTrace:
           at Microsoft.Office.Interop.Word.ApplicationClass.Run(String MacroName, Object& varg1, Object& varg2, Object& varg3, Object& varg4, Object& varg5, Object& varg6, Object& varg7, Object& varg8, Object& varg9, Object& varg10, Object& varg11, Object& varg12, Object& varg13, Object& varg14, Object& varg15, Object& varg16, Object& varg17, Object& varg18, Object& varg19, Object& varg20, Object& varg21, Object& varg22, Object& varg23, Object& varg24, Object& varg25, Object& varg26, Object& varg27, Object& varg28, Object& varg29, Object& varg30)
           at Corrs.Business.DMS.DM5Library.SAPISaveDocAs(WordDocument worddocument) in C:\WIP\Corrs Office 2013\Trunk\Source\Source\Corrs.Office.Integration\Corrs.Business\DMS\DM5Library.vb:line 1543
      InnerException: 

    The Word template is loaded as prior to this code, from vb.net other vba code is called successfully

    I thought i was on the right track with passing and sharing variables between vb.net and vba

    any ideas how i can get this working

    TIA





    Thursday, July 9, 2015 12:51 AM

Answers

  • Hi dd,

    Which points that you don’t understand?

    For the function that has the return value:

    Public Function VBAReturn() As String
    VBAReturn = "hello word"
    End Function

    Get the returned value:

    object o = this.Application.Run(Macro: "VBAReturn");

    Regards

    Starain


    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.

    Friday, July 10, 2015 6:57 AM
    Moderator

All replies

  • update...

    ive updated the vb.net code call to the following, and the exception error is now cleared.

    Ive added a message box to display the variable values captured, however the values are 'nothing'

    any ideas


    Dim nddocid As String = Nothing
    Dim ndVersion As String = Nothing
    Dim ndFileName As String = Nothing
    Dim ndAuthor As String = Nothing
    Dim ndClient As String = Nothing
    Dim ndMatterID As String = Nothing
    Dim ndDescription As String = Nothing
    Dim ndCabinet As String = Nothing
    
    worddocument.Document.Application.Run(MacroName:="NDShowSaveDialog", varg1:=nddocid, varg2:=ndVersion, varg3:=ndFileName, varg4:=ndAuthor, varg5:=ndClient, varg6:=ndMatterID, varg7:=ndDescription, varg8:=ndCabinet)
    
    
    MsgBox(nddocid & "-" & ndVersion & "-" & ndFileName & "-" & ndAuthor & "-" & ndClient & "-" & ndMatterID & "-" & ndDescription & "-" & ndCabinet)


    • Edited by dd_ Thursday, July 9, 2015 4:18 AM
    Thursday, July 9, 2015 4:15 AM
  • I wasnt sure whether to post this in the Visual Basic forum or Office dev forum.

    in VS2013 vbnet a VSTO Word addin is developed.

    Workflow...

    This vb.net code calls a macro to run vba code in a Word 2013 template. The code ...

    Dim nddocid As String = Nothing
    Dim ndVersion As String = Nothing
    Dim ndFileName As String = Nothing
    Dim ndAuthor As String = Nothing
    Dim ndClient As String = Nothing
    Dim ndMatterID As String = Nothing
    Dim ndDescription As String = Nothing
    Dim ndCabinet As String = Nothing
    
    
    worddocument.Document.Application.Run(MacroName:="NDShowSaveDialog", varg1:=nddocid, varg2:=ndVersion, varg3:=ndFileName, varg4:=ndAuthor, varg5:=ndClient, varg6:=ndMatterID, varg7:=ndDescription, varg8:=ndCabinet)
    
    
    
    MsgBox(nddocid & "-" & ndVersion & "-" & ndFileName & "-" & ndAuthor & "-" & ndClient & "-" & ndMatterID & "-" & ndDescription & "-" & ndCabinet)
    

    vba code in Word 2013  template .dotm loaded  from C:\Program Files (x86)\Microsoft Office\Office15\STARTUP 

    Public Sub GetNetDocsInfo(nddocid As String, ndVersion As String, ndFileName As String, ndAuthor As String, ndClient As String, ndMatterID As Integer, ndDescription As String, ndCabinet As String)
    
    
        Dim ndo As Object
        Set ndo = CreateObject("ndOffice.EchoingDataService")
        Dim docInfo As Object
        Set docInfo = ndo.GetDocumentInfo(Application.ActiveDocument.FullName)
        
        ' If document originated from NetDocuments
        If Not docInfo Is Nothing Then
            
            '' standard fields
            ndCabinet = docInfo.cabinet
            nddocid = docInfo.Identifier   
            ndFileName = CleanFileName(docInfo.FileName)      
            ndVersion = CStr(docInfo.Version)
            
            Dim profileAttribute As Object
            
            For Each profileAttribute In docInfo.ProfileAttributes
            
           
                MsgBox profileAttribute.ID & "-" & profileAttribute.name & "-" & profileAttribute.value
            
            
                '' mandatry nd fields
                If LCase(profileAttribute.name) = "Author" Then
                    ndAuthor = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Document Date" Then
                    ndDateCreated = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "eDOCS No" Then
                     eDocsNo = profileAttribute.value
                End If
            
                If LCase(profileAttribute.name) = "eDOCS Parent Folder" Then
                     eDocsParentFolder = profileAttribute.value
                End If
                
                ' subject
                If LCase(profileAttribute.name) = "Description" Then
                     ndDescription = profileAttribute.value
                End If
            
                '' client matter cabinet
                If LCase(profileAttribute.name) = "Client" Then
                    ndClient = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Matter" Then
                    ndMatterID = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Classification" Then
                    Classification = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Sub Classification" Then
                    SubClassification = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Physical Exists" Then
                    blnPhysicalExists = profileAttribute.value
                End If
                  
                If LCase(profileAttribute.name) = "QA Completed" Then
                    blnQACompleted = profileAttribute.value
                End If
           
                '' non matter cabinet
                If LCase(profileAttribute.name) = "ToP Category" Then
                    TopCategory = profileAttribute.value
                End If
                
                If LCase(profileAttribute.name) = "Team or Project" Then
                    TeamOrProject = profileAttribute.value
                End If
           
                If LCase(profileAttribute.name) = "Activity" Then
                    Activity = profileAttribute.value
                End If
           
           
              Next profileAttribute
                       
     End If
     
    End Sub

    ...back to vb.net to continue to process the  data variables obtain in  vba

    Ive added a message box to display the variable values captured, however the values are 'nothing'

    any ideas

    TIA


    • Merged by Youjun Tang Friday, July 10, 2015 7:29 AM more appropriate
    Thursday, July 9, 2015 4:17 AM
  • Can you run the macro from within WORD?  One possible problem is security - if you have to respond to a security prompt within WORD then the macro may not run from an external application.

    FWIW,
        Dim nddocid As String = Nothing
    is not the same as
        Dim nddocid As String = ""
    which may be relevant to your last comment.


    • Edited by Acamar Thursday, July 9, 2015 4:34 AM fmt
    Thursday, July 9, 2015 4:33 AM
  • Yes I can successfully run the macro from within Word, therefore its not related to security.

    OK I implemented your suggestion and the results were 

    Dim nddocid As String = ""

    The message box displays the variable values captured, however the values display as blank or nothing


    • Edited by dd_ Thursday, July 9, 2015 5:29 AM
    Thursday, July 9, 2015 4:36 AM
  • Hi dd_

    First thing, I'm going to move this to the VSTO forum as that's where you'll have the best chance to get help with passing information between VSTO and VBA :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 9, 2015 2:06 PM
    Moderator
  • Hi dd,

    Base on my test (e.g. add ByRef to parameter, use object type), I found we can’t get the returned value through parameter.

    For this requirement, I suggest that you could return the value through the function directly (Application.Run() method return the dynamic value), for multiple result, you could combine them with special character, then split the returned value with that character. For this way, please let the macro in the module (not in the sheet).

    Regards

    Starain


    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.

    Friday, July 10, 2015 6:47 AM
    Moderator
  • Thanks Starain

    Could you pls provide an example for me to work with

    Friday, July 10, 2015 6:49 AM
  • Hi dd,

    Which points that you don’t understand?

    For the function that has the return value:

    Public Function VBAReturn() As String
    VBAReturn = "hello word"
    End Function

    Get the returned value:

    object o = this.Application.Run(Macro: "VBAReturn");

    Regards

    Starain


    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.

    Friday, July 10, 2015 6:57 AM
    Moderator
  • Many thanks Starain

    This worked :)

    Friday, July 10, 2015 7:58 AM