[英]how to check file type and opening/ saving files in a folder VBA
The following code loops through All files in a specified folder, formats each file, and saves it as a PDF in the same folder.以下代码循环遍历指定文件夹中的所有文件,格式化每个文件,并将其保存为同一文件夹中的 PDF。
The code runs fine but there are 2 issues:代码运行良好,但有两个问题:
1)if there are any files in the folder that are already pdf,s it will open in and mess it up. 1)如果文件夹中有任何文件已经是 pdf,它将打开并搞砸。 How can I make this so it only opens the excel files in the folder and not PDF files?
我怎样才能做到这一点,所以它只打开文件夹中的 excel 文件而不是 PDF 文件?
2)if I run it twice it works but it just saves over files if the filename already exists. 2)如果我运行它两次它可以工作,但如果文件名已经存在,它只会保存文件。 How can I make I so if when it saves it and the file name already exists it saves it as a new file like filename-b, filename-c filename-d, filename-f ect?
如果当它保存它并且文件名已经存在时,我怎么能将它保存为一个新文件,如文件名-b、文件名-c、文件名-d、文件名-fect?
Sub File_Loop_Example()
Dim MyFolder As String, MyFile As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
MyFolder = .SelectedItems(1)
Err.clear
End With
MyFile = Dir(MyFolder & "\", vbReadOnly)
Do While MyFile <> ""
DoEvents
On Error GoTo 0
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
Application.Run "PERSONAL.XLSB!TTDA"
ChDir MyFolder
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
MyFolder & "\" & MyFile, Quality:= _
xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
0
Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir
Loop
End Sub
Try replacing of尝试更换
MyFile = Dir(MyFolder & "\", vbReadOnly)
with和
MyFile = Dir(MyFolder & "\" & "*.xlsx")
In this way, the code will open only .xlsx
files.这样,代码将只打开
.xlsx
文件。
Or you can let your code like it is, but filter the files to be open using the next function which retrieves the file extension:或者您可以让您的代码保持原样,但使用检索文件扩展名的下一个 function 过滤要打开的文件:
Private Function GetExt(fileName As String) As String
GetExt = Split(fileName, ".")(UBound(Split(fileName, ".")))
End Function
The function can be called just before the workbook opening. function 可以在工作簿打开之前调用。 Your loop will become something like this:
你的循环会变成这样:
Do While MyFile <> ""
DoEvents
If GetExt(MyFile) = "xlsx" or GetExt(MyFile) = "xlsm" then
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
Application.Run "PERSONAL.XLSB!TTDA"
ChDir MyFolder
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
MyFolder & "\" & MyFile, Quality:= _
xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False
OpenAfterPublish:=True
Workbooks(MyFile).Close SaveChanges:=False
End if
MyFile = Dir
Loop
For the next issue, use please:对于下一个问题,请使用:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(MyFolder & "\" & MyFile & ".pdf") Then
If fso.FileExists(MyFolder & "\" & MyFile & "_b.pdf") Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
MyFolder & "\" & MyFile & "_a", Quality:= _
xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
MyFolder & "\" & MyFile & "_b", Quality:= _
xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False
End If
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
MyFolder & "\" & MyFile, Quality:= _
xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.