Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vba - Paste clipboard in outlook email in normal order

I have 5 userforms for an email. The workflow goes like this:

create new email

userform1.show
user selects the fields
automatic printscreen is inserted in the text

userform2.show
user selects the fields
automatic printscreen is inserted in the text

userform3.show
user selects the fields
automatic printscreen is inserted in the text

userform4.show
user selects the fields
automatic printscreen is inserted in the text

userform5.show
user selects the fields
automatic printscreen is inserted in the text

My problem is that in the end, the email will look like this:

userform1 selected fields
userform2 selected fields
userform3 selected fields
userform4 selected fields
userform5 selected fields

print screen 5
print screen 4
print screen 3
print screen 2
print screen 1

Is there a way to make the print screens appear in the correct order?

Here is the code that copies the clipboard for the first userform ( the print screen is from another application )

Dim olInsp As Object
Dim oRng As Object
Dim wdDoc As Object

With objItem

         Set olInsp = .GetInspector
         Set wdDoc = olInsp.WordEditor
         Set oRng = wdDoc.Range
         oRng.collapse 1
         objItem.Display
         objItem.Visible = True
         objItem.HtmlBody = "<br><br>" & objItem.HtmlBody

         On Error Resume Next
         oRng.Paste

         objItem.HtmlBody = "<br>" & objItem.HtmlBody

         Dim myOutlook As Object
         Set myOutlook = GetObject(, "Outlook.Application")
         myOutlook.ActiveExplorer.Activate

End With

I made the cursor to move to the end of the mail but the paste doesn't work at all

Dim objCurrentMail As Outlook.MailItem
Dim objWordDocument As Word.Document
Dim objWordRange As Word.Range
Dim VarPosition As Variant

    'Only work if the current email is using word editor
    Set objCurrentMail = Outlook.Application.ActiveInspector.CurrentItem
    Set objWordDocument = objCurrentMail.GetInspector.WordEditor


       VarPosition = objWordDocument.Range.End - 1000
       Set objWordRange = objWordDocument.Range(VarPosition, VarPosition)
       objWordRange.Select

    keybd_event VK_DOWN, 0, 0, 0
    keybd_event VK_DOWN, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_CONTROL, 0, 0, 0
    keybd_event VK_V, 0, 0, 0
    keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_V, 0, KEYEVENTF_KEYUP, 0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is code here to move the cursor to the end http://www.vboffice.net/en/developers/determine-cursor-position/

Public Sub SetCursor()
    Dim Ins As Outlook.Inspector
    Dim Doc As Word.Document
    Dim range As Word.range
    Dim pos As Long

    Set Ins = Application.ActiveInspector
    Set Doc = Ins.WordEditor
    If Not Doc Is Nothing Then
        pos = Doc.range.End - 1
        Set range = Doc.range(pos, pos)
        range.Select
    End If
End Sub

Your code could look like this:

Option Explicit

Sub pasteAtEnd()

Dim olInsp As Object
Dim oRng As Object
Dim wdDoc As Object

Dim pos As Long
Dim objItem As Object

Set objItem = ActiveInspector.currentItem

With objItem

    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor
    Set oRng = wdDoc.range

    objItem.Display
    'objItem.HTMLBody = "<br><br>" & objItem.HTMLBody
    objItem.HTMLBody = objItem.HTMLBody & "<br><br>"

    pos = wdDoc.range.End - 1
    Set oRng = wdDoc.range(pos, pos)
    oRng.Select

    MsgBox "Cursor should be at end of the mail body."

    'On Error Resume Next ' Use proper error handling
    oRng.Paste

End With

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...