当 word 是编辑器时,让 outlook 2003 宏工作?

Make outlook 2003 macro work when word is the editor?

我拥有的是一段类似的代码,我让它与 outlook 编辑器一起工作(足够困难),我正在努力让它现在与充当 outlook 编辑器的 Word 一起工作。 (用户习惯word邮件) 我试过:把代码直接移到这个文档下的word里,也没用。按照我看到的代码:创建一个 objword objdoc 然后将其与 outlook class 类型的交易配对,但没有运气。这是一个代码示例:

Sub SetCategory()
Dim olMessage As Outlook.MailItem
Set olMessage = Application.ActiveInspector.CurrentItem
If olMessage.SenderName = donations Then
olMessage.Categories = "donations"
ElseIf olMessage.SenderName = "Donations" Then
olMessage.Categories = "donations"
End If
With olMessage
    .Send
End With
End Sub

使用 "word mail" 时您没有使用 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

编辑:根据评论添加。这是初学者容易犯的步骤。

"Since this macro also uses Outlook functionality to create the mail we must add the reference to the project. To do this choose Tools-> References… and select Microsoft Outlook 12.0 Object Library (or 14.0 when using Outlook 2010). After this press OK."

最新的 Outlook 版本默认使用 Word 作为电子邮件编辑器。无需检查编辑器类型。 WordEditor property of the Inspector class returns the Microsoft Word Document Object Model of the message being displayed. You can read more about that in the Chapter 17: Working with Item Bodies .

此外,您可能会发现 How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited 文章很有帮助。