使用将 Excel 另存为 PDF 的 VBScript 如何删除生成的 PDF 顶部和底部的空白 space?
Using VBScript which saves Excel as PDF how can I remove blank space at top and bottom in resulting PDF?
我正在创建一个 VBScript,它将 运行 循环并获取指定文件夹中的所有 XLSX 文件,然后将它们另存为 PDF。那部分我开始工作了。我遇到的问题是生成的 PDF 文件在顶部和底部有 blank/white space。我只是用一个 2 列 10 行的小 XLSX 文件进行测试。
问题:
如何消除由 VBScript 保存的 PDF 文件顶部和底部的空白 space?
'Set Objects and Variables
Set xlObj = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("\myserver\mypath\vbstest")
'Run a loop
For Each file In f.Files
'IF statement to look for XLSX files only
If LCase(fso.GetExtensionName(file.Name)) = "xlsx" Then
set xlWB = xlObj.Workbooks.Open(file)
thisFileName =Left(xlWB.FullName , InStrRev(xlWB.FullName , ".") - 1)
xlWB.Sheets.Select
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 0,,,0
xlWB.close False
counter = counter + 1
'WScript.Echo "File " & counter & " of " & f.Files.count & " Done"
End IF
'Next loop
Next
'Quit Excel
xlObj.Quit
Set xlObj = Nothing
Set xlWB = Nothing
注意:我还使用以下 True/False (1/0) 开关尝试了 ExportAsFixedFormat,但没有任何区别:
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 1, 1,,0
我用作测试的 XLSX 文件如下所示:
一旦 VBScript 运行s 并创建一个 PDF,结果如下所示,结果的顶部和底部有大空白 space:
您是否看过以下内容?
https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
我会尝试更新以下内容
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 0,,,0
像这样
xlWB.ActiveSheet.ExportAsFixedFormat xlTypePDF, thisFileName & ".pdf", 0, 1, True, True
将 IncludeDocProperties 和 IgnorePrintAreas 设置为 True
经过进一步搜索,我找到了自己问题的答案。
通过添加一个部分来调整我的脚本以通过 PageSetup
更改页边距
'Set Page Margins to eliminate blank/white spaces
With xlWB.ActiveSheet.PageSetup
.TopMargin = 0.25
.LeftMargin = 0.25
.RightMargin = 0.25
.BottomMargin = 0.25
End With
这是添加该部分后的完整脚本
On Error Resume Next
'Set Objects and Variables
Set xlObj = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("\myserver\mypath\vbstest")
'Run a loop
For Each file In f.Files
'IF statement to look for XLSX files only
If LCase(fso.GetExtensionName(file.Name)) = "xlsx" Then
set xlWB = xlObj.Workbooks.Open(file)
thisFileName =Left(xlWB.FullName , InStrRev(xlWB.FullName , ".") - 1)
xlWB.Sheets.Select
'Set Page Margins to eliminate blank/white spaces
With xlWB.ActiveSheet.PageSetup
.TopMargin = 0.25
.LeftMargin = 0.25
.RightMargin = 0.25
.BottomMargin = 0.25
End With
'Save as PDF
'ExportASFixedFormat options info here: https://powerspreadsheets.com/save-excel-file-pdf-vba/
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 0,,,0
xlWB.close False
counter = counter + 1
'For Testing Only - Echo
'WScript.Echo "File " & counter & " of " & f.Files.count & " Done"
End IF
'Next loop
Next
'Quit Excel
xlObj.Quit
Set xlObj = Nothing
Set xlWB = Nothing
'Error Checking and any steps associated with post error
'Quit Excel
xlObj.Quit
Set xlObj = Nothing
Set xlWB = Nothing
最终结果是一个如下所示的 PDF。顶部和左侧多余的 space 现在不见了。
右侧和底部的 Space 只是第 1 页的空白部分,因为 Excel 中没有以
开头的内容
我正在创建一个 VBScript,它将 运行 循环并获取指定文件夹中的所有 XLSX 文件,然后将它们另存为 PDF。那部分我开始工作了。我遇到的问题是生成的 PDF 文件在顶部和底部有 blank/white space。我只是用一个 2 列 10 行的小 XLSX 文件进行测试。
问题: 如何消除由 VBScript 保存的 PDF 文件顶部和底部的空白 space?
'Set Objects and Variables
Set xlObj = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("\myserver\mypath\vbstest")
'Run a loop
For Each file In f.Files
'IF statement to look for XLSX files only
If LCase(fso.GetExtensionName(file.Name)) = "xlsx" Then
set xlWB = xlObj.Workbooks.Open(file)
thisFileName =Left(xlWB.FullName , InStrRev(xlWB.FullName , ".") - 1)
xlWB.Sheets.Select
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 0,,,0
xlWB.close False
counter = counter + 1
'WScript.Echo "File " & counter & " of " & f.Files.count & " Done"
End IF
'Next loop
Next
'Quit Excel
xlObj.Quit
Set xlObj = Nothing
Set xlWB = Nothing
注意:我还使用以下 True/False (1/0) 开关尝试了 ExportAsFixedFormat,但没有任何区别:
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 1, 1,,0
我用作测试的 XLSX 文件如下所示:
一旦 VBScript 运行s 并创建一个 PDF,结果如下所示,结果的顶部和底部有大空白 space:
您是否看过以下内容?
https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
我会尝试更新以下内容
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 0,,,0
像这样
xlWB.ActiveSheet.ExportAsFixedFormat xlTypePDF, thisFileName & ".pdf", 0, 1, True, True
将 IncludeDocProperties 和 IgnorePrintAreas 设置为 True
经过进一步搜索,我找到了自己问题的答案。 通过添加一个部分来调整我的脚本以通过 PageSetup
更改页边距 'Set Page Margins to eliminate blank/white spaces
With xlWB.ActiveSheet.PageSetup
.TopMargin = 0.25
.LeftMargin = 0.25
.RightMargin = 0.25
.BottomMargin = 0.25
End With
这是添加该部分后的完整脚本
On Error Resume Next
'Set Objects and Variables
Set xlObj = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("\myserver\mypath\vbstest")
'Run a loop
For Each file In f.Files
'IF statement to look for XLSX files only
If LCase(fso.GetExtensionName(file.Name)) = "xlsx" Then
set xlWB = xlObj.Workbooks.Open(file)
thisFileName =Left(xlWB.FullName , InStrRev(xlWB.FullName , ".") - 1)
xlWB.Sheets.Select
'Set Page Margins to eliminate blank/white spaces
With xlWB.ActiveSheet.PageSetup
.TopMargin = 0.25
.LeftMargin = 0.25
.RightMargin = 0.25
.BottomMargin = 0.25
End With
'Save as PDF
'ExportASFixedFormat options info here: https://powerspreadsheets.com/save-excel-file-pdf-vba/
xlWB.ActiveSheet.ExportAsFixedFormat 0, thisFileName & ".pdf", 0, 1, 0,,,0
xlWB.close False
counter = counter + 1
'For Testing Only - Echo
'WScript.Echo "File " & counter & " of " & f.Files.count & " Done"
End IF
'Next loop
Next
'Quit Excel
xlObj.Quit
Set xlObj = Nothing
Set xlWB = Nothing
'Error Checking and any steps associated with post error
'Quit Excel
xlObj.Quit
Set xlObj = Nothing
Set xlWB = Nothing
最终结果是一个如下所示的 PDF。顶部和左侧多余的 space 现在不见了。 右侧和底部的 Space 只是第 1 页的空白部分,因为 Excel 中没有以
开头的内容