Email -How do I place cursor at the end of the bod

2019-09-18 12:26发布

I am using the answer (i.e. the code) to this question Working with current open email to build an email in steps. Every time I run the macro, it adds a piece of body text to the existing body text of an open email. The problem is that if I manually do some changes to the email between the macro "runs", then upon next run, the macro will insert the new body text building block where I left the cursor. But I want the macro to always add the new body text after the end of the existing (and expanding) body text. More specifically, this means that I (probably) need some code just before the line with the code "oSelection.InsertAfter myText" (see the code in the above link) that moves the cursor (insertion point) to the end of the open email that I am working with.

I have tried to play around with the SendKeys-command but using that I only manage to send the cursor to the end of the excel workbook sheet where I have the macro button. I want the cursor to end of the open email instead!

3条回答
倾城 Initia
2楼-- · 2019-09-18 12:49

The macro leaves the cursor in the subject field. This will jump cursor down into the body, and move it to the end of the line:

SendKeys "{Tab}{End}", True

Full Example:

Public Sub CreateNewMessage() Dim objMsg As MailItem Set objMsg = Application.CreateItem(olMailItem) With objMsg .To = "fake@example.com" .CC = "other@example.com" .BCC = "" .Subject = "Test email for you" .Categories = "" .VotingOptions = "" .BodyFormat = olFormatPlain ' send plain text message .Body = "Thank you for your submission. " .Display SendKeys "{Tab}{End}", True End With Set objMsg = Nothing End Sub

References:

http://www.slipstick.com/developer/create-a-new-message-using-vba/

查看更多
我命由我不由天
3楼-- · 2019-09-18 12:59

This line:

SendKeys "{Tab}{End}", True

left an inactive cursor at the beginning of the body of my e-mail message. By manually tabbing, I noticed the cursor moving through the From, To, CC, BCC, etc. fields. It took 11 tabs, and 1 End to get the cursor to the end of a line of text in my e-mail. Silly though it seemed, I coded the extra tabs in, and it worked perfectly.

With Mess
    .Display
    .Sensitivity = 3
    .To = Recip
    .bcc = "redacted recipient"
    .subject = subject
    .Attachments.Add Path & fileName & ".pdf"
    .HTMLBody = strbody & "<br>" & .HTMLBody
SendKeys "{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{End}", True
查看更多
我命由我不由天
4楼-- · 2019-09-18 13:12

Ok, so I finally figured it out:

  If oInspector.IsWordMail Then
            With NewMail
             .Display
             SendKeys "^+{END}", True
             SendKeys "{END}", True
             SendKeys "{NUMLOCK}"
            End With
查看更多
登录 后发表回答