【问题标题】:Merging two Excel VBA Code (Save as PDF + Send Via Outlook)合并两个 Excel VBA 代码(另存为 PDF + 通过 Outlook 发送)
【发布时间】:2015-06-21 09:47:20
【问题描述】:

我有两个VBA 代码,一个是将打印区域保存为与工作簿同名的 PDF,保存文件位置是桌面,它工作正常 而且我确实有另一个代码可以启动 Outlook 新消息,并将某些特定的单元格值作为主题,将另一个值作为正文。

问题是我希望新邮件的代码附上代码 1 中保存的 PDF 文件,并使主题与 PDF 文件名相同。

保存pdf代码为:

Sub Save_as_pdf()
Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String

Set FSO = CreateObject("Scripting.FileSystemObject")

s(0) = "C:\Users\" & Environ("UserName") & "\Desktop\" & ThisWorkbook.Name

If FSO.FileExists(ThisWorkbook.FullName) Then
    '//Change Excel Extension to PDF extension in FilePath
    s(1) = FSO.GetExtensionName(s(0))

    If s(1) <> "" Then
        s(1) = "." & s(1)
        sNewFilePath = Replace(s(0), s(1), ".pdf")

        '//Export to PDF with new File Path
        ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=sNewFilePath, _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=False
    End If
Else
    '//Error: file path not found
    MsgBox "Error: this workbook may be unsaved.  Please save and try again."
End If

Set FSO = Nothing

End Sub

...第二个outlook新邮件代码是:

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As     String, _
ByVal nShowCmd As Long) As Long

Sub SendEMail()
Dim Email As String, Subj As String
Dim Msg As String, URL As String

Email = " "

Subj = "P.O # " & "-" & Cells(9, 5) & "-" & Cells(15, 2) & "-" & Cells(15, 8) & Cells(15, 7)

Msg = " "
Msg = "Dear Mr. " & vbCrLf & vbCrLf & "Good Day" & vbCrLf & vbCrLf & "Kindly find the attahched P.O to be delivered to " & Cells(10, 12)



'Replace spaces with %20 (hex)
Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")
Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")

'Replace carriage returns with %0D%0A (hex)
Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")

'Create the URL
URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg



'Execute the URL (start the email client)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

'Wait two seconds before sending keystrokes
'Application.Wait (Now + TimeValue("0:00:02"))
'Application.SendKeys "%s"
End Sub

我希望我能澄清我的问题。 提前致谢。

【问题讨论】:

    标签: excel vba pdf outlook


    【解决方案1】:

    你可以试试这个: 它将 PDF 导出更改为一个函数以获取文件路径并将其用作另一个函数的参数。 URL 方法不适用于附件,因此下面是 Outlook 的一些代码(已编辑以包含整个代码

    使用 Outlook 准备邮件(抱歉法国 cmets):

    Sub Send_To_Pdf()
    Dim PdfPath As String
    Dim BoDy As String
    
    BoDy = Msg = "Dear Mr. " & vbCrLf & vbCrLf & "Good Day" & vbCrLf & vbCrLf & "Kindly find the attahched P.O to be delivered to " & Cells(10, 12)
    
    
    PdfPath = Save_as_pdf
    EnvoiMail Right(PdfPath, InStr(1, StrReverse(PdfPath), "\") - 1), "recepient1@domain.com;recepient2@domain.com", , , BoDy, 1, PdfPath
    End Sub
    
    Public Function Save_as_pdf() As String
    Dim FSO As Object
    Dim s(1) As String
    Dim sNewFilePath As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    s(0) = "C:\Users\" & Environ("UserName") & "\Desktop\" & ThisWorkbook.Name
    
    If FSO.FileExists(ThisWorkbook.FullName) Then
        '//Change Excel Extension to PDF extension in FilePath
        s(1) = FSO.GetExtensionName(s(0))
    
        If s(1) <> "" Then
            s(1) = "." & s(1)
            sNewFilePath = Replace(s(0), s(1), ".pdf")
    
            '//Export to PDF with new File Path
            ActiveSheet.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=sNewFilePath, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=False
        End If
    Else
        '//Error: file path not found
        MsgBox "Error: this workbook may be unsaved.  Please save and try again."
    End If
    
    Set FSO = Nothing
    
    Save_as_pdf = sNewFilePath
    
    End Function
    
    
    Sub EnvoiMail(Subject As String, Destina As String, Optional CCdest As String, Optional CCIdest As String, Optional BoDyTxt As String, Optional NbPJ As Integer, Optional PjPaths As String)
      Dim MonOutlook As Object
      Dim MonMessage As Object
      Set MonOutlook = CreateObject("Outlook.Application")
      Set MonMessage = MonOutlook.createitem(0)
    
      Dim PJ() As String
      PJ() = Split(PjPaths, ";")
    
      With MonMessage
          .Subject = Subject      '"Je suis content"
          .To = Destina           '"marcel@machin.com;julien@chose.com"
          .cc = CCdest            '"chef@machin.com;directeur@chose.com"
          .bcc = CCIdest          '"un.copain@supermail.com;une-amie@hotmail.com"
          .BoDy = BoDyTxt
            If PjPaths <> "" And NbPJ <> 0 Then
                For i = 0 To NbPJ - 1
                    'MsgBox PJ(I)
                  .Attachments.Add PJ(i)      '"C:\Mes Documents\Zoulie Image.gif"
                Next i
            End If
          .display
          '.send                        '.Attachments.Add ActiveWorkbook.FullName
      End With                        '?plusieurs?MonMessage.Attachments.Add "D:\Prof\Janvier\Base clients.mdb"
    
      Set MonOutlook = Nothing
    End Sub
    

    【讨论】:

    • 感谢您的及时回复。但是你能建议把这些代码放在哪里吗? Outlook 中的第一个代码?一个普通的模块?和excel中的第二个代码?如果是这样,我试过了,但我可以将 excel 宏分配给一个按钮,所以我等你回复
    • 将所有内容放在excel中,如果它们位于单独的应用程序中,它们将无法通信(它们可以但更复杂)。看看我添加的代码,一开始,它创建了一个 Outlook 应用程序,以便能够直接在 Excel 中与之交互。对于您使用 URL 的方法,Excel 也应该足够了。
    • 感谢您的帮助,但在尝试代码后,它会保存为 pdf 并启动 Outlook,但仍然没有附件 :(
    • 你是否使用了我放的代码的第二部分(因为 URL mailto 不会添加附件),请使用带有 PJ 参数的sub EnvoiMail 和“/”分隔符作为附件!
    • 我认为我以错误的方式使用了代码,如果我不能浪费您的时间,请您将代码放入工作簿并将文件发送给我,以便您确保其中的所有内容都已更正放置并确保它按您的意思工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2016-01-18
    • 2013-05-27
    相关资源
    最近更新 更多