Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert variable number of pages from .doc to .pdf 1

Status
Not open for further replies.

SMHSleepy

Technical User
Sep 8, 2009
174
0
0
CA
Hello all, I'm not sure if this is more of a MS Word Macro question or an Access database question but here it goes. I have documents in .doc format which need to be converted to .pdf for processing. I am able to write code in Access to grab the .doc files and convert them to .pdf but now there's one important catch. I don't want all the pages converted to .pdf and it's not always the same number of pages. There is a certain table in the document and I only want all the pages up to that table. Is there a way for access to scan the document, look for a marker, record the page number, then convert pages 1 to (marker -1)?

sleepy
 
This is really a Word question because you are using the Word object model.
The ExportAsFixedFormat method of the document has a parameter for the Range to export

1) Find you location in the document. I would assume you could use the tables collection or a bookmark
2) Define your range. Many ways to do that once you find your location. You could find the page number, but not necessary. Once you find the end of the table you can define a range as start of document to end of table
3) Pass the range as a parameter of the exportasfixedformat method.
 
Sounds doable. Will give it a try, thanks.
 
I tested the idea in Word. The range is actually a From To page and not a range. This exports from the beginning to the first table in the document.
Code:
Public Sub ExportSectionToPDF()
  Dim rng As Word.Range
  Dim doc As Word.Document
  Dim tbl As Word.Table
  Dim theStart As Long
  Dim theEnd As Long
  theStart = 1
  Set doc = Application.ActiveDocument
  Set tbl = doc.Tables(1)
  theEnd = tbl.Range.Information(wdActiveEndPageNumber)
  doc.ExportAsFixedFormat doc.Path & "\Test.pdf", wdExportFormatPDF, True, wdExportOptimizeForOnScreen, wdExportFromTo, theStart, theEnd
End Sub
 
Sorry, I've been distracted and haven't had a chance to test this yet. If I want to go to the 2nd table instead of the first, would I just change the code to Set tbl = doc.Tables(2)?
 
I just tested it and my assumption was correct; however, this doesn't work for me because the end point is not actually a table. Is there a way to modify this so it looks for a specific phrase instead of a table?
 
The example given from the link is:
With Selection.Find
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Execute FindText:="library"
End With
But how do I find what page number that relates to?
 
These are examples and assume you have some knowledge of vba. Not meant to be a cut and paste answer for someone without some basic programming skills.

Code:
Public Sub ExportSectionToPDF()
  Dim rng As Word.Range
  Dim doc As Word.Document
  Dim theStart As Long
  Dim theEnd As Long
  'Start page
  theStart = 1
  Set doc = Application.ActiveDocument
  Set rng = doc.Content
  'Can be more precise look at the find.execute method
  rng.Find.Execute ("Text to find")
  'End page
  theEnd = rng.Information(wdActiveEndPageNumber)
  doc.ExportAsFixedFormat doc.Path & "\Test.pdf", wdExportFormatPDF, True, wdExportOptimizeForOnScreen, wdExportFromTo, theStart, theEnd
End Sub
 
Thank you very much majP!
Works like a charm. Just made a small ammendment to grab existing file name and change it from .doc to .pdf

Code:
Sub ExportSectionToPDF()
  Dim rng As Word.Range
  Dim doc As Word.Document
  Dim theStart As Long
  Dim theEnd As Long
  Dim strDocName As String
  Dim strFName As String
  Dim path As String
   'Start page
  theStart = 1
  Set doc = Application.ActiveDocument
  Set rng = doc.Content
  'Can be more precise look at the find.execute method
  rng.Find.Execute ("RECORDING TECH'S COMMENTS")
  'End page
  theEnd = rng.Information(wdActiveEndPageNumber) - 1
  path = "J:\"
  With ActiveDocument
  strDocName = (.Name)
  strFName = Replace(strDocName, ".doc", ".pdf")
  doc.ExportAsFixedFormat path & strFName, wdExportFormatPDF, True, wdExportOptimizeForOnScreen, wdExportFromTo, theStart, theEnd
  End With
End Sub
 
Hi MajP,

After the conversion, Adobe Acrobat remains open. Is there a way to close it?

sleepy
 
Nevermind, I figured it out. Just had to change the True to False in the macro. Duh! All is good now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top