使用Excel宏在一张图纸中创建特定范围的PDF

使用Excel宏在一张图纸中创建特定范围的PDF,excel,vba,Excel,Vba,我可以在一定范围内创建pdf,但页面数量无法标准化。那么,是否有可能更新我的宏以在已知范围内创建4页pdf Dim fso As Object Dim s(1) As String Dim sNewFilePath As String Dim pg1 As Range Dim pg2 As Range Dim pg3 As Range Dim pg4 As Range Dim r As Range Dim ws As Worksheet Set ws = ActiveSheet

我可以在一定范围内创建pdf,但页面数量无法标准化。那么,是否有可能更新我的宏以在已知范围内创建4页pdf

Dim fso As Object
Dim s(1) As String
Dim sNewFilePath As String
Dim pg1 As Range
Dim pg2 As Range
Dim pg3 As Range
Dim pg4 As Range
Dim r As Range
Dim ws As Worksheet
    Set ws = ActiveSheet
     With ws.PageSetup
        .LeftMargin = Application.InchesToPoints(0)
        .RightMargin = Application.InchesToPoints(0)
        .TopMargin = Application.InchesToPoints(0)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .FitToPagesWide = 1
    End With
Set pg1 = ActiveSheet.Range("A1:K92")
Set pg2 = ActiveSheet.Range("A93:K164")
Set pg3 = ActiveSheet.Range("A165:K237")
Set pg4 = ActiveSheet.Range("A239:K313")
Set r = Union(pg1, pg2, pg3, pg4)
    Set fso = CreateObject("Scripting.FileSystemObject")
    s(0) = ThisWorkbook.FullName

    If fso.FileExists(s(0)) Then
        '//Change Excel Extension to PDF extension in FilePath
        s(1) = fso.GetExtensionName(s(0))
        If s(1) <> "" Then
            s(1) = "." & s(1)
            sNewFilePath = Replace(s(0), s(1), ".pdf")


                r.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=sNewFilePath, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, OpenAfterPublish:=True
        End If
    Else
        '//Error: file path not found
        MsgBox "Error: this workbook may be unsaved.  Please save and try again."
    End If

    Set fso = Nothing
ActiveWorkbook.Save
ActiveWindow.Close
Dim fso作为对象
Dim s(1)作为字符串
将sNewFilePath设置为字符串
Dim pg1 As系列
Dim pg2 As系列
Dim pg3 As系列
Dim pg4 As系列
调光范围
将ws设置为工作表
设置ws=ActiveSheet
使用ws.PageSetup
.LeftMargin=Application.InchesToPoints(0)
.RightMargin=应用程序.InchesToPoints(0)
.TopMargin=应用程序.InchesToPoints(0)
.BottomMargin=应用程序.InchesToPoints(0)
.HeaderMargin=应用程序.InchesToPoints(0)
.FooterMargin=应用程序.InchesToPoints(0)
.FitToPagesWide=1
以
设置pg1=ActiveSheet.Range(“A1:K92”)
设置pg2=ActiveSheet.Range(“A93:K164”)
设置pg3=ActiveSheet.Range(“A165:K237”)
设置pg4=ActiveSheet.Range(“A239:K313”)
集合r=联合(第1、2、3、4页)
设置fso=CreateObject(“Scripting.FileSystemObject”)
s(0)=ThisWorkbook.FullName
如果存在fso.files(s(0)),则
“//将文件路径中的Excel扩展名更改为PDF扩展名
s(1)=fso.GetExtensionName(s(0))
如果s(1)“,则
第(1)款=“”&第(1)款
sNewFilePath=Replace(s(0),s(1),“.pdf”)
r、 ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=sNewFilePath_
质量:=xlQualityStandard,IncludeDocProperties:=True_
IgnorePrintAreas:=False,OpenAfterPublish:=True
如果结束
其他的
“//错误:找不到文件路径
MsgBox“错误:此工作簿可能未保存。请保存并重试。”
如果结束
设置fso=无
活动工作簿。保存
活动窗口,关闭
这并不像我想象的那样有效。 我还尝试了
hpagebreaks.add
,但我无法管理它


那么您有什么想法吗?

响应我在测试时尝试的请求代码(这与您的代码非常相似,使用
Union()
然后导出):

示例代码如下:

Private Sub printToPDF()
    Dim printArea1 As Range
    Set printArea1 = Range(Cells(1, "A"), Cells(11, "R"))
    Dim printarea2 As Range
    Set printarea2 = Cells(61, "A")
    Dim completePrintRange As Range
    Set completePrintRange = Union(printArea1, printarea2)
    completePrintRange.ExportAsFixedFormat xlTypePDF, "Test", , , , , , True
End Sub

请注意,导出将在PDF中的各自页面上显示每个范围。从列运行到下一页,例如,
printArea1
在两页上。这与纸张大小和缩放有关。

响应我在测试时尝试的请求代码(这与代码中的代码非常相似,使用
Union()
然后导出):

示例代码如下:

Private Sub printToPDF()
    Dim printArea1 As Range
    Set printArea1 = Range(Cells(1, "A"), Cells(11, "R"))
    Dim printarea2 As Range
    Set printarea2 = Cells(61, "A")
    Dim completePrintRange As Range
    Set completePrintRange = Union(printArea1, printarea2)
    completePrintRange.ExportAsFixedFormat xlTypePDF, "Test", , , , , , True
End Sub

请注意,导出将在PDF中的各自页面上显示每个范围。从列运行到下一页,例如,
printArea1
在两页上。这与纸张大小和缩放有关。

事实上,我已经跟随你的脚步,请我的朋友给我一双新眼睛。他只是用记录宏基本上解决了这个问题。现在可以了。 @西里尔感谢你和你的时间

   Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = ""
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0)
        .RightMargin = Application.InchesToPoints(0)
        .TopMargin = Application.InchesToPoints(0)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = True
        .CenterVertically = True
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 4
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = False
        .AlignMarginsHeaderFooter = True
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = "$C$1:$K$312"
    ActiveSheet.ResetAllPageBreaks
    Set ActiveSheet.HPageBreaks(1).Location = Range("C93")
    Set ActiveSheet.HPageBreaks(2).Location = Range("C165")
    Set ActiveSheet.HPageBreaks(3).Location = Range("C239")

    Dim FileName As String
    Application.DisplayAlerts = False
    On Error Resume Next

    sPath = ThisWorkbook.Path
    With Worksheets("Final")

        FileName = ThisWorkbook.FullName
                               .ExportAsFixedFormat _
                Type:=xlTypePDF, _
                FileName:=Left(FileName, InStr(FileName, ".") - 1), _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=True
    End With

事实上,我已经跟随你的脚步,请我的朋友给我一双新的眼睛。他只是用记录宏基本上解决了这个问题。现在可以了。 @西里尔感谢你和你的时间

   Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = ""
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0)
        .RightMargin = Application.InchesToPoints(0)
        .TopMargin = Application.InchesToPoints(0)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = True
        .CenterVertically = True
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 4
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = False
        .AlignMarginsHeaderFooter = True
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = "$C$1:$K$312"
    ActiveSheet.ResetAllPageBreaks
    Set ActiveSheet.HPageBreaks(1).Location = Range("C93")
    Set ActiveSheet.HPageBreaks(2).Location = Range("C165")
    Set ActiveSheet.HPageBreaks(3).Location = Range("C239")

    Dim FileName As String
    Application.DisplayAlerts = False
    On Error Resume Next

    sPath = ThisWorkbook.Path
    With Worksheets("Final")

        FileName = ThisWorkbook.FullName
                               .ExportAsFixedFormat _
                Type:=xlTypePDF, _
                FileName:=Left(FileName, InStr(FileName, ".") - 1), _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=True
    End With

您需要将每个范围复制到新工作表中,将这些工作表上的
PrintArea
设置为该范围的区域,然后选择所有工作表。选择工作表后,可以使用
ExportAsFixedFormat
创建PDF。实际上,这是一种a表单,所有工作表都应位于同一工作表中。通常情况下,如果它们在不同的工作表中,这很容易。@d讽刺的是,使用
union()
会将这些范围中的每一个作为它们自己的页面放在导出文件中。Selpaq,您是否有问题,即每个用户的打印参数(边距等)正在将其中一些打印区域从页面推到下一页?@cyril是的,所有这些范围都应保留在他们自己的页面中。但是标题可以跳转到其他图纸或最后一行。这是一种非常重要的形式,全球有50多人使用它。而且我无法手动逐个设置它们的打印区域。@SelpaqM您需要设置打印区域,包括页边距/页眉/等,例如,在
导出为固定格式之前添加
。FitToPagesWide=1
。之后,您需要将
IgnorePrintAreas
的参数设置为false以使用指定的打印条件。您需要将这些范围复制到新工作表中,将这些工作表上的
PrintArea
设置为该范围的区域,然后选择所有工作表。选择工作表后,可以使用
ExportAsFixedFormat
创建PDF。实际上,这是一种a表单,所有工作表都应位于同一工作表中。通常情况下,如果它们在不同的工作表中,这很容易。@d讽刺的是,使用
union()
会将这些范围中的每一个作为它们自己的页面放在导出文件中。Selpaq,您是否有问题,即每个用户的打印参数(边距等)正在将其中一些打印区域从页面推到下一页?@cyril是的,所有这些范围都应保留在他们自己的页面中。但是标题可以跳转到其他图纸或最后一行。这是一种非常重要的形式,全球有50多人使用它。而且我无法手动逐个设置它们的打印区域。@SelpaqM您需要设置打印区域,包括页边距/页眉/等,例如,在
导出为固定格式之前添加
。FitToPagesWide=1
。之后,您需要将
IgnorePrintAreas
的参数设置为false,以使用指定的打印条件。不幸的是,该代码不适用于我。还是有同样的问题。我认为
xlpagebreakmanual
可以解决我的问题,但它不能设置为超过1个已知范围。明白,@SelpaqM。提供代码纯粹是为了解决我测试的情况下的请求。我唯一的另一个建议是把文件。。。11x17纸,用