Object object variable or with block variable not set

trishgyrl

New Member
Joined
Jan 16, 2018
Messages
21
Please help. I have no clue why I'm getting an error that says 'object variable or with block variable not set"

I have a userform with several textboxes, checkboxes, and radio buttons on it. They are not all required. My goal is to get the values or captions of these controls to display in the body of an email.

The code below works fine as long as none of the checkboxes or textboxes are empty. What do I need to do to prevent this error?


Code:
If Me.optExisting.Value = True Then   strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "<b><i>Please refer to the details of the request below:</i></b>" & _
    "<br><br>" & _
    "********************************************" & _
    "<b>" & Me.lblDate & ": " & "</b>" & Me.txtDate.Value & _
    "<br><br>" & _
    "<b>" & Me.lblBusinessNeed & ": " & "</b>" & Me.txtBusinessNeed & _
    "<br><br>" & _
    "<b>" & Me.lblProduct & ": " & "</b>" & Me.txtProduct & _
    "<br><br>" & _
    "<b>" & Me.lblCopyFrom & ": " & "</b>" & Me.txtCopyFrom & _
    "<br><br>" & _
    "<b>" & Me.lblExtend & ": " & "</b>" & Me.txtExtend & _
    "<br><br>" & _
    "<b>" & Me.lblAddDeal & ": " & "</b>" & Me.frameAddDeal.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & Me.lblNewExist & ": " & "</b>" & Me.frameNewExist.ActiveControl.Caption & _
    "<br><br>" & _
    strBody = strBody & "****************Deal Information Below****************************" & _
    "<br><br>" & _
    "<b>" & Me.chkTerminal.Caption & ": " & "</b>" & Me.txtTerminal.Value & " - " & Me.frameTerminal.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & Me.chkBlend.Caption & ": " & "</b>" & Me.txtBlend.Value & " - " & Me.frameBlend.ActiveControl.Caption & _
    "<br><br>" & _
    "********************************************" & "</BODY>"
     End If
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi trishgyrl. I'm not that familiar with VBAing e-mails but as a general statement using Me.whatever isn't that good. U should use the name of the form like Userform1.whatever. As far as the code goes....
Code:
Me.lblDate & ": " & "" & Me.txtDate.Value
Not sure what lblDate control is, seems like a label, but should be ... Userform1.lblDate.text (or .caption or .value).. I'm not sure but it should be something. I'm guessing that it also must be text/string entry into the e-mail so it should be Userform1.txtDate.Text or CSTR(Userform1.txtDate.Value). I'm also guessing that if there is no value that U will get an error. Therefore U need to check for a value before trying to put it in the e-mail. Something like...
Code:
if Userform1.txtDate.Text =vbnullstring then
msgbox "no entry for txtdate"
exit sub
end if
A whole lot of guessing but maybe it will be useful. Dave
 
Upvote 0
For some reason it's not liking that whole part beyond the section that starts with "strBody = strBody &..."...

Code:
If Me.optExisting.Value = True Then   strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "<b><i>Please refer to the details of the request below:</i></b>" & _
    "<br><br>" & _
    "********************************************" & _
    "<br><br>" & _
    "<b>" & Me.lblDate & ": " & "</b>" & Me.txtDate.Value & _
    "<br><br>" & _
    "<b>" & Me.lblBusinessNeed & ": " & "</b>" & Me.txtBusinessNeed & _
    "<br><br>" & _
    "<b>" & Me.lblProduct & ": " & "</b>" & Me.txtProduct & _
    "<br><br>" & _
    "<b>" & Me.lblCopyFrom & ": " & "</b>" & Me.txtCopyFrom & _
    "<br><br>" & _
    "<b>" & Me.lblExtend & ": " & "</b>" & Me.txtExtend & _
    "<br><br>" & _
    "<b>" & Me.lblAddDeal & ": " & "</b>" & Me.frameAddDeal.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & Me.lblNewExist & ": " & "</b>" & Me.frameNewExist.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & Me.frameDealType.Caption & ": " & "</b>" & Me.frameDealType.ActiveControl.Caption & _
    "<br><br>"
    strBody = strBody & "<b>" & "****************Deal Information Below****************************" & _
    "<br><br>" & _
    "<b>" & CreateProduct.chkTerminal.Caption & ": " & "</b>" & Me.txtTerminal.Value & " - " & Me.frameTerminal.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & CreateProduct.chkBlend.Caption & ": " & "</b>" & CreateProduct.txtBlend.Value & " - " & CreateProduct.frameBlend.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & CreateProduct.chkStorage.Caption & ": " & "</b>" & CreateProduct.txtStorage.Value & " - " & CreateProduct.frameStorage.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & CreateProduct.chkRail.Caption & ": " & "</b>" & CreateProduct.txtRail.Value & " - " & CreateProduct.frameRail.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & CreateProduct.chkPipeline.Caption & ": " & "</b>" & CreateProduct.txtPipeline.Value & " - " & CreateProduct.framePipeline.ActiveControl.Caption & _
    "<br><br>" & _
    "<b>" & CreateProduct.chkTruck.Caption & ": " & "</b>" & CreateProduct.txtTruck.Value & " - " & CreateProduct.frameTruck.ActiveControl.Caption & _
    "<br><br>" & _
    "********************************************" & "</BODY>" & _
    "<br><br>" & _
    "<b>" & Me.lblComments & ": " & "</b>" & Me.txtComments.Value
     End If
 
Upvote 0
Try removing the End If. It's difficult to follow what that code is doing but it looks like a one line If, rather than a block if.
 
Upvote 0
Is this really all on one line or is it on two?
Code:
If Me.optExisting.Value = True Then   strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "[B][I]Please refer to the details of the request below:[/I][/B]" & _
I ask because sometimes this forum does weird things and merge the first two rows into one. It should be on two, like this:
Code:
If Me.optExisting.Value = True Then 
    strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "[B][I]Please refer to the details of the request below:[/I][/B]" & _

since you have an "End If" statement and more code in between it.

If you have more than one line of code after the IF statement, the structure needs to look like this:
Code:
If ... Then
    '[I]lines of code here[/I]
End If
If it is just one single line of code to do after your IF, you can do it all in one line like this:
Code:
[COLOR=#333333]If ... Then '[I]line[/I][/COLOR][I] of code here[/I]

But then you would not use any "End If" statement, since it is all contained in a single line.
 
Last edited:
Upvote 0
Is this really all on one line or is it on two?

It's on two lines...I'll re-paste it the way I have it.

Code:
If Me.optExisting.Value = True Then
   strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "Please refer to the details of the request below:" & _
    "" & _
    "********************************************" & _
    "" & _
    "" & Me.lblDate & ": " & "" & Me.txtDate.Value & _
    "" & _
    "" & Me.lblBusinessNeed & ": " & "" & Me.txtBusinessNeed & _
    "" & _
    "" & Me.lblProduct & ": " & "" & Me.txtProduct & _
    "" & _
    "" & Me.lblCopyFrom & ": " & "" & Me.txtCopyFrom & _
    "" & _
    "" & Me.lblExtend & ": " & "" & Me.txtExtend & _
    "" & _
    "" & Me.lblAddDeal & ": " & "" & Me.frameAddDeal.ActiveControl.Caption & _
    "" & _
    "" & Me.lblNewExist & ": " & "" & Me.frameNewExist.ActiveControl.Caption & _
    "" & _
    "" & Me.frameDealType.Caption & ": " & "" & Me.frameDealType.ActiveControl.Caption & _
    ""
    strBody = strBody & "" & "****************Deal Information Below****************************" & _
    "" & _
    "" & CreateProduct.chkTerminal.Caption & ": " & "" & Me.txtTerminal.Value & " - " & Me.frameTerminal.ActiveControl.Caption & _
    "" & _
    "" & CreateProduct.chkBlend.Caption & ": " & "" & CreateProduct.txtBlend.Value & " - " & CreateProduct.frameBlend.ActiveControl.Caption & _
    "" & _
    "" & CreateProduct.chkStorage.Caption & ": " & "" & CreateProduct.txtStorage.Value & " - " & CreateProduct.frameStorage.ActiveControl.Caption & _
    "" & _
    "" & CreateProduct.chkRail.Caption & ": " & "" & CreateProduct.txtRail.Value & " - " & CreateProduct.frameRail.ActiveControl.Caption & _
    "" & _
    "" & CreateProduct.chkPipeline.Caption & ": " & "" & CreateProduct.txtPipeline.Value & " - " & CreateProduct.framePipeline.ActiveControl.Caption & _
    "" & _
    "" & CreateProduct.chkTruck.Caption & ": " & "" & CreateProduct.txtTruck.Value & " - " & CreateProduct.frameTruck.ActiveControl.Caption & _
    "" & _
    "********************************************" & "" & _
    "" & _
    "" & Me.lblComments & ": " & "" & Me.txtComments.Value
     End If
 
Last edited by a moderator:
Upvote 0
It tells me Block If without End If.​
Did you read completely what I wrote in my previous post?
Do you really have something after the work "Then" on the first line of code, or is it really on the second line (and the board just changed it to look that way)?
If it really is on the next line and not directly after the word "Then" on the first line, then you need the "End If".
If there really is stuff on the first line after the word "Then", then you would not have the "End If", as I explained (and there is no current problem with the construct of your IF...THEN statement).
 
Upvote 0
Did you read completely what I wrote in my previous post?
Do you really have something after the work "Then" on the first line of code, or is it really on the second line (and the board just changed it to look that way)?

It is really on the second line...the board just changed it to look that way.
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,086
Members
448,944
Latest member
sharmarick

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