2015-02-09 23 views
-1

我有什么,是一个类似的代码&我使它与Outlook编辑器(足够硬),我试图让它现在与Word作为Outlook编辑器。 (用户习惯用单词邮件)我尝试过:将代码直接移动到本文档下的单词中,它什么也没做。遵循我看到的代码:创建一个objword objdoc,然后将其与outlook类型的交易配对,但没有运气。以下是一个代码示例:当word是编辑器时,让Outlook 2003的宏工作吗?

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 
+0

我发现在word中我应该在普通模块下创建它,以便它可以应用于任何word文档。但是我仍然难以使代码的工作方式与Word兼容。请帮助!!!! 1 – 2015-02-09 22:03:18

+1

请修改您的问题以包含清晰简明的问题陈述。这个代码错误吗?如果是这样,在什么位置以及错误信息是什么?另外,期望的结果是什么?你想达到什么目的?你基本上只给了一段代码,并说“这不起作用”。但是如果你没有描述你需要什么,或者具体的失败是什么,那么人怎么能帮助你? – 2015-02-09 22:51:54

+0

上面的代码片段是工作代码,作为示例提供。我认为它necs – 2015-02-10 01:09:09

回答

1

使用“单词邮件”时,您没有使用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。“

+0

感谢您指引我朝着正确的方向发送“Word Word Document As”文章是一个很大的帮助。事实上,这是我之前访问过的网站,无法找到回头路。除了将oOutlookApplication定义为应用程序外,另一部分是在字设置中启用库引用。非常感谢niton。 – 2015-02-10 19:30:25

+0

它现在证明我的程序将所有电子邮件设置为类别捐赠。这是因为行oItem.Categories =捐赠,无论if语句如上所述。这对我来说几乎没有意义,因为如果oItem.SenderName不等于捐赠或捐赠,它应该跳到最后。我做错了什么? – 2015-02-10 19:33:46

+0

那是因为括号从第一行删除。 – 2015-02-10 19:40:11