Hello All,
I have some code that finally works as needed however now the only issue is ....its slow. In the paste my code would only work some of the time due to, as I discovered, range b13:c21 would not always copy to the clipboard. With the error handler it does, yet takes 15 seconds+ to finally paste to the body of the email. I noticed when I click in the body of the email it appears a bit quicker then simply waiting for the code to execute. I have been searching for some time on how to make this quicker, and have found using Application.ScreenUpdating = False, Application.Calculation = xlCalculationManual and Application.EnableEvents = False should help, but for me it doesn't seem to make a difference. Any ideas? I appreciate all your help!!! This forum has really helped me learn about excel.
code:
Sub email()
Application.ScreenUpdating = False
Dim oOutlook As Object
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
If oOutlook Is Nothing Then
MsgBox "Outlook is not open, open Outlook and try again"
Else
Call CopyAndPasteToMailBody
End If
Application.ScreenUpdating = True
End Sub
Sub CopyAndPasteToMailBody()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim mailapp As Object
Dim Mail As Object
Dim mysubject As String
mysubject = Worksheets("sheet1").Range("B10").value
Set mailapp = CreateObject("Outlook.Application")
Set Mail = mailapp.CreateItem(olMailItem)
Mail.display
Set wEditor = mailapp.ActiveInspector.wordEditor
pg1copyattempt:
DoEvents
ActiveSheet.Range("B13:C21").Copy
On Error GoTo pg1pastefail
wEditor.Application.Selection.paste
On Error GoTo 0
On Error Resume Next
With Mail
.To = "me@me.com"
.CC = ""
.BCC = ""
.Subject = mysubject
.display
End With
Exit Sub
pg1pastefail:
If Err.Number = 4605 Then
DoEvents
Resume pg1copyattempt
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
I have some code that finally works as needed however now the only issue is ....its slow. In the paste my code would only work some of the time due to, as I discovered, range b13:c21 would not always copy to the clipboard. With the error handler it does, yet takes 15 seconds+ to finally paste to the body of the email. I noticed when I click in the body of the email it appears a bit quicker then simply waiting for the code to execute. I have been searching for some time on how to make this quicker, and have found using Application.ScreenUpdating = False, Application.Calculation = xlCalculationManual and Application.EnableEvents = False should help, but for me it doesn't seem to make a difference. Any ideas? I appreciate all your help!!! This forum has really helped me learn about excel.
code:
Sub email()
Application.ScreenUpdating = False
Dim oOutlook As Object
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
If oOutlook Is Nothing Then
MsgBox "Outlook is not open, open Outlook and try again"
Else
Call CopyAndPasteToMailBody
End If
Application.ScreenUpdating = True
End Sub
Sub CopyAndPasteToMailBody()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim mailapp As Object
Dim Mail As Object
Dim mysubject As String
mysubject = Worksheets("sheet1").Range("B10").value
Set mailapp = CreateObject("Outlook.Application")
Set Mail = mailapp.CreateItem(olMailItem)
Mail.display
Set wEditor = mailapp.ActiveInspector.wordEditor
pg1copyattempt:
DoEvents
ActiveSheet.Range("B13:C21").Copy
On Error GoTo pg1pastefail
wEditor.Application.Selection.paste
On Error GoTo 0
On Error Resume Next
With Mail
.To = "me@me.com"
.CC = ""
.BCC = ""
.Subject = mysubject
.display
End With
Exit Sub
pg1pastefail:
If Err.Number = 4605 Then
DoEvents
Resume pg1copyattempt
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub