【问题标题】:Issues with conversion of .mht to .pdf files and clearing memory将 .mht 转换为 .pdf 文件和清除内存的问题
【发布时间】:2020-06-09 02:18:21
【问题描述】:

我目前正在使用 VBA 循环浏览一些 .msg Outlook 文件,并通过中间步骤 .mht 转换将它们转换为 .pdf。代码将粘贴在下面。目前,我在大约 40% 的时间内得到了正确的转换。正确的转换意味着有问题的文件夹在运行此宏后将具有正确的 .pdf 和 .mht 版本的 .msg 文件。

但是,在另外 60% 的时间里,从 .mht 到 .pdf 的转换会卡在 Word 中。一切都挂起,我必须结束 Word.exe 进程。那是问题#1。也许与这个问题有关,每当这个 VBA 成功地将文件从 .msg 转换为 .mht 到 .pdf 时,我也会留下我已转换的先前 .mht 文件的幽灵,即使它们不在文件夹。这些文件的打字机字体信息非常有限,其文件名以“~$ NAME CUTOFF HERE”开头。我曾尝试在运行我的代码以尝试释放内存之前和之后将我的对象设置为 Nothing,但它仍然被赶上。下面的照片也可以帮助解释。有人对发生的事情有任何想法吗?

从 .msg 到 .mht 的转换每次都很有效。

请注意,我只发布相关代码。此 ElseIf 语句之上的所有其他内容都将各种其他文件类型转换为 .pdf(即 .doc*、.xls* 和 .ppt*)。

我以前使用过这个的变体,但想要一个。加快进程和 b.不必每次都点击“保存”。 link here.

Image of Ghost Files - Most recent run of code below with that one .msg file

'Above this is irrelevant other If statement
      ElseIf (oFile) Like ("*.msg") Then

                Dim newName4 As String
                newName4 = Replace(oFile.path, ".msg", ".pdf")
                newName4 = Replace(newName4, ".msg", ".pdf")
                Dim strHTML As String

                Dim objOL As Object
                Dim Msg As Object
                Dim Dms2 As Object
                Set objOL = CreateObject("Outlook.Application")
                Set Msg = objOL.Session.OpenSharedItem(oFile.path)

                strHTML = Left(oFile.path, InStrRev(oFile.path, Chr(46))) & "mht"

                With Msg
                    .SaveAs strHTML, olMHTML
                    .Close olDiscard
                End With

            ElseIf (oFile) Like ("*.mht") Then

                Dim newName5 As String
                newName5 = Replace(oFile.path, ".mht", ".pdf")
                newName5 = Replace(newName5, ".mht", ".pdf")

                Dim wrdApp As Word.Application
                Dim wrdDoc As Word.Document

                Set wrdDoc = Nothing
                Set wrdApp = Nothing

                Set wrdApp = CreateObject("Word.Application")
                Dim x As Integer
                x = 1
                wrdApp.Documents.Open Filename:=oFile.path, Visible:=False
                'Set wrdDoc = wrdApp.Documents.Item(x)

                wrdApp.Documents.Item(x).ExportAsFixedFormat OutputFileName:=newName5 _
                        , ExportFormat:= _
                        wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
                        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=0, To:=0, _
                        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
                        BitmapMissingFonts:=True, UseISO19005_1:=False

                wrdApp.Documents.Item(x).Close
                wrdApp.Quit

                Set wrdDoc = Nothing
                Set wrdApp = Nothing

            End If

        Next oFile

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    回答了我自己的问题!

                ElseIf (oFile) Like ("*.msg") Then
    
                    Dim newName4 As String
                    newName4 = Replace(oFile.path, ".msg", ".pdf")
                    newName4 = Replace(newName4, ".msg", ".pdf")
                    Dim strHTML As String
    
                    Dim objOL As Object
                    Dim Msg As Object
                    Dim Dms2 As Object
                    Set objOL = CreateObject("Outlook.Application")
                    Set Msg = objOL.Session.OpenSharedItem(oFile.path)
    
                    strHTML = Left(oFile.path, InStrRev(oFile.path, Chr(46))) & "mht"
    
                    With Msg
                        .SaveAs strHTML, olMHTML
                        .Close olDiscard
                    End With
    
                    On Error GoTo WordAppQuit
                    Dim newName5 As String
                    newName5 = Replace(strHTML, ".mht", ".pdf")
                    newName5 = Replace(newName5, ".mht", ".pdf")
                    Dim wrdApp As Word.Application
                    Dim wrdDoc As Word.Document
                    Set wrdDoc = Nothing
                    Set wrdApp = Nothing
    
                    Set wrdApp = CreateObject("Word.Application")
                    Dim x As Integer
                    x = 1
                    wrdApp.Documents.Open Filename:=(strHTML)
                    'Set wrdDoc = wrdApp.Documents.Item(x)
    
                    wrdApp.Documents.Item(x).ExportAsFixedFormat OutputFileName:=newName5 _
                            , ExportFormat:= _
                            wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
                            wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=0, To:=0, _
                            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
                            BitmapMissingFonts:=True, UseISO19005_1:=False
    WordAppQuit:
                    wrdApp.Documents.Item(x).Close
                    wrdApp.Quit
    
                    Set wrdDoc = Nothing
                    Set wrdApp = Nothing
    
                    newName5 = ""
    

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2023-04-04
      • 2018-03-28
      • 2020-04-12
      相关资源
      最近更新 更多