且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用VBA代码将文本文件intp pdf文件转换为文件夹

更新时间:2023-02-12 21:54:29

如果要保存PDF,则需要使用适当的保存格式 - 这不是wdFormatXMLDocument!尝试:

If you want to save a PDF, then you need to use the appropriate save format - which isn't wdFormatXMLDocument! Try:

Sub ConvertToPDF()
Dim file As Variant, wdDoc As Document
file = Dir("C:\Text" & "*.txt") 'txt path
Do While (file <> "")
  Set wdDoc = Documents.Open(FileName:="C:\Text" & file, ReadOnly:=True, _
    AddToRecentFiles:=False, Format:=wdOpenFormatAuto, Visible:=False)
  wdDoc.SaveAs2 FileName:="C:\Text2" & Replace(file, ".txt", ".pdf"), _
    FileFormat:=wdFormatPDF, AddToRecentFiles:=False
  wdDoc.Close False
  file = Dir
Loop
End Sub