0

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!

Community
  • 1
  • 1
neo
  • 1
  • 1
  • 1
  • `manually do some changes` well, easiest solution is to not manually do anything until all macros are done? – findwindow Apr 21 '16 at 16:47
  • true -but what if the user does not have a clue? The underlying aim is to be user-friendly _and_ have a user-stable macro. – neo Apr 21 '16 at 16:53
  • Right. In which case, user shouldn't manually do anything? I posed that suggestion only because if something needs to be done manually at some point then kinda pointless to code something only to get it 99%? Makes sense? – findwindow Apr 21 '16 at 16:55
  • 1
    You could just get rid of the `if` statement and the rich text part of the code (keep everything after the `else`). Unless you are doing something special with the richtext version of the email. Everything after the else just appends text to the already existing body, which is exactly what you want. – JNevill Apr 21 '16 at 17:55
  • @JNevill I was thinking similar – StormsEdge Apr 21 '16 at 17:59

3 Answers3

0

Ok, so I finally figured it out:

  If oInspector.IsWordMail Then
            With NewMail
             .Display
             SendKeys "^+{END}", True
             SendKeys "{END}", True
             SendKeys "{NUMLOCK}"
            End With
neo
  • 1
  • 1
  • 1
0

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/

cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

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
Mike R
  • 1