title: Word VBA Macro to Export Sections as PDF
body: <p><strong>Background</strong>: I currently have a document that is broken up into separate sections by section breaks in Word. I have a macro to <strong>print</strong> pdf's of the sections to a users chosen directory and a macro to <strong>export</strong> static pages as pdf's. I've entered the page numbers in the <strong>export</strong> 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.</p>
  
  <p><strong>Note</strong>: 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. </p>
  
  <p>What I Have So Far (This is the <strong>export</strong> macro):</p>
  
  <pre><code>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 &amp; "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
  </code></pre>
  
  <p><strong>What I Would Like to Have</strong>:</p>
  
  <p>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? </p>
  
  <p>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.</p>
  
  <p>If anyone would be able to help me out I would really appreciate it.</p>
  
  <p>Thank You</p>
  
  <p>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.</p>
  
  <pre><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 = ??
  </code></pre>
  
  <p><strong>(SOLVED)</strong>
  Hi everyone, thanks for helping me out, I have the final code that is working well for me now. Here's the code:</p>
  
  <pre><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 &amp; "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
  </code></pre>