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
 
Does your code look like post#7, I've added a linebreak to it.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
OK, then what Fluff and I were talking about doesn't really apply here.

I would focus on the reply from Dave, especially his recommendation on not using "Me".
 
Upvote 0
Could you also post the entire code, as the problem could well lie elsewhere.
 
Upvote 0
Here is the entire sub procedure:

Code:
Sub Send_Mail(myfile As String)    Dim OutApp As Object
    Dim OutMail As Object
    Dim strTo As String
    Dim strSubject As String
    Dim strBody As String
    Dim strBody1 As String
    Dim strBody2 As String
    Dim sig$
    Dim ctrl As Control
    Dim ctrlCheck As String






    
If Me.NoDeal.Value = True Then
    strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "[B][I]Please refer to the details of the request below:[/I][/B]" & _
    "

" & _
    "********************************************" & _
    "

" & _
    "[B]" & Me.lblDate & ": " & "[/B]" & Me.txtDate.Value & _
    "

" & _
    "[B]" & Me.lblBusinessNeed & ": " & "[/B]" & Me.txtBusinessNeed & _
    "

" & _
    "[B]" & Me.lblProduct & ": " & "[/B]" & Me.txtProduct & _
    "

" & _
    "[B]" & Me.lblCopyFrom & ": " & "[/B]" & Me.txtCopyFrom & _
    "

" & _
    "[B]" & Me.lblExtend & ": " & "[/B]" & Me.txtExtend & _
    "

" & _
    "[B]" & Me.lblAddDeal & ": " & "[/B]" & Me.frameAddDeal.ActiveControl.Value & _
    "

" & _
    "[B]" & Me.lblComments & ": " & "[/B]" & Me.txtComments & _
    "

" & _
    "********************************************" & ""
          
Else
    
If Me.optNew.Value = True Then
    strBody = "****** style=" & "font face=calibri" & "font size=11" & ">" & "[B][I]Please refer to the details of the request below:[/I][/B]" & _
    "

" & _
    "********************************************" & _
    "

" & _
    "[B]" & Me.lblDate & ": " & "[/B]" & Me.txtDate.Value & _
    "

" & _
    "[B]" & Me.lblBusinessNeed & ": " & "[/B]" & Me.txtBusinessNeed & _
    "

" & _
    "[B]" & Me.lblProduct & ": " & "[/B]" & Me.txtProduct & _
    "

" & _
    "[B]" & Me.lblCopyFrom & ": " & "[/B]" & Me.txtCopyFrom & _
    "

" & _
    "[B]" & Me.lblExtend & ": " & "[/B]" & Me.txtExtend & _
    "

" & _
    "[B]" & Me.lblAddDeal & ": " & "[/B]" & Me.frameAddDeal.ActiveControl.Caption & _
    "

" & _
    "[B]" & Me.lblNewExist & ": " & "[/B]" & Me.frameNewExist.ActiveControl.Caption & _
    "

" & _
    "[B]" & Me.lblComments & ": " & "[/B]" & Me.txtComments & _
    "

" & _
    "********************************************" & ""
    
Else
    
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]" & _
    "

" & _
    "********************************************" & _
    "

" & _
    "[B]" & Me.lblDate & ": " & "[/B]" & Me.txtDate.Value & _
    "

" & _
    "[B]" & Me.lblBusinessNeed & ": " & "[/B]" & Me.txtBusinessNeed & _
    "

" & _
    "[B]" & Me.lblProduct & ": " & "[/B]" & Me.txtProduct & _
    "

" & _
    "[B]" & Me.lblCopyFrom & ": " & "[/B]" & Me.txtCopyFrom & _
    "

" & _
    "[B]" & Me.lblExtend & ": " & "[/B]" & Me.txtExtend & _
    "

" & _
    "[B]" & Me.lblAddDeal & ": " & "[/B]" & Me.frameAddDeal.ActiveControl.Caption & _
    "

" & _
    "[B]" & Me.lblNewExist & ": " & "[/B]" & Me.frameNewExist.ActiveControl.Caption & _
    "

"
[COLOR=#b22222]    strBody = strBody & "****************Deal Information Below****************************" & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & Me.chkTerminal.Caption & ": " & "[/B]" & Me.txtTerminal.Value & " - " & Me.frameTerminal.ActiveControl.Caption & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & Me.chkBlend.Caption & ": " & "[/B]" & Me.txtBlend.Value & " - " & Me.frameBlend.ActiveControl.Caption & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & CreateProduct.chkStorage.Caption & ": " & "[/B]" & CreateProduct.txtStorage.Value & " - " & CreateProduct.frameStorage.ActiveControl.Caption & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & CreateProduct.chkRail.Caption & ": " & "[/B]" & CreateProduct.txtRail.Value & " - " & CreateProduct.frameRail.ActiveControl.Caption & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & CreateProduct.chkPipeline.Caption & ": " & "[/B]" & CreateProduct.txtPipeline.Value & " - " & CreateProduct.framePipeline.ActiveControl.Caption & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & CreateProduct.chkTruck.Caption & ": " & "[/B]" & CreateProduct.txtTruck.Value & " - " & CreateProduct.frameTruck.ActiveControl.Caption & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "********************************************" & "" & _[/COLOR]
[COLOR=#b22222]    "

" & _[/COLOR]
[COLOR=#b22222]    "[B]" & Me.lblComments & ": " & "[/B]" & Me.txtComments.Value[/COLOR]
End If
End If
End If
    
    Set OutApp = New Outlook.Application
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    With OutMail
        .Display
        sig = .HTMLBody
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = Me.Caption
        .HTMLBody = strBody
       ' .Attachments.Add myfile
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub

The other two If statements work perfectly (where there would be no null values), I'm thinking it's something with there being empty check boxes. When the error pops up, it highlights the code that I've put in red font.
 
Last edited:
Upvote 0
Try
Code:
Sub Send_Mail(myfile As String)
   Dim OutApp As Object
   Dim OutMail As Object
   Dim strTo As String
   Dim strSubject As String
   Dim strBody As String
   Dim strBody1 As String
   Dim strBody2 As String
   Dim sig$
   Dim ctrl As Control
   Dim ctrlCheck As String

    
   If Me.NoDeal.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.Value & _
          "" & _
          "" & Me.lblComments & ": " & "" & Me.txtComments & _
          "" & _
          "********************************************" & ""
                
   ElseIf Me.optNew.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.lblComments & ": " & "" & Me.txtComments & _
          "" & _
          "********************************************" & ""
          
   ElseIf 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 & _
             ""
             strBody = strBody & "****************Deal Information Below****************************" & _
             "" & _
             "" & Me.chkTerminal.Caption & ": " & "" & Me.txtTerminal.Value & " - " & Me.frameTerminal.ActiveControl.Caption & _
             "" & _
             "" & Me.chkBlend.Caption & ": " & "" & Me.txtBlend.Value & " - " & Me.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
    
    Set OutApp = New Outlook.Application
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    With OutMail
        .Display
        sig = .HTMLBody
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = Me.Caption
        .HTMLBody = strBody
       ' .Attachments.Add myfile
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    
End Sub
 
Upvote 0
Still getting the same error that highlights the same lines of code. *sigh*

So, let me ask this question... Is it possible to show the caption of an unchecked checkbox?

Maybe that where it's hanging up?? Because I wouldn't have all boxes checked on the form...
 
Upvote 0
That shouldn't be a problem.
Check that your controls are named correctly
 
Upvote 0
Posting the same question to multiple sites is known as "Cross-Posting". While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Oh okay. My apologies. Yes, It's the same project I've been working on for awhile but I decided to just list out and display the value of all controls. I thought I was asking two different questions.

On the other post, I was trying to see how if I could only return ones with a value.

Now, I just decided to list them all out. I'm stuck on that one part.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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