Bre*_*ugh 5 pdf microsoft-office microsoft-excel
我目前正在尝试将 Excel 文档保存为 PDF,但同样的过程可以应用于 Word 文档。我想将特定范围的页面(例如第 1、3-5 和 8 页)保存到 PDF 文件中。
当我尝试将文件另存为 PDF 时,当我选择 PDF 作为文件类型并单击“选项...”按钮时,“页面范围”下只有两个选项。我可以选择All, 或Page(s),这使我只能选择一个范围(例如 1-10,仅此而已)。
有什么方法可以保存通用页面范围,类似于打印文档时,这样我就可以输入以逗号分隔的页面列表(如1, 3-5, 8)以包含在 PDF 中?
虽然我并不羞于使用 VBA,但我想避免使用PDF 打印机,因为我无法在我的工作计算机上安装驱动程序(没有管理权限)。我还想尽量保持页码一致(如果我打印第 1、4、5 和 8 页,它会在底部显示“第 1 页,共 3 页”、“第 2 页,共 3 页”等)。我知道这是可能的,因为当我将文档导出为带有多个选定单元格的 PDF 时会发生这种情况。
谢谢你。
我终于找到了解决我的问题的方法,使用一些 VBA 代码和ExportAsFixedFormat方法。基本上,该函数将使用类型和文件名导出当前范围。下面的答案虽然针对我的情况,但很容易适应任何人的情况。
经过一番深思熟虑和编码后,我想出了一个找到合适范围的解决方案 - 尽管它远非完美。我编写了以下 VBA 函数(仅适用于 Excel),它返回两个水平分页符之间的范围(您确实需要指定起始列和结束列):
' Name: Get Range
' Desc: Returns a string representing a range (e.g. "A1:Z30"), which encompasses the specified page breaks.
' Args: startHPBreak - Used to determine which starting page break to use, from 0 up to the number of pagebreaks.
' endHPBreak - Used to determine the last page break to return up to.
Function GetRange(ByVal startHPBreak As Long, ByVal endHPBreak As Long) As String
Dim startCol, endCol As String ' First, we define our starting/ending columns.
startCol = "A"
endCol = "Z"
Dim numHPBreaks As Long ' Holds the total number of horizontal page breaks.
Dim startRow, endRow As Long ' Holds the starting/ending rows of the range.
' First, we get the total number of page breaks.
numHPBreaks = ActiveSheet.HPageBreaks.Count
' Then, we check to see the passed ranges are valid (returns a blank string if they are not).
If (startHPBreak < 0) Or (startHPBreak > numHPBreaks) Then Exit Function
If (endHPBreak <= startHPBreak) Or (endHPBreak > numHPBreaks) Then Exit Function
' Next, we build our string by obtaining our starting and ending rows.
If startHPBreak = 0 Then ' If we're starting at the 0th page break...
startRow = 1 ' Then start exporting at the first row.
Else ' Else, just get the starting page break row.
startRow = ActiveSheet.HPageBreaks(startHPBreak).Location.Row
End If
' Lastly, we get the ending page break row, build the range as a string, and return it.
endRow = ActiveSheet.HPageBreaks(endHPBreak).Location.Row - 1
GetRange = startCol & startRow & ":" & endCol & endRow
End Function
Run Code Online (Sandbox Code Playgroud)
有一些注意事项,主要是您需要定义水平分页符才能使其工作。就我而言,这个功能有效,但每个页面上都有图表。每当我尝试在单个页面上使用范围时,后续页面上的图形都会由于某种原因全部被切断,因此我编写了以下函数将每个页面添加为字符串中的不同范围:
最后,创建的范围通过以下函数传递给ExportAsFixedFormat我的答案顶部详细介绍的函数(我只想导出第 1-2 和 12-17 页):
' Name: Export Pages
' Desc: Exports the specified pages/range in the function to the passed filename as a PDF.
' Args: outFileName - Full or relative file name (include the extension) to export the file as.
Sub ExportPages(outFileName As String)
Dim outputRange As String ' Used to build the output range.
Dim currPage As Byte ' Holds the current page we want to export.
' There are no graphs on the first two pages, so we can get that range all at once.
outputRange = GetRange(0, 2)
' Next, we loop through and add pages 12 to 17 to the range (since they have graphs).
For currPage = 12 To 17
' We build the range one page at a time, and seperate the ranges with commas.
outputRange = outputRange & "," & GetRange(currPage - 1, currPage)
Next currPage
' Finally, we use the outputRange string to specify the range, and export it as a PDF.
ActiveSheet.Range(outputRange).ExportAsFixedFormat _
Type := xlTypePDF, _
Filename := outFileName, _
Quality := xlQualityStandard, _
IncludeDocProperties := True, _
IgnorePrintAreas := False, _
OpenAfterPublish := True
End Sub
Run Code Online (Sandbox Code Playgroud)
请注意,我必须一次添加第 12 页到第 17 页的范围,否则(正如我提到的)我的图表将被截断。我不知道为什么我必须这样做,但它最终成功了。希望我在这里发布的内容足以让人们走上正确的道路,使用 VBA 代码将文档导出为 PDF 文件。
如果将来有人能提出更好的解决方案,请将其作为答案发布,我们将予以考虑。
| 归档时间: |
|
| 查看次数: |
4970 次 |
| 最近记录: |