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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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