我想編寫一些VBA代碼,它將自動從RTF文檔創建一封新郵件。我使用下列程序:1。 的Microsoft Word 2013 2.微軟的Outlook 2013如何從Microsoft Word中粘貼到Outlook
我已經成功地做到我想要的一切,除了如何,我複製到電子郵件的正文內容粘貼。
我在網上搜索瞭如何做到這一點,但我還沒有找到任何簡單的方法來做到這一點。另外,我發現的所有例子都與Microsoft Excel有關。我注意到使用Microsoft Word時存在差異。
下面是我寫的代碼:
Sub SendDocAsMail()
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim TheUser As String
Dim Subject As String
Dim ClientRef As String
Dim Body As String
Dim Signature As String
Dim SigString As String
Dim i As Integer
Dim Pos As Integer
Dim myAttachments As Outlook.Attachments
TheUser = Environ("UserName")
On Error Resume Next
'Start Outlook if it isn't running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
End If
'Create a new message
Set oItem = oOutlookApp.CreateItem(olMailItem)
'Copy the open document to subject and body
'Change only Mysig.htm to the name of your signature
SigString = Environ("appdata") & _
"\Microsoft\Signatures\" & TheUser & ".htm"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Subject = Selection.Text
Subject = Left(Subject, Len(Subject) - 1)
ClientRef = Subject
ClientRef = Right(ClientRef, Len(ClientRef) - 1)
For i = 1 To Len(ClientRef)
If Mid(ClientRef, i, 1) = "|" Then
Pos = i
End If
Next i
ClientRef = Left(ClientRef, Pos - 1)
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeParagraph
Selection.InsertFile (SigString)
Selection.WholeStory
Selection.Copy
oItem.To = "[email protected]; [email protected]"
oItem.BCC = "[email protected]"
oItem.Subject = Subject
'oItem.Body = 'NEED HELP
'Selection.PasteAndFormat (wdFormatOriginalFormatting)
oItem.Display
Set myAttachments = oItem.Attachments
'myAttachments.Add.PathName = "C:\Users\" & TheUser & "\Dropbox\PATENT\Bressler\" & ClientRef & "\"
'Clean up
' Word.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Word.Application.Quit SaveChanges:=wdDoNotSaveChanges
End Sub
在粘貼與原始格式的複製文本的所有幫助,將不勝感激。
非常感謝你給我如此快速的迴應。我一直在解決這個問題三天了。 你可能會給我一個快速解釋爲什麼這個工作,以便我可以瞭解代碼背後的原因。 另外,在我看來,你會是正確的人問以下問題: 我想宏打開附件對話到一個特定的文件夾。我找不到符合法案的任何方法。什麼是我實施上述的最佳方式? –
請參閱上述回答的修訂:) –
非常感謝您的幫助! 出於好奇,爲什麼文件對話框工作,而不是從附件對象使用的東西?我期望找到的第一個方法是在Attachments對象中。在我編寫錯誤的方式? –