0
votes

I've been asked to create a MS access database that catalogs all the data that is stored in MS Word Files. The Word files have tables that contain the data.
For example, "Customer Name:" | Customer data | "Date" | Date data

I have used MS Access to loop and open each file using objects Word.Application and Word.Document . Then I use a 2nd loop on i using this command to get the value in the cell: worddoc.Tables(tableindex).Range.Cells(i).Range.Value

HOWEVER if the cell contains a dropdown box, I do not get the value in the dropdown box; I get a square character.

1) Is there a way to determine what type of data is in the cell? Most of the time it would be Text, Textbox, or Dropdown box. 2) When it is a Dropdown box, how do I get the data from it?

I hope I provided enough information. please ask if you need more info.

2
What kind of dropdown box might it be? - Tim Williams
@TimWilliams So it is, Tim. I wandered from the Word to the Excel object model. Sigh. I'll delete the comment. Thanks! - BobRodes

2 Answers

1
votes

Maybe something like this (for example - assuming you have "content controls" and not some other type of control...)

Sub Tester()

    Dim t As Table, r, c, rw, cel As Cell, cc

    Set t = ActiveDocument.Tables(1)
    For r = 1 To t.Rows.Count
        Set rw = t.Rows(r)
        For c = 1 To rw.Cells.Count
            Set cel = rw.Cells(c)
            Debug.Print r, c, GetContent(cel)
        Next c
    Next r
End Sub

Function GetContent(c As Cell)
    Dim cc, con, sep
    Set cc = c.Range.ContentControls
    If cc.Count = 0 Then
        'stripping off "end of cell" marker...
        GetContent = Right(c.Range.Text, Len(c.Range.Text) - 2)
    Else
       For Each con In cc
        GetContent = GetContent & sep & con.Range.Text
        sep = " "
       Next con
    End If
End Function
1
votes

The little square box you are getting is the end of cell marker. In a table cell, it functions kind of like a last paragraph. So ... you have to get rid of it before you do anything else.

The following is some example test code, just looking at one cell in a table, but use it as a guide to fit into your process of looping thru the various cells. My other caveat is I took your reference to a "Textbook" literally in this example. If what you meant was a text box content control, then you can ignore that portion of this example.

Dim rng As Range, cc As ContentControl, shp As Shape
Set rng = ActiveDocument.Tables(1).rows(1).Cells(1).Range
rng.MoveEnd wdCharacter, -1
If rng.ContentControls.Count > 0 Then
    Set cc = rng.ContentControls(1)
    Debug.Print cc.Range.Text
ElseIf rng.ShapeRange.Count > 0 Then
    If rng.ShapeRange(1).Type = msoTextBox Then
        Set shp = rng.ShapeRange(1)
        If shp.TextFrame.HasText Then
            Debug.Print shp.TextFrame.TextRange.Text
        End If
    End If
Else
    If Not rng.Text = vbNullString Then
        Debug.Print rng.Text
    End If
End If