【问题标题】:Convert word doc to pdf and send as attachment in Outlook将 word doc 转换为 pdf 并在 Outlook 中作为附件发送
【发布时间】:2016-08-11 14:34:25
【问题描述】:

我想将我的 Word Doc 转换为 pdf,并将其作为附件作为我构建的 Outlook 电子邮件的一部分发送。

我尝试在我的 SaveAs2 行的末尾添加“.pdf”,这将文件格式更改并附加为 pdf,但是,当尝试打开文件时它不显示并给我一条消息,表明该文件作为附件发送时没有所有代码。

Private Sub emailbutton_Click()
        'No-option email sending
        Dim OL              As Object
        Dim EmailItem       As Object
        Dim Doc             As Document

        Application.ScreenUpdating = False
        Set OL = CreateObject("Outlook.Application")
        Set EmailItem = OL.CreateItem(olMailItem)
        Set Doc = ActiveDocument

        If VName.Value = "" Then
            Doc.SaveAs ("Quotation_Blank 2016")
        Else
           Doc.SaveAs2 ("QFORM" & "_" & JNumber.Value & "_" & VName.Value)

        End If

       With EmailItem
        .Display
        End With
    '        Signature = EmailItem.body


        With EmailItem
            .Subject = "QFORM" & "_" & JNumber.Value & "_" & VName.Value

            'HTMLbody
            msg = "<b><font face=""Times New Roman"" size=""4"" color=""blue"">INTEGRATED ASSEMBLY </font></b><br>" _
            & "   1200 Woodruff Rd.<br>" _
            & "   Suite A12<br>" _
            & "   Greenville, SC 29607<br><br>" _
            & "We have recently released subject project, which will contain assemblies to be outsourced. You have been selected to build these assemblies according to the attachment. <br><br>" _
            & "As part of this process, please review the quotation form attached and indicate your acceptance. If adjustments and-or corrections are required, please feel free to contact us for quick resolution. <br><br>" _
            & "<b><font face=""Times New Roman"" size=""4"" color=""Red"">NOTE: </font></b>" _
            & "The information on attached quotation form is not a contract and only an estimate of predetermined costs per hourly rate for outsource assemblies. <br><br>" _
            & "*******For your records you may wish to print out the completed quote form. <br><br>" _
            & "Thank you, <br><br>" _
            & "<b>HARTNESS INTERNATIONAL </b><br>" _
            & "H1 Production Control <br>" _
            & vbNewLine & Signature

            .HTMLBody = msg & .HTMLBody

            If VName.Value = "INTEGRATED ASSEMBLY" Then
                .To = "Email1.com;"
                .CC = "Email2.com;" & "Email3.com;"
                .Importance = olImportanceNormal 'Or olImportanceHigh Or         olImportanceLow
                .Attachments.Add Doc.FullName
                .Display
             ElseIf VName.Value = "LEWALLEN" Then
                .To = "Email1.com;"
                .CC = "Email2.com;" & "Email3.com;"
                .Importance = olImportanceNormal 'Or olImportanceHigh Or         olImportanceLow
                .Attachments.Add Doc.FullName
                .Display

             End If
        End With




        Application.ScreenUpdating = True

        Set Doc = Nothing
        Set OL = Nothing
        Set EmailItem = Nothing

    End Sub

【问题讨论】:

    标签: vba outlook ms-word


    【解决方案1】:

    使用 SaveAs2 您可以指定文件格式

    https://msdn.microsoft.com/en-us/library/office/ff836084.aspx

    expression .SaveAs2(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions, LineEnding, AddBiDiMarks, CompatibilityMode)

    https://msdn.microsoft.com/en-us/library/office/ff839952.aspx

    文件格式为 wdFormatPDF 或 17

    【讨论】:

      【解决方案2】:

      以这种方式更改您的 saveAs2。

      If VName.Value = "" Then
          Doc.SaveAs ("Quotation_Blank 2016")
      Else
          Doc.ExportAsFixedFormat OutputFileName:="QFORM" & "_" & JNumber.Value , _
          ExportFormat:=wdExportFormatPDF
      End If
      

      编辑

      使用路径和添加作为附件

      If VName.Value = "" Then
          Doc.SaveAs ("Quotation_Blank 2016")
      Else
          Path = "C:\Temp\"
          FileName = "QFORM" & "_" & JNumber.Value & "_" & VName.Value
          Doc.ExportAsFixedFormat OutputFileName:=Path & FileName, _
          ExportFormat:=wdExportFormatPDF
      End If
      

      和附件

              .Attachments.Add Path & FileName & ".pdf"
      

      【讨论】:

      • 代码看起来像我需要的,但我认为我的另一个问题是如何创建将其保存在当前文件夹中的路径,以及如何将其作为附件添加到此代码中。我目前正在使用 .Attachment.Add Doc.FullName,它仍在提取 word 文件而不是 pdf。
      • 好调用@Om3r - 我不得不通过将路径设置为常量来稍微调整它。我不断收到“编译错误 - 无法分配给只读属性”,当前小路。感谢您的帮助。
      【解决方案3】:

      如果您需要将多个 Word 文件转换为其他格式,例如 TXT、RTF、HTML 或 PDF,请运行以下脚本。

      Option Explicit On
      
      Sub ChangeDocsToTxtOrRTFOrHTML()
          'with export to PDF in Word 2007
          Dim fs As Object
          Dim oFolder As Object
          Dim tFolder As Object
          Dim oFile As Object
          Dim strDocName As String
          Dim intPos As Integer
          Dim locFolder As String
          Dim fileType As String
          On Error Resume Next
      
          locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\Users\your_path_here\")
          Select Case Application.Version
              Case Is < 12
                  Do
                      fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT"))
                  Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML")
              Case Is >= 12
                  Do
                      fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT"))
                  Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF")
          End Select
      
          Application.ScreenUpdating = False
          Set fs = CreateObject("Scripting.FileSystemObject")
          Set oFolder = fs.GetFolder(locFolder)
          Set tFolder = fs.CreateFolder(locFolder & "Converted")
          Set tFolder = fs.GetFolder(locFolder & "Converted")
      
          For Each oFile In oFolder.Files
              Dim d As Document
              Set d = Application.Documents.Open(oFile.Path)
              strDocName = ActiveDocument.Name
              intPos = InStrRev(strDocName, ".")
              strDocName = Left(strDocName, intPos - 1)
              ChangeFileOpenDirectory tFolder
              Select Case fileType
                  Case Is = "TXT"
                      strDocName = strDocName & ".txt"
                      ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText
              Case Is = "RTF"
                      strDocName = strDocName & ".rtf"
                      ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF
              Case Is = "HTML"
                      strDocName = strDocName & ".html"
                      ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML
              Case Is = "PDF"
                      strDocName = strDocName & ".pdf"
                      ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF
              End Select
              d.Close
              ChangeFileOpenDirectory oFolder
          Next oFile
          Application.ScreenUpdating = True
      
      End Sub
      

      结果保存在一个动态创建的文件夹中,并且保存在包含您刚刚转换的文档的同一文件夹中。

      【讨论】:

        【解决方案4】:

        我想知道您是否可以发布此解决方案的所有代码。我一直在寻找这样的东西,我所有的经验都在 powershell 上。我知道这我通常不赞成,但我已经没有选择了

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多