I have been attempting to create a way to send a copied part of my Excel workbook as an email through Outlook. The current Code I have works 9 times out of 10. The 10th time it gives me an error error. The line that Visual Basic is showing as the issue is:
pageEditor.Application.Selection.PasteAndFormat (wdFormatOriginalFormatting)
The full code is here:
Private Sub CommandButton2_Click()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Are you ready to submit?", vbYesNo, "Run Macro")
If Answer = vbYes Then
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim CSVString As String
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
With newEmail
.To = "email"
.CC = ""
.BCC = ""
.Subject = Sheet1.Range("B2")
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheet1.Range("B2:D11").Copy
pageEditor.Application.Selection.Start = 1
pageEditor.Application.Selection.End =
pageEditor.Application.Selection.Start
.Display
Application.Wait (Now + TimeValue("0:00:02"))
pageEditor.Application.Selection.PasteAndFormat
(wdFormatOriginalFormatting)
.Display
.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
MsgBox "Adjustment Form has been submitted"
End If
End Sub
It is never consistent.