使用“单词邮件”时,您没有使用Outlook。这里描述了如何从Word调用Outlook。一旦Outlook打开,您可以使用Outlook VBA。
http://www.howto-outlook.com/howto/senddocasmail.htm
未经测试,你将需要删除你不需要的部分。
Sub SendDocAsMail()
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
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
On Error GoTo 0 ' <=== Important to see errors now if there are any
'Create a new message
Set oItem = oOutlookApp.CreateItem(olMailItem)
' --------------------------
'Set oItem = oOutlookApp.ActiveInspector.CurrentItem
If oItem.SenderName = donations Then
oItem.Categories = "donations"
ElseIf oItem.SenderName = "Donations" Then
oItem.Categories = "donations"
End If
' --------------------------
'Allow the user to write a short intro and put it at the top of the body
Dim msgIntro As String
msgIntro = InputBox("Write a short intro to put above your default " & _
"signature and current document." & vbCrLf & vbCrLf & _
"Press Cancel to create the mail without intro and " & _
"signature.", "Intro")
'Copy the open document
Selection.WholeStory
Selection.Copy
Selection.End = True
'Set the WordEditor
Dim objInsp As Outlook.Inspector
Dim wdEditor As Word.Document
Set objInsp = oItem.GetInspector
Set wdEditor = objInsp.WordEditor
'Write the intro if specified
Dim i As Integer
If msgIntro = IsNothing Then
i = 1
'Comment the next line to leave your default signature below the document
wdEditor.Content.Delete
Else
'Write the intro above the signature
wdEditor.Characters(1).InsertBefore (msgIntro)
i = wdEditor.Characters.Count
wdEditor.Characters(i).InlineShapes.AddHorizontalLineStandard
wdEditor.Characters(i + 1).InsertParagraph
i = i + 2
End If
'Place the current document under the intro and signature
wdEditor.Characters(i).PasteAndFormat (wdFormatOriginalFormatting)
'Display the message
oItem.Display
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
Set objInsp = Nothing
Set wdEditor = Nothing
End Sub
编辑:添加,根据评论。这是初学者旅行的一步。
“由于此宏也使用Outlook功能来创建邮件,因此我们必须添加对项目的引用。为此,请选择Tools-> References ...并选择Microsoft Outlook 12.0 Object Library(或使用Outlook 2010时为14.0)。之后按OK。“
我发现在word中我应该在普通模块下创建它,以便它可以应用于任何word文档。但是我仍然难以使代码的工作方式与Word兼容。请帮助!!!! 1 – 2015-02-09 22:03:18
请修改您的问题以包含清晰简明的问题陈述。这个代码错误吗?如果是这样,在什么位置以及错误信息是什么?另外,期望的结果是什么?你想达到什么目的?你基本上只给了一段代码,并说“这不起作用”。但是如果你没有描述你需要什么,或者具体的失败是什么,那么人怎么能帮助你? – 2015-02-09 22:51:54
上面的代码片段是工作代码,作为示例提供。我认为它necs – 2015-02-10 01:09:09