none
Send a byte array from VBA to a WCFService RRS feed

  • Question

  • Hey guys,

    I need some help with a problem.

    So, I have a very basic WCF Service application which has method that gets a byte array as input and returns a bool. The byte array consists in the content of a document and is generated in VBA. What I try to do is to conect myself from VBA to the WCFService and pass the byte array and get the result back in the vba project. Problem is that I'm not sure how to do that. Here is what I tried:

    Public Function GetServiceData() As XMLHTTP Dim httpData As New XMLHTTP Dim webServiceURL As String webServiceURL = ipAddress & port & "/TestService/SaveAndAttach" httpData.Open "POST", webServiceURL, False httpData.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" httpData.send Example Set GetServiceData = httpData End Function End Function

    Public Function GetFileBytes(ByVal path As String) As Byte()
        Dim lngFileNum As Long
        Dim bytRtnVal() As Byte
        lngFileNum = FreeFile
        If LenB(Dir(path)) Then ''// Does file exist?
            Open path For Binary Access Read As lngFileNum
            ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
            Get lngFileNum, , bytRtnVal
            Close lngFileNum
        Else
            Err.Raise 53
        End If
        GetFileBytes = bytRtnVal
        Erase bytRtnVal
    End Function

    Public Function Example() As Byte()
        Dim byteFile() As Byte
        byteFile = GetFileBytes("C:\Desktop\FilesForTesting\TestDocument13_2.docx")
        Example = byteFile
    End Function

    Web config file from WCFService

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
      </appSettings>
      <!--
        For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
    
        The following attributes can be set on the <httpRuntime> tag.
          <system.Web>
            <httpRuntime targetFramework="4.5.2" />
          </system.Web>
      -->
      <system.web>
        <compilation debug="true" targetFramework="4.5.2"/>
        <httpRuntime targetFramework="4.5.2"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="1"/>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    </configuration>

    I added a break point in the service and from what I know when you send data to it should trigger.

    Is there any extra settings I should do in the WCF Service project? 

    Is the VBA code wrong? (I haven't worked too much in VBA so might be a stupid mistake I did).

    Thank you very much for the time.



    • Moved by CoolDadTx Thursday, August 23, 2018 1:53 PM VBA related
    • Edited by Bogdan-Sorin Friday, August 24, 2018 5:52 AM edit
    Thursday, August 23, 2018 12:09 PM

Answers

  • Unfortunately, it doesn't work with XMLHTTP object to calling WCF service. Also, according to my research, VBA access WCF can be implemented by TCP but has limitations, it only fits the return value or the parameter is a common type (Int, string, etc.) or an array of commonly used types. You can reference this post

    the post meaning is that Create a COM library to access WCF, and VBA accesses WCF indirectly through COM.

    Structure:

    Hopefully it helps you or provide new ideas for you.

    Best Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by Bogdan-Sorin Tuesday, August 28, 2018 7:23 AM
    Monday, August 27, 2018 3:24 AM
    Moderator

All replies

  • Hi Bogdan,

    Could you please provide your SaveAndAttch function code for my test ? Also, you can use SOAP to send your file to WCF Client as well. 

    More information: how-to-transfer-large-file-from-ms-word-add-in-vba-to-web-server

    Hopefully it helps you and feedback to me.

    Best Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, August 24, 2018 8:55 AM
    Moderator
  • Hi Simon, 

    Thanks alot for your time.

            public string SaveAndAttach(byte [] doc)
            {
                if (doc != null)
                {
                    using (Stream file = File.OpenWrite(@"C:\Users\xxxx\Desktop\WebService - Test Folder\test.docx"))
                    {
                        file.Write(doc, 0, doc.Length);
                        return "All good";
                    }
                }
    
                return "All bad";
            }

    Its just a dummy test method. What I want to achieve is to send a byte array from vba containing the content of a file to this wcf service so he can save it to a location.

    I think all problems are from my config file, don't want use soap tho.


    • Edited by Bogdan-Sorin Friday, August 24, 2018 9:05 AM edit
    Friday, August 24, 2018 9:03 AM
  • OK,  I will get back to you if any updates.

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, August 24, 2018 9:39 AM
    Moderator
  • Unfortunately, it doesn't work with XMLHTTP object to calling WCF service. Also, according to my research, VBA access WCF can be implemented by TCP but has limitations, it only fits the return value or the parameter is a common type (Int, string, etc.) or an array of commonly used types. You can reference this post

    the post meaning is that Create a COM library to access WCF, and VBA accesses WCF indirectly through COM.

    Structure:

    Hopefully it helps you or provide new ideas for you.

    Best Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    • Marked as answer by Bogdan-Sorin Tuesday, August 28, 2018 7:23 AM
    Monday, August 27, 2018 3:24 AM
    Moderator