Excel VBA: Sent Outlook email does not include pasted Range

前端 未结 1 1241
萌比男神i
萌比男神i 2021-01-20 01:09

I have initially answered question How to only paste visible cells in an email body

The code I tested and posted (see below) did not include sending email. After the

相关标签:
1条回答
  • 2021-01-20 01:38

    But if I run the whole Sub at once, with no breakpoints, the email is sent with no pasted Excel Range in the body. What is the reason for that, and what is the solution?

    The reason is pretty simple. When you use breakpoint, you are giving Excel enough time for a copy paste. SendKeys hence are very unreliable when working with other applications.

    There are many ways to solve your problem. One is to give enough time for a copy paste. You can do that by using DoEvents or forcing a Wait Time. For example

    SendKeys "^({v})", True
    DoEvents
    

    Or

    SendKeys "^({v})", True
    Wait 2 '<~~ Wait for 2 seconds
    

    and use this sub in your code

    Private Sub Wait(ByVal nSec As Long)
        nSec = nSec + Timer
        While nSec > Timer
            DoEvents
        Wend
    End Sub
    

    BTW, instead of using SendKeys you can use the RangetoHTML function by Ron de Bruin as shown HERE

    EDIT

    If the above doesn't work then that means SendKeys is getting executed too fast in that case, use DoEvents/Wait right after .Display as well.

    .Display
    DoEvents
    

    Or

    .Display
    Wait 2
    
    0 讨论(0)
提交回复
热议问题