【问题标题】:Word VBA Macro to Export Sections as PDFWord VBA 宏将部分导出为 PDF
【发布时间】:2018-01-10 07:41:08
【问题描述】:

背景:我目前有一个文档,它在 Word 中按分节符分成单独的部分。我有一个宏可以将部分的 pdf 打印 到用户选择的目录,还有一个宏可以将静态页面导出 为 pdf。我暂时在 export 宏中输入了页码,因为保存功能比打印为 pdf 功能快得多。但我希望将宏导出部分作为 pdf 文件,其页码可以更改。

注意:部分页面可能会根据我在主文件上所做的工作而改变,因此在我的宏中使用静态页码只是临时解决方案。解决这个问题对我来说真的很重要。

到目前为止我有什么(这是 export 宏):

Sub PLANv()
'
' PLANv Macro
'
'
Dim strName As String

strName = InputBox(Prompt:="Save To:", Title:="Save file to:", _
Default:="C:\Users\PRESTONAVH\Desktop\Task Order Files\")

If strName = vbNullString Then
    Exit Sub
Else

End If

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    strName & "PLAN.pdf", _
    ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
    wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=3, To:=4, Item:= _
    wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub

我想要什么

似乎我导出为 PDF 的宏不允许我将部分放在范围字段中,我已经尝试过,它总是给我一条错误消息。我目前有静态页面范围(3-4)。我在想也许在此之前我可以输入一些代码,这将返回我正在导出的部分的起始页码和结束页码。然后我可以为返回的任何内容分配一个字符串,然后在范围函数中输入这些字符串作为页码?

我真的不擅长这些东西,但我浏览了很多论坛并试图拼凑其他人的建议,但没有运气。可能有一个更简单的解决方案,但只要它有效,那就太好了。一段时间以来,我一直在尝试解决这个问题,但是我浏览了太多论坛,并且是 VBA 超级初学者。

如果有人能帮助我,我将不胜感激。

谢谢

更新: 我按照建议尝试了导出部分代码,但我的字段在导出的文档中被删除并添加了一个空白页。所以我试图使用部分范围来设置导出范围的第一个和最后一个整数。我可以得到 intValue1 ,它给了我部分范围的最后一页。但我不知道如何获取节范围第一页的 intValue2 。下面是我在保存提示和导出代码之间添加的内容。

Dim intValueR As Range
Dim intValue1 As Integer
Dim intValue2 As Integer
Set intValueR = ActiveDocument.Sections(3).Range
intValue1 = CStr(intValueR.Information(wdActiveEndPageNumber))
intValue2 = ??

(已解决) 大家好,感谢您帮助我,我现在有最终的代码对我来说运行良好。代码如下:

Dim strName As String

strName = InputBox(Prompt:="Save To:", Title:="Save file to:", _
Default:="C:\Users\PRESTONAVH\Desktop\Task Order Files\")

If strName = vbNullString Then
Exit Sub
Else

End If

Dim intValue1 As Integer
Dim intValue2 As Integer
intValue1 = 
ActiveDocument.Sections(1).Range.Information(wdActiveEndPageNumber) + 1
intValue2 = 
ActiveDocument.Sections(2).Range.Information(wdActiveEndPageNumber)

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    strName & "PLAN.pdf", _
    ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
    wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=intValue1, 
To:=intValue2, Item:= _
    wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=True, UseISO19005_1:=False

【问题讨论】:

  • 尝试导出页码时出现问题,因为页码取决于打印的页面大小、边距等。您可能需要以某种方式指定具有预设页面的“虚拟”打印机和边距大小。
  • 我在另一个宏上注意到了这一点,它在运行它的任何一台计算机上都使用默认设置。当我们更改默认设置时,边距和页面大小正确显示。但目前,导出功能似乎更适合我们的目的。谢谢!

标签: vba ms-word


【解决方案1】:

好吧,所以我找到了一个解决方案,它真的很难看,但它确实有效。因为我不知道如何将节范围的第一部分设置为整数,但我确实知道如何将节范围的最后部分设置为整数。我只是将第一部分设置为上一节范围的最后部分加 1。

结果就是这段代码:

Dim intValueR As Range
Dim intValueR2 As Range
Dim intValue1 As Integer
Dim intValue2 As Integer
Set intValueR = ActiveDocument.Sections(3).Range
Set intValueR2 = ActiveDocument.Sections(2).Range
intValue1 = CStr(intValueR.Information(wdActiveEndPageNumber))
intValue2 = CStr(intValueR2.Information(wdActiveEndPageNumber)) + 1

如果有人有更清洁的方式来做到这一点,那就太棒了。但如果有人需要这样的东西,它对我有用。

向@TonyM 和@jsotola 致敬!

【讨论】:

  • NB:Cstr 是不必要的,并且可能是错误的开销。它将整数/长值转换为字符串,然后隐式转换回整数(例如,intValue1 As Integer 等)。确定删除Cstr
  • @PrestonAVH,将section2page转换包装在一个函数function sec2page(sec as integer) as integersec2page = ActiveDocument.Sections(sec).Range.Information(wdActiveEndPageNumber)end function
  • @jsotoloa,哇,谢谢。这大大清理了代码并消除了定义范围的使用。非常感谢您的帮助,这将为我节省大量工作时间
【解决方案2】:

我建议您先选择要导出的部分,然后使用wdExportSelection 而不是wdExportFromTo。要选择该部分,请参阅this page,其中使用了Selection.Range.Sections.First.Range.Select,或使用宏记录器进行试验。但是,如果您确实想使用wdExportFromTo,那么关于“将这些字符串作为页码输入”的问题的答案是,是的,您的想法是正确的,但是您需要使用整数而不是字符串。

【讨论】:

  • 感谢您的反馈。我尝试了 wdExportSelection,但遇到了更多问题。我将研究如何将部分中的页码分配为您提到的整数,因为这最终可能会起作用。当我导出 ExportSelection 并且我的字段在导出的文档中为空白时,我得到一个额外的页面。 (我在文档中更新了字段)谢谢!
  • 所以我一直在尝试输入页码是整数,并且已经弄清楚如何将节的结尾作为整数。我无法将部分范围的第一个值作为整数。我猜这很容易,你知道那个命令可能是什么吗?下面是我添加的内容,intValue1 用于识别该部分的最后一页。但我无法让 inValue2 识别该部分的第一页。
  • Dim intValueR As Range Dim intValue1 As Integer Dim intValue2 As Integer Set intValueR = ActiveDocument.Sections(3).Range intValue1 = CStr(intValueR.Information(wdActiveEndPageNumber)) intValue2 = ?? 
  • 我添加了上述帖子中的代码作为对我原始帖子的编辑。格式使它在我的回复中无法阅读。
【解决方案3】:

这是如何导出范围...通过记录一个以Selection.ExportAsFixedFormat开头的宏来达到这个目的...Selection是一个范围对象.....

ActiveDocument.Range(800, 1000).ExportAsFixedFormat _
    OutputFileName:=strName & "PLAN.pdf", _
    ExportFormat:=wdExportFormatPDF, _
    OpenAfterExport:=True, _
    OptimizeFor:=wdExportOptimizeForPrint, _
    ExportCurrentPage:=False, _
    Item:=wdExportDocumentContent, _
    IncludeDocProps:=False, _
    KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, _
    DocStructureTags:=True, _
    BitmapMissingFonts:=True, _
    UseISO19005_1:=False

更改第一行以导出第二部分

    ActiveDocument.Sections(2).Range().ExportAsFixedFormat _

您可以这样做以查看将导出的内容...用于调试,查看您引用的范围

ActiveDocument.Sections(2).Range().Select   ' you can instead paste this line into the "Immediate Window", and then look at your document
Stop

【讨论】:

  • 感谢您提供上面的代码,我将其粘贴并试了一下,但是正在生成一个额外的页面,并且在导出的文档中找不到我在文档中的活动字段。我将首先尝试在我的第一种方法中使用整数,但也会使用这个方法。我是新手,所以这对我来说都是陌生的。感谢您的支持!
猜你喜欢
  • 2015-02-06
  • 2020-02-07
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 2017-12-12
  • 2018-09-29
  • 2020-10-15
  • 1970-01-01
相关资源
最近更新 更多