Compile error: "End With without With"

GizmoL

New Member
Joined
Mar 25, 2019
Messages
4
Hi Guys :),

Could you please help me in correcting the below MACRO?
Wenrning it, I am getting a compile error: "End With without With" message.
Thanks a mill


' email Macro

Code:
Set oOutlook =CreateObject("Outlook.Application")
Set oNameSpace =oOutlook.GetNameSpace("MAPI")
oNameSpace.Logon , , True
ans = MsgBox("Are you sure youwish to send this email?", vbYesNo)
If (ans = vbYes) Then
Set MItem = oOutlook.CreateItem(0)
With MItem
.To = Range("C4").Value
.Subject = Range("C7").Value
.Body = Range("C6").Value
If (.Range("B8").Value <>"") Then
.Attachments.AddRange("B8").Value
If (.Range("C8").Value <>"") Then
.Attachments.AddRange("C8").Value
If (.Range("D8").Value <>"") Then
.Attachments.AddRange("D8").Value
.Display
End With
End Sub
 
Last edited by a moderator:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Re: Compile error: "End With without With" - please HELP

Hi & welcome to MrExcel
You are in fact missing a number of End If lines.
Try
Code:
Set oOutlook = CreateObject("Outlook.Application")
Set oNameSpace = oOutlook.GetNameSpace("MAPI")
oNameSpace.Logon , , True
ans = MsgBox("Are you sure youwish to send this email?", vbYesNo)
If (ans = vbYes) Then
   Set MItem = oOutlook.CreateItem(0)
   With MItem
      .To = Range("C4").Value
      .Subject = Range("C7").Value
      .Body = Range("C6").Value
      If (.Range("B8").Value <> "") Then
         .Attachments.AddRange("B8").Value
      ElseIf (.Range("C8").Value <> "") Then
         .Attachments.AddRange("C8").Value
      ElseIf (.Range("D8").Value <> "") Then
         .Attachments.AddRange("D8").Value
      End If
      .Display
   End With
End If
End Sub
 
Upvote 0
Re: Compile error: "End With without With" - please HELP

Thanks Fluff :),

I am now getting an run-time error '438' object doesn't support this property or method message

when I click on debug, it brings me to the below line and highlight it:
If (.Range("B8").Value <> "") Then
 
Upvote 0
Re: Compile error: "End With without With" - please HELP

Try this. It is recommended to use indentation to identify the structure of each statement

Code:
Sub test()
' email Macro


    Set oOutlook = CreateObject("Outlook.Application")
    Set oNameSpace = oOutlook.GetNameSpace("MAPI")
    oNameSpace.Logon , , True
    ans = MsgBox("Are you sureyou wish to send this email?", vbYesNo)
[COLOR=#0000ff]    If (ans = vbYes) Then[/COLOR]
        Set MItem = oOutlook.CreateItem(0)
        With MItem
            .To = Range("C4").Value
            .Subject = Range("C7").Value
            .Body = Range("C6").Value
            If Range("B8").Value <> "" Then .Attachments.Add Range("B8").Value
            If Range("C8").Value <> "" Then .Attachments.Add Range("C8").Value
            If Range("D8").Value <> "" Then .Attachments.Add Range("D8").Value
            .Display
        End With
[COLOR=#0000ff]    End If[/COLOR]
End Sub
 
Upvote 0
Re: Compile error: "End With without With" - please HELP

Hi DanteAmor,

it worked.
Thanks a million :)
You made my day.

ps: I will use indentation going forward.
 
Upvote 0
Re: Compile error: "End With without With" - please HELP

I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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