在Outlook中自动生成电子邮件,并使用VBS附加当前打开的Word文档

时间:2011-01-12 21:11:14

标签: vbscript outlook ms-word

我想编写一个VBS宏来自动生成outlook中的电子邮件并附加word文档。我目前有一个宏为excel执行此操作,但我不能让它为Word工作。我不知道我的生活是什么,我的“FName =”应该是什么。任何建议或帮助将不胜感激。这就是我所拥有的:

Sub AutoEmail()
    On Error GoTo Cancel

    Dim Resp As Integer
    Resp = MsgBox(prompt:=vbCr & "Yes = Review Email" & vbCr & "No = Immediately Send" & vbCr & "Cancel = Cancel" & vbCr, _
    Title:="Review email before sending?", _
    Buttons:=3 + 32)

    Select Case Resp

        'Yes was clicked, user wants to review email first
        Case Is = 6
            Dim myOutlook As Object
            Dim myMailItem As Object

            Set otlApp = CreateObject("Outlook.Application")
            Set otlNewMail = otlApp.CreateItem(olMailItem)
            FName = ActiveWord & "\" & ActiveWord.Name

            With otlNewMail
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = "Good Morning," & vbCr & vbCr & "" & Format(Date, "MM/DD") & "."
            .Attachments.Add FName
            .Display

            End With


            Set otlNewMail = Nothing
            Set otlApp = Nothing
            Set otlAttach = Nothing
            Set otlMess = Nothing
            Set otlNSpace = Nothing


        'If no is clicked
        Case Is = 7
            Dim myOutlok As Object
            Dim myMailItm As Object

            Set otlApp = CreateObject("Outlook.Application")
            Set otlNewMail = otlApp.CreateItem(olMailItem)
            FName = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name

            With otlNewMail
            .To = ""
            .CC = ""
            .Subject = ""
            .Body = "Good Morning," & vbCr & vbCr & " " & Format(Date, "MM/DD") & "."
            .Attachments.Add FName
            .Send
            '.Display
            'Application.Wait (Now + TimeValue("0:00:01"))
            'Application.SendKeys "%s"

            End With

            'otlApp.Quit

            Set otlNewMail = Nothing
            Set otlApp = Nothing
            Set otlAttach = Nothing
            Set otlMess = Nothing
            Set otlNSpace = Nothing


        'If Cancel is clicked
        Case Is = 2
        Cancel:
            MsgBox prompt:="No Email has been sent.", _
            Title:="EMAIL CANCELLED", _
            Buttons:=64

    End Select

End Sub

1 个答案:

答案 0 :(得分:1)

可能有点晚了,但我想解决它以备将来使用。 您希望将活动文档作为文件名(FName)。

FName = Application.ActiveDocument.Path + "\" + Application.ActiveDocument.Name
' .Path returns only the Path where the file is saved without the file name like "C:\Test"
' .Name returns only the Name of the file, including the current type like "example.doc"
' Backslash is needed because of the missing backslash from .Path

otlNewMail.Attachements.Add FName

您可能还希望在通过outlook发送之前保存当前文档,否则您将发送文档而不进行更改。

Function SaveDoc()
    ActiveDocument.Save
End Function

我希望这会对其他人有所帮助,因为在编写类似脚本时,问题中的代码对我有很大的帮助。