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