I am having an issue with calling a web service from Excel using VBA and XMLHTTP. I got it to work in 2013 with the following code:
Private Sub btnQryMaterialsIn_Click()
Dim mResult As VbMsgBoxResult
mResult = MsgBox("You are about to pull a list of all materials. This process could take several minutes and will erase everthing in the ""Data"" worksheet.", vbOKCancel)
If mResult = VbMsgBoxResult.vbOK Then
'Initialize the Sheet
Sheet2.Cells.Delete
Sheet2.Range("A1").Value = "ID"
Sheet2.Range("B1").Value = "Created Date"
Sheet2.Range("C1").Value = "Modified Date"
Sheet2.Range("D1").Value = "Product Category ID"
Sheet2.Range("E1").Value = "Unit of Measure"
Sheet2.Range("F1").Value = "Description"
Sheet2.Range("G1").Value = "Details"
'Format the rows and columns
Sheet2.Range("A1").EntireRow.Font.Bold = True
Sheet2.Range("A1").EntireColumn.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
Sheet2.Range("B1").EntireColumn.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
Sheet2.Range("C1").EntireColumn.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
Sheet2.Range("D1").EntireColumn.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
Sheet2.Range("E1").EntireColumn.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
Sheet2.Range("F1").EntireColumn.ColumnWidth = 40
Sheet2.Range("F1").EntireColumn.WrapText = True
Sheet2.Range("G1").EntireColumn.ColumnWidth = 100
Sheet2.Range("G1").EntireColumn.WrapText = True
'Initalize Processing Variables
Dim iRow As Integer
Dim iCurrentRow As Integer
Dim iAscii As Integer
Dim sEnvelope As String
iRow = 2
iCurrentRow = 2
iAscii = 48
Sheet1.Range("A6").Value = "Started... Please wait"
'The loops search the materials beginning with those that start and end if "1".
'It will then move to "1*2", then "1.3", until it reaches "Z.Z"
Do While iAscii <= 90
'Skip some non-aplhanumberic characters
If iAscii = 58 Then iAscii = 65
For iLast = 48 To 90
'Skip some non-aplhanumberic characters
If iLast = 58 Then iLast = 65
Sheet1.Range("A7").Value = Chr(iAscii) + "*" + Chr(iLast)
sEnvelope = "<soap:Envelope version=""1.1"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:glob=""http://sap.com/xi/SAPGlobal20/Global"" xmlns:a01=""http://sap.com/xi/AP/CustomerExtension/BYD/A01R4"">" & _
"<soap:Header/>" & _
"<soap:Body>" & _
"<glob:MaterialByElementsQuery_sync>" & _
"<MaterialSelectionByElements>" & _
"<SelectionByInternalID>" & _
"<InclusionExclusionCode>I</InclusionExclusionCode>" & _
"<IntervalBoundaryTypeCode>1</IntervalBoundaryTypeCode>" & _
"<LowerBoundaryInternalID>@materialid</LowerBoundaryInternalID>" & _
"<UpperBoundaryInternalID/>" & _
"</SelectionByInternalID>" & _
"</MaterialSelectionByElements>" & _
"<ProcessingConditions>" & _
"<!--Optional:-->" & _
"<QueryHitsUnlimitedIndicator>true</QueryHitsUnlimitedIndicator>" & _
"</ProcessingConditions>" & _
"</glob:MaterialByElementsQuery_sync>" & _
"</soap:Body>" & _
"</soap:Envelope>"
'"<QueryHitsMaximumNumberValue>250</QueryHitsMaximumNumberValue>"
sEnvelope = Replace(sEnvelope, "@materialid", Chr(iAscii) & "*" & Chr(iLast))
Dim request As New MSXML2.XMLHTTP
Dim xmlResult As New MSXML2.DOMDocument
With request
.Open "POST", Sheet1.Range("B4").Value, False, "_OCI_EXTERNA", "_OCI_EXTERNAa1"
.setRequestHeader "Content-Type", "text/xml"
.setRequestHeader "AllDBName ", "http://sap.com/cpm/sm/webservices/ssm/"
.send (sEnvelope)
xmlResult.LoadXML .responseText
End With
Dim xmlNode As IXMLDOMNode
For Each xmlNode In xmlResult.SelectNodes("//soap-env:Envelope/soap-env:Body/n0:MaterialByElementsResponse_sync/Material")
Sheet2.Range("A" + CStr(iRow)).EntireRow.VerticalAlignment = Excel.XlVAlign.xlVAlignTop
Sheet2.Range("A" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "InternalID")
Sheet2.Range("B" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "SystemAdministrativeData/CreationDateTime")
Sheet2.Range("C" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "SystemAdministrativeData/LastChangeDateTime")
Sheet2.Range("D" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "ProductCategoryID")
Sheet2.Range("E" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "BaseMeasureUnitCode")
Sheet2.Range("F" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "Description/Description")
Sheet2.Range("G" + CStr(iRow)).Value2 = GetValueFromElement(xmlNode, "Detail/ContentText")
iRow = iRow + 1
Next
If iRow > iCurrentRow Then
Sheet1.Range("A6").Value = Str(iRow) + " Rows Processed (Still Processing)"
iCurrentRow = iRow
End If
Next
iAscii = iAscii + 1
Loop
Sheet2.Range("A1").EntireColumn.AutoFit
Sheet2.Range("B1").EntireColumn.AutoFit
Sheet2.Range("C1").EntireColumn.AutoFit
Sheet2.Range("D1").EntireColumn.AutoFit
Sheet2.Range("E1").EntireColumn.AutoFit
Sheet1.Range("A6").Value = Str(iRow) + " Rows Processed (Complete)"
Sheet1.Range("A7").Delete
End If
End Sub
I am getting an error in 2010 when sending the request. The error is:
![VBA Issue.jpg]()
I checked browser versions and default browsers and they are the same as with the computer that has 2013.
Has anyone run into this issue with two different versions of Excel?
rd