excel 파일을 pdf로 대량 변환하는 VBA 스크립트
5812 단어 Excel
Function RDB_Create_PDF(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim Fname As Variant
'Test to see if the Microsoft Create/Send add-in is installed.
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the PDF file.
FileFormatstr = "PDF Files (*.pdf), *.pdf"
Fname = Application.GetSaveAsFilename("", filefilter:=FileFormatstr, _
Title:="Create PDF")
'If you cancel this dialog, exit the function.
If Fname = False Then Exit Function
Else
Fname = FixedFilePathName
End If
'If OverwriteIfFileExist = False then test to see if the PDF
'already exists in the folder and exit the function if it does.
If OverwriteIfFileExist = False Then
If Dir(Fname) <> "" Then Exit Function
End If
'Now export the PDF file.
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
On Error GoTo 0
'If the export is successful, return the file name.
If Dir(Fname) <> "" Then RDB_Create_PDF = Fname
End If
End Function
Function DigIn(sPath As String)
Dim FS, f, f1, fc, s
Set FS = CreateObject("Scripting.FileSystemObject")
Set f = FS.GetFolder(sPath)
Set fc = f.Files
For Each f1 In fc
ExtName = GetExtName(f1.Path)
If ExtName = "xlsx" Then
RDB_Workbook_To_PDF (f1.Path)
End If
Next
For Each subfolder In f.SubFolders
s = s & subfolder.Path
DigIn (subfolder.Path)
Next
End Function
Function GetExtName(ScanString As String) As String
'*******************************************************
'<DESC> Retrieves File Extension Name from full
' directory path</DESC>
'<RETURN> File Extension Only
' </RETURN>
'<ACCESS> Public
'<ARGS> FullPath:
' Full Filepath incl. Filename
' </ARGS>
'<USAGE> If GetExtName("c:\autoexec.bat")
' </USAGE>
'*******************************************************
Dim intPos As String
Dim intPosSave As String
If InStr(ScanString, ".") = 0 Then
GetExtName = ""
Exit Function
End If
intPos = 1
Do
intPos = InStr(intPos, ScanString, ".")
If intPos = 0 Then
Exit Do
Else
intPos = intPos + 1
intPosSave = intPos - 1
End If
Loop
GetExtName = Trim$(Mid$(ScanString, intPosSave + 1))
End Function
Sub RDB_Convert_Files_To_PDF()
Dim sStartPath As String
Dim sWhat As String
sStartPath = "D:/workspace/clothes-report/data/fankui/output" 'Where?
sWhat = "test.log" 'What?
result = DigIn(sStartPath) 'First step
End Sub
Sub RDB_Workbook_To_PDF(fPath As String)
Dim FileName As String
'Call the function with the correct arguments.
Workbooks.Open fPath
FileName = RDB_Create_PDF(ActiveWorkbook, Replace(fPath, ".xlsx", "") & ".pdf", True, True)
ActiveWorkbook.Close SaveChanges:=False
'For a fixed file name and to overwrite the file each time you run the macro, use the following statement.
'RDB_Create_PDF(ActiveWorkbook, "C:\Users\Ron\Test\YourPdfFile.pdf", True, True)
If FileName <> "" Then
'Uncomment the following statement if you want to send the PDF by mail.
'RDB_Mail_PDF_Outlook FileName, "[email protected]", "This is the subject", _
"See the attached PDF file with the last figures" _
& vbNewLine & vbNewLine & "Regards Ron de bruin", False
Else
MsgBox "It is not possible to create the PDF; possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to save the file in arg 2 is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exists."
End If
End Sub
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.