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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling data from a table using a bookmark? 1

Status
Not open for further replies.

it2hill

IS-IT--Management
Sep 5, 2002
28
US
I am pulling data from a word document using bookmarks.

If ActiveDocument.Bookmarks.Exists(sBookmark) Then
temp = ActiveDocument.Bookmarks(sBookmark).Range.Text
Else
iErrorCount = iErrorCount + 1
MsgBox ("ERROR: The Bookmark named '" & sBookmark & "' can not be found for: " & sFileName)
End If

The problem is that some users are adding a table inside the bookmark. When the data is placed in temp, I loose the table... but keep the contents. Is there a way to 'copy' the data out of the bookmark into temp? I need to keep the data in the same format that the user enters the information for the section.

Thanks,

Hill
 
Not sure what you're doing with the data once you get it, but you can check for the existence of a table and extract its contents explicitly. XML has gained much popularity due to this kind of problem.

Since you don't know what the users are going to add to the document, you can't get field names for the tables unless you you know the first row of the table actually contains the field names.

You can easily convert the tables to comma delimited format and identify them using a text qualifier such as "
" and "[END TABLE]" so when you read back the data you can recreate the table.

You could also convert the tables to HTML which would recreate the tables as well. Here are a couple of examples:

1. Delimited:
Code:
Public Function GetBookMarkDelimited(ByVal strBookMark As String, Optional ByVal strDelim As String = ",") As String
On Error GoTo ErrHandler

  Dim wd As Word.Application
  Dim doc As Word.Document
  Dim p As Word.Paragraph
  Dim bm As Word.Bookmark
  Dim i As Integer
  Dim idx As Integer
  Dim tbl As Object
  Dim strTemp As String
  
  If strDelim = "" Then strDelim = ","
  
  Set wd = New Word.Application
  Set doc = wd.Documents.Open("C:\Bookmarks.doc", , False, False)
  
  If doc.Bookmarks.Exists(strBookMark) Then
    Set bm = doc.Bookmarks(strBookMark)

    With bm
      For i = 1 To .Range.Paragraphs.Count
        Set p = .Range.Paragraphs(i)
        If p.Range.Tables.Count > 0 Then
          Set tbl = p.Range.Tables(1)
          idx = tbl.rows.Count
          strTemp = strTemp & "[TABLE]" & vbCr
          strTemp = strTemp & p.Range.Tables(1).ConvertToText(strDelim)
          strTemp = strTemp & "[END TABLE]" & vbCr & vbCr
          i = i + idx
        Else
          strTemp = strTemp & Replace(p.Range.Text, Chr(1), "")
        End If
NextParagraph:
      Next i
    End With
  End If
  
  GetBookMarkDelimited = strTemp
  
ExitHere:
  On Error Resume Next
  doc.Close False
  wd.Quit
  Exit Function
ErrHandler:
  If Err = 5941 Then
    Resume NextParagraph
  End If
  MsgBox Err & ": " & Err.Description
  Resume ExitHere
End Function
Sample output:

[tt]
Summary

Here’s the sales information:


Bill,4.55,2/1/03
Bob,6.25,2/11/03
Henry,23.00,2/12/03
Jenny,54.60,2/15/03
Jill,45.00,2/16/03
Gary,12.05,2/18/03
Joe,56.90,2/19/03
Eddie,37.64,2/21/03
[END TABLE]




Send me an email when you read this,

Thanks.
[/tt]


2. HTML format:
Code:
Public Function GetBookMarkHTML(ByVal strBookMark As String) As String
On Error GoTo ErrHandler

  Dim wd As Word.Application
  Dim doc As Word.Document
  Dim p As Word.Paragraph
  Dim bm As Word.Bookmark
  Dim rw As Word.row
  Dim cl As Word.Cell
  Dim i As Integer
  Dim idx As Integer
  Dim tbl As Object

  Dim strTemp As String
  
  Set wd = New Word.Application
  Set doc = wd.Documents.Open("C:\Bookmarks.doc", , False, False)
  
  If doc.Bookmarks.Exists(strBookMark) Then
    Set bm = doc.Bookmarks(strBookMark)

    With bm
      For i = 1 To .Range.Paragraphs.Count
        Set p = .Range.Paragraphs(i)
        If p.Range.Tables.Count > 0 Then
          Set tbl = p.Range.Tables(1)
          idx = tbl.Range.Paragraphs.Count
          strTemp = strTemp & &quot;<TABLE  BORDER='1' CELLPADDING='4'>&quot; & vbCr
          For Each rw In tbl.rows
              strTemp = strTemp & &quot;  <TR>&quot; & vbCr
            For Each cl In rw.Cells
              strTemp = strTemp & &quot;    <TD>&quot; & _
                Left(cl.Range.Text, InStrRev(cl.Range.Text, Chr(13)) - 1) & &quot;</TD>&quot; & vbCr
            Next cl
              strTemp = strTemp & &quot;  </TR>&quot; & vbCr
          Next rw
          strTemp = strTemp & &quot;</TABLE>&quot; & vbCr
          i = i + idx
        Else
          strTemp = strTemp & Replace(p.Range.Text, Chr(1), &quot;&quot;)
        End If
NextParagraph:
      Next i
    End With
  End If
  
  GetBookMarkHTML = strTemp
  
ExitHere:
  On Error Resume Next
  doc.Close False
  wd.Quit
  Exit Function
ErrHandler:
  If Err = 5941 Then
    Resume NextParagraph
  End If
  MsgBox Err & &quot;: &quot; & Err.Description
  Resume ExitHere
End Function
Sample output:

[tt]
Summary

Here’s the sales information:

<TABLE BORDER='1' CELLPADDING='4'>
<TR>
<TD>Bill</TD>
<TD>4.55</TD>
<TD>2/1/03</TD>
</TR>
<TR>
<TD>Bob</TD>
<TD>6.25</TD>
<TD>2/11/03</TD>
</TR>
<TR>
<TD>Henry</TD>
<TD>23.00</TD>
<TD>2/12/03</TD>
</TR>
<TR>
<TD>Jenny</TD>
<TD>54.60</TD>
<TD>2/15/03</TD>
</TR>
<TR>
<TD>Jill</TD>
<TD>45.00</TD>
<TD>2/16/03</TD>
</TR>
<TR>
<TD>Gary</TD>
<TD>12.05</TD>
<TD>2/18/03</TD>
</TR>
<TR>
<TD>Joe</TD>
<TD>56.90</TD>
<TD>2/19/03</TD>
</TR>
<TR>
<TD>Eddie</TD>
<TD>37.64</TD>
<TD>2/21/03</TD>
</TR>
</TABLE>



Send me an email when you read this,

Thanks.

[/tt]


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
VBSLammer,

Thanks so much for the detailed response. I am inserting the logic in my code right now.

IT2 Hill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top