![](/img/trans.png)
[英]Excel VBA Search for multiple file formats (PDF, DOC, DOCX, XLS)
[英]How can I save .doc or .docx as PDF with VBA overwitring?
我有一個代碼,我在 Access 中使用將 all.docx 保存在一個文件夾中,為.pdf。 問題是:當已經存在file.docx as.pdf時,代碼失敗,我不知道為什么它沒有將.docx另存為.pdf覆蓋Z437175BA4191210EE009ZE1D 我怎樣才能用我的實際代碼做到這一點?
我的實際代碼是:
Private Sub Generate_PDFs_Click()
Dim directory As String
Dim fldr As Object
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select folder with Word files to export to PDF"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
directory = .SelectedItems(1)
End With
Dim fso, newFile, Folder, files, folders
Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder(directory)
Set files = Folder.files
For Each file In files
If file.path Like "*.docx*" Then
newName = Replace(file.path, ".docx", ".pdf")
newName = Replace(newName, ".doc", ".pdf")
'Debug.Print file.Path
Documents.Open FileName:=file.path, _
ConfirmConversions:=False, _
ReadOnly:=False, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Format:= _
wdOpenFormatAuto, _
XMLTransform:=""
ActiveDocument.ExportAsFixedFormat OutputFileName:=newName, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:= _
wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
From:=1, To:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
ActiveDocument.Close
End If
If file.path Like "*.dwg*" Then
MsgBox "Isso ainda não foi feito"
End If
Next
End Sub
在運行保存之前,輸入以下行,這樣 Access 將不會提示您,如果文件已經存在,則覆蓋您的文件, Application.DisplayAlerts = False
然后在保存后重新打開 DisplayAlerts, Application.DisplayAlerts = True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.