【问题标题】:Autogenerate an email in an outlook and attach the currently open word document with VBS在 Outlook 中自动生成电子邮件并使用 VBS 附加当前打开的 Word 文档
【发布时间】:2011-06-08 03:07:00
【问题描述】:

我想编写一个 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

【问题讨论】:

    标签: vbscript outlook ms-word


    【解决方案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
    

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

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2012-06-12
      • 2014-02-05
      相关资源
      最近更新 更多