【问题标题】:Excel to PDF ConversionExcel 到 PDF 转换
【发布时间】:2017-09-25 07:32:12
【问题描述】:

我有一个包含许多工作表的 Excel 工作簿。数据需要转换为PDF。

现在要注意的是,PDF 的页眉和页脚中必须存在一些图像。还需要打印页码。

您能否建议处理此问题的最佳方法。我在 VBA 上工作。谁能告诉这是否可以使用 Excel - VBA 来实现?

非常感谢。

【问题讨论】:

    标签: vba excel pdf


    【解决方案1】:

    试试这个方法,我已经解决了我的问题......因为你没有发布任何具体细节......你需要弄清楚如何使用这个代码......

    Private Sub CommandButton1_Click()
    
        Dim wksAllSheets As Variant ' define a worksheet object arrray
        Dim wksSheet1 As Worksheet  ' a dummy variable
        Dim strFilename As String   ' file name
        Dim strFilepath As String   ' file path, default is the current working dir
    
        ' file destination folder name...
        strFilepath = "C:\Reports" 
    
        ' check if folder exist or not, if doesn't then creates it otherwise ignore it
        If Len(Dir(strFilepath, vbDirectory)) = 0 Then
           MkDir strFilepath
       End If
    
        strFilepath = strFilepath & "\"
    
        ' Initialize variables
        wksAllSheets = Array("Strategy", "Summary", "Trades", "TradeAnalysis") '' array of sheetName i want to save as pdf 
        strFilename  = "outputFileName.pdf"
    
    
    
    
    
        ''''''''''''''''''''''''''''''''''''  Here I am setting the page layout for each sheet, use if you need '''''''''''''''''''''''''''''''''''''''''''''''''
    
        Set wksSheet1 = ThisWorkbook.Sheets("Strategy")
        ' change the page setup Attributes of 'Strategy' sheet object
        With wksSheet1.PageSetup
        .Orientation = xlLandscape
        .PaperSize = xlPaperFanfoldLegalGerman
        .Zoom = 85
        .TopMargin = 0
        .BottomMargin = 0
        .RightMargin = 0
        .LeftMargin = 0
        .HeaderMargin = 0
        .FooterMargin = 0
        End With
    
        ' change the page setup Attributes of 'Summary' sheet object
         Set wksSheet1 = ThisWorkbook.Sheets("Summary")
        With wksSheet1.PageSetup
        .Orientation = xlLandscape
        .PaperSize = xlPaperLegal
        .Zoom = 90
        .TopMargin = 0.25
        .BottomMargin = 0.25
        .RightMargin = 0.25
        .LeftMargin = 0.25
        .HeaderMargin = 0.25
        .FooterMargin = 0.25
        End With
    
        ' change the page setup Attributes of 'Trades' sheet object
        Set wksSheet1 = ThisWorkbook.Sheets("Trades")
        With wksSheet1.PageSetup
        .CenterHeader = "Trades"
        .Orientation = xlLandscape
        .PrintArea = "$B$2:$U$321"   ' pass it as a parameter
        .Zoom = 100
        .PaperSize = xlPaperLegal
        .PrintTitleRows = wksSheet1.Rows(2).Address
        End With
    
        ' change the page setup Attributes of 'TradeAnalysis' sheet object
         Set wksSheet1 = ThisWorkbook.Sheets("TradeAnalysis")
        With wksSheet1.PageSetup
        .CenterHeader = "TradeAnalysis"
        .Orientation = xlPortrait
        .LeftMargin = 0.25
        .RightMargin = 0.25
        .Zoom = 100
        End With
    
       '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    
    
        'Save all the sheet in the array as one single PDF file
        ThisWorkbook.Sheets(wksAllSheets).Select
        wksSheet1.ExportAsFixedFormat _
                  Type:=xlTypePDF, _
                  Filename:=strFilepath & strFilename, _
                  Quality:=xlQualityStandard, _
                  IncludeDocProperties:=True, _
                  IgnorePrintAreas:=False, _
                  OpenAfterPublish:=True
        'update the wksSheet1 object in with the next entry from wksAllSheets array of object
        wksSheet1.Select
    
    
    End Sub
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    

    这是reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2018-10-17
      • 2013-10-11
      • 2015-08-02
      相关资源
      最近更新 更多