Home:ALL Converter>Word VBA Macro to Export Sections as PDF

Word VBA Macro to Export Sections as PDF

Ask Time:2017-08-03T05:58:32         Author:PrestonAVH

Json Formatter

Background: I currently have a document that is broken up into separate sections by section breaks in Word. I have a macro to print pdf's of the sections to a users chosen directory and a macro to export static pages as pdf's. I've entered the page numbers in the export macro for the time being because the save function works a lot faster than the print as pdf function. But I would like to have the macro export sections as pdfs with page numbers that can change.

Note: Section pages can change depending on the work I am doing on my master file, so using static page numbers in my macro is only temporary solution. Resolving this is really important for me.

What I Have So Far (This is the export macro):

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

What I Would Like to Have:

It seems that my macro that exports as a PDF does not allow me to put the sections in the range field, I've tried and it always gives me an error message. I currently have the static page range in there (3-4). I was thinking that maybe there there is some code I could enter before that, that would return the starting page number and ending page number of the section I'm exporting. Then I could assign a string to whatever is returned and then in the range function enter those strings as the page numbers?

I'm really not good at this stuff but have been going through lots of forums and trying to piece together what other people have suggested with no luck. There is probably a much easier solution, but as long as it works that would really be great. I've been trying to figure this out for some time now, but I've gone through too many forums and am a VBA super beginner.

If anyone would be able to help me out I would really appreciate it.

Thank You

Update: I tried the export section code as recommended but my fields were erased in the exported documents and a blank page added. So I'm trying to use the section range to set the first and last integers of the export range. I can get intValue1 which gives me the last page of the section range. But I don't know how to get intValue2 for the first page of the section range. Below is what I added in between my save prompt and the export code.

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

(SOLVED) Hi everyone, thanks for helping me out, I have the final code that is working well for me now. Here's the code:

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

Author:PrestonAVH,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45471698/word-vba-macro-to-export-sections-as-pdf
PrestonAVH :

Alright so I found a solution, it's really ugly but it works. Since I didn't know how to set the first part of the section range as an integer but I did know how to set the last part of the section range as an integer. I simply set the first part as the last part of the previous section's range plus 1. \n\nThe result is this bit of code:\n\nDim intValueR As Range\nDim intValueR2 As Range\nDim intValue1 As Integer\nDim intValue2 As Integer\nSet intValueR = ActiveDocument.Sections(3).Range\nSet intValueR2 = ActiveDocument.Sections(2).Range\nintValue1 = CStr(intValueR.Information(wdActiveEndPageNumber))\nintValue2 = CStr(intValueR2.Information(wdActiveEndPageNumber)) + 1\n\n\nIf someone has a cleaner way of doing this, that would be fantastic. But if someone needs something like this, it's working for me.\n\nShout out to @TonyM and @jsotola!",
2017-08-03T16:32:37
Tony M :

I would suggest that you first select the section you want to export and then use wdExportSelection instead of wdExportFromTo. To select the section, see this page where Selection.Range.Sections.First.Range.Select is used, or experiment with the macro recorder. But, if you do want to use wdExportFromTo then the answer to your question about \"entering those strings as page numbers\" is that, yes, you have the right idea, but you would need to use integers rather than strings. ",
2017-08-02T23:17:13
yy