Getting “Error: 91: Object variable or With block variable not set" inside a function. Not sure why. Help!

ChetShannon

Board Regular
Joined
Jul 27, 2007
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am getting an “Error: 91: Object variable or With block variable not set” inside a function I am running. Not sure why. My mouseover of the variables being passed to the function shows the values are present. (macro_name, strid, strURL, strfile all present.)

What might be causing this type of error?? Stumped I am!.. .Thanks.. My code for the function below.


Function GetGNPReport(macro_name As String, strid As String, strURL As String, Optional strfile As String) As String
Dim myVars As String
On Error GoTo getout

myHttp.Open "GET", strURL, False
'code failing on this GET statement above and goes to getout: as of tue 130pm error code 91
'strURL= web link removed
' myHttp.SetRequestHeader "User-Agent", macro_name & " " & strid

myHttp.Send
myHttp.waitForResponse

If myHttp.Status <> "200" Then
GetGNPReport = "Error: HTTP RESPONSE " & myHttp.Status & " - " & myHttp.StatusText
Exit Function
End If

If strfile <> "" Then
Dim mystream As TextStream
Dim myfsobj As New FileSystemObject
Set mystream = myfsobj.OpenTextFile(strfile, ForWriting, True)
mystream.Write myHttp.ResponseText
End If

GetGNPReport = myHttp.ResponseText

getout:
If Err.Number <> 0 Then
GetGNPReport = "Error: " & Err.Number & ": " & Err.Description
End If
End Function
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I have myHttp as a "Public myHttp As Object" above this function in a standalone section of the code. I don't have a SET statement for myHttp though. Maybe I need that?...
 
Upvote 0
You definitely need to Set myHttp somewhere. As you've declared myHttp as a global variable you could define it in any procedure which (eventually) calls GetGNPReport:
Code:
    If myHttp Is Nothing Then Set myHttp = CreateObject("MSXML2.XMLHTTP")
If myHttp is only used by GetGNPReport then I would delete the Public declaration and put this inside GetGNPReport:
Code:
    Static myHttp As Object
    If myHttp Is Nothing Then Set myHttp = CreateObject("MSXML2.XMLHTTP")
 
Upvote 0
You definitely need to Set myHttp somewhere. As you've declared myHttp as a global variable you could define it in any procedure which (eventually) calls GetGNPReport:
Code:
    If myHttp Is Nothing Then Set myHttp = CreateObject("MSXML2.XMLHTTP")
If myHttp is only used by GetGNPReport then I would delete the Public declaration and put this inside GetGNPReport:
Code:
    Static myHttp As Object
    If myHttp Is Nothing Then Set myHttp = CreateObject("MSXML2.XMLHTTP")

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top