Send this page




Acrobat Resources

HACK #55
Easily Attach Your Document’s Tables
Pack your document’s essential information into its PDF edition.

Readers copy data from PDF documents to use in their own documents or spreadsheets. Tables usually contain the most valuable data, yet they are the most difficult to extract from a PDF. Give readers what they need, as shown in Figure 5-4, by automatically extracting tables from your source document, converting them into an Excel spreadsheet, and then attaching them to your PDF.

Figure 5-4. Giving your readers live data to work with



Copy tables into a new document

In Microsoft Word, use the following macro to copy a document’s tables into a new document. In Word, create the macro like so:

Open the Macros dialog box (Tools > Macro > Macros...). Type CopyTablesIntoNewDocument into the “Macro name:” field, set “Macros in:” to Normal.dot, and click Create.

A window will open where you can enter the macro’s code. It already will have two lines of code: Sub CopyTablesIntoNewDocument() and End Sub. You don’t need to duplicate these lines.

You can download the following code from www.pdfhacks.com/copytables/:

Sub CopyTablesIntoNewDocument()
' version 1.0
' http://www.pdfhacks.com/copytables/
Dim SrcDoc, NewDoc As Document
Dim SrcDocTableRange As Range
Set SrcDoc = ActiveDocument
If SrcDoc.Tables.Count <> 0 Then

  Set NewDoc = Documents.Add(DocumentType:=wdNewBlankDocument)
  Set NewDocRange = NewDoc.Range
  Dim PrevPara As Range
  Dim NextPara As Range
  Dim NextEnd As Long
  NextEnd = 0

  For Each SrcDocTable In SrcDoc.Tables
    Set SrcDocTableRange = SrcDocTable.Range

     'output the preceding paragraph?
     Set PrevPara = SrcDocTableRange.Previous(wdParagraph, 1)
     If PrevPara Is Nothing Or PrevPara.Start < NextEnd Then
     Else
       Set PPWords = PrevPara.Words
       If PPWords.Count > 1 Then 'yes
         NewDocRange.Start = NewDocRange.End
         NewDocRange.InsertParagraphBefore
         NewDocRange.Start = NewDocRange.End
         NewDocRange.InsertParagraphBefore
         NewDocRange.FormattedText = PrevPara.FormattedText			
    	  End If
     End If

    'output the table
    NewDocRange.Start = NewDocRange.End
    NewDocRange.FormattedText = SrcDocTableRange.FormattedText

    'output the following paragraph?
    Set NextPara = SrcDocTableRange.Next(wdParagraph, 1)
    If NextPara Is Nothing Then
    Else
      Set PPWords = NextPara.Words
      NextEnd = NextPara.End
      If PPWords.Count > 1 Then 'yes
         NewDocRange.Start = NewDocRange.End
         NewDocRange.InsertParagraphBefore
         NewDocRange.FormattedText = NextPara.FormattedText
       End If
    End If

  Next SrcDocTable

End If
End Sub

Run this macro from Word by selecting Tools > Macro > Macro..., selecting Copy Tables Into New Document, and clicking Run. A new document will open that contains all the tables from your current document. It will also include the paragraphs immediately before and after each table. This feature was added to help readers find the table they want. Modify the macro code to suit your requirements.



Excerpt from "PDF Hacks" by Sid Steward
Republished with permission of O'Reilly Media
© 2004 O'Reilly Media



<< Back to Books on Acrobat


AcrobatUsers.com  >>  User Groups • News • Events • Articles • Blogs • How To • Resources • Member Log in