【发布时间】:2019-04-14 03:28:25
【问题描述】:
我在下面的链接中找到了一个 Excel vba,它将特定目录中的 excel 文件转换为 pdf。 我希望您帮助对此代码进行必要的更改,以使其将特定目录中的 Word 文档转换为 pdf。
归功于: https://www.listendata.com/2013/02/excel-macro-convert-multiple-excel.html
代码如下:
Sub ExcelToPDF2()
Dim Path As String, FilesInPath As String _
, OutputPath As String, OutputPath2 As String
Dim MyFiles() As String, Fnum As Long
Dim Buk As Workbook, BukName As String
Dim CalcMode As Long
Dim sh As Worksheet
Dim StartTime As Date, EndTime As Date
Dim LPosition As Integer
'Specify the path of a folder where all the excel files are stored
StartTime = Timer
Path = Range("G6").Text & "\"
OutputPath = Range("G8").Text & "\"
FilesInPath = Dir(Path & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set Buk = Nothing
On Error Resume Next
Set Buk = Workbooks.Open(Path & MyFiles(Fnum))
On Error GoTo 0
If Not Buk Is Nothing Then
LPosition = InStr(1, Buk.Name, ".") - 1
BukName = Left(Buk.Name, LPosition)
Buk.Activate
OutputPath2 = OutputPath & BukName & ".pdf"
On Error Resume Next
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=OutputPath2,
_
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
On Error GoTo 0
End If
Buk.Close SaveChanges:=False
Next Fnum
End If
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
EndTime = Timer
MsgBox "Task succesfully completed in " & Format(EndTime - StartTime,
"0.00") & " seconds"
End Sub
【问题讨论】:
-
欢迎来到 Stackoverflow。我建议阅读How to ask 页面。更具体地说,如果您只是要求代码而不显示您尝试过的内容以及遇到的具体问题,那么您不太可能获得太多帮助。
-
嗨@Matt,感谢您的通知,但我认为这个问题足够具体。恳请您重新评估问题并提供支持(如有)。
标签: excel vba pdf ms-word type-conversion