【问题标题】:Saving Multiple Charts in Excel into one PDF file将 Excel 中的多个图表保存到一个 PDF 文件中
【发布时间】:2014-11-23 20:02:38
【问题描述】:

我正在使用 Excel 2013。我正在尝试将一个 Excel 工作簿中的多个图表保存为 pdf 文件。图表位于包含数据的不同工作表上。所以,我需要在每张纸上选择图表并保存到一个 PDF 文件中。我想在 pdf 文件的一页上有一张图表。有没有办法做到这一点?感谢您的支持。

谢谢珍妮

【问题讨论】:

    标签: excel pdf charts vba


    【解决方案1】:

    选择带有图表的工作表并保存为 PDF。

    【讨论】:

      【解决方案2】:

      经过反思的 VBA 解决方案看起来有点麻烦,但它是可定制的?将代码放在标准代码模块中,并将 outputPath 替换为您的。方法是在单独的工作表上组合布局,然后将工作表导出为 .pdf。对于此示例,请确保您的工作簿中有一个名为“撰写”的工作表(可能添加一些代码来执行此操作)。

       Option Explicit
      
      Sub chartsTopdf()
      Dim outSheet As Worksheet, sht As Worksheet
      Dim RngToCover As Range
      Dim chtObj As ChartObject
      Dim outputPath As String, fileStem As String
      Dim chHeight As Long, chWidth As Long
      Dim topM As Integer, botM As Integer, rightM As Integer
      Dim n As Integer, pbRow As Integer, rwOffset As Integer
      Dim chrt As String
      
      Set outSheet = Sheets("Compose")
      outputPath = "C:\Data\Barry\VBA\SO\"
      fileStem = "Charts"
      'these values in 'points'
      topM = 60
      botM = 60
      rightM = 60
      'these values in 'rows'
      pbRow = 1
      rwOffset = 8
      chHeight = 12
      chWidth = 5
      Set RngToCover = Cells(chHeight, chWidth)
      n = 0
      
          With ThisWorkbook
              With outSheet
                  .ResetAllPageBreaks
                  .ChartObjects.Delete
                      With .PageSetup
                          .Orientation = xlPortrait
                          .PrintArea = ""
                          .TopMargin = topM
                          .BottomMargin = botM
                          .RightMargin = rightM
                      End With
              End With
      
              For Each sht In .Worksheets
                  If Not sht.Name = outSheet.Name Then
                      'Copy Chart
                      Set chtObj = sht.ChartObjects(1)
                      chtObj.Copy
                          With outSheet
                              .Paste
                              n = n + 1
                              Set RngToCover = .Range(.Cells(pbRow + rwOffset, 1), .Cells(pbRow + rwOffset + chHeight, 1 + chWidth))
                              Set chtObj = .ChartObjects(n)
                              chtObj.Height = RngToCover.Height ' resize
                              chtObj.Width = RngToCover.Width   ' resize
                              chtObj.Top = RngToCover.Top       ' reposition
                              chtObj.Left = RngToCover.Left     ' reposition
                              'add hpage break
                              .HPageBreaks.Add before:=.Cells(pbRow + rwOffset + chHeight, 1 + chWidth).Offset(2, 0)
      
                              pbRow = .HPageBreaks(n).Location.Row
                          End With
                  End If
              Next sht
      
          ActiveCell.Select
      
              'set essential page parameters
              With outSheet.PageSetup
                  .Orientation = xlPortrait
                  .PrintArea = ""
                  .TopMargin = topM
                  .BottomMargin = botM
                  .RightMargin = rightM
              End With
      
              'produce pdf file
              outSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                  outputPath & fileStem & ".pdf", Quality:=xlQualityStandard, _
                  IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                  OpenAfterPublish:=True
      
          End With
      
      End Sub
      

      我使用了 Jon Peltier 的 interesting article,这可能也很有趣。

      【讨论】:

      • 如果它回答了你的问题,也许你会通过点击“勾号”来“接受”这个答案。这将表明它已解决。
      猜你喜欢
      • 2013-07-21
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多