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

VBA Word, Tables & Fields 1

Status
Not open for further replies.

jmarkus

Technical User
Oct 15, 2002
124
CA
I have a VBA macro which runs from Excel, opens a Word document and reads cells in a table in the Word document.

I read the cell with the code:
<code>
TrStr = .cell(3, 4).Range.Text
</code>

I need to know if the cell is empty. Sometimes the cell contains a Text Form Field which is empty, but because the Form Field itself is considered something (usually 5 blank spaces), it doesn't detect that it is empty.

How do I detect first that there is a Text Form Field in that cell, and second that the field is empty?

Thanks,
Jeff
 
How about

Code:
If Len(Trim(.cell(3, 4).Range.Text)) = 0 Then[green]
    'No Text[/green]
Else
    ....
end If


---- Andy

There is a great need for a sarcasm font.
 
Trim will remove whitespace, but it won't remove the field, so it still thinks that something is there when I try it.

Jeff
 
Just ask how many fields are there.

Code:
.cell(r,c).range.fields.count
 
Part way there. That will tell me if there is a field in the cell, how do I check if that field is empty or not?

Jeff
 
If you use early binding of Word, what do you get as intelisense when you type:

[pre]
.cell(r,c).range.fields
[/pre] and hit Ctrl-Space?

Because you may have something like:
[pre]
.cell(r,c).range.fields(0).Value
[/pre]

---- Andy

There is a great need for a sarcasm font.
 
Assuming there's only one paragraph in each cell and/or only the first paragraph has relevant content:
Trim(Split(.Cell(3, 4).Range.Text, vbCr)(0))

Cheers
Paul Edstein
[MS MVP - Word]
 
Ok, this is what I did and seems to work:

Code:
If .cell(3, 4).Range.Fields.Count > 0 Then
    'MsgBox ("Contains a field!")
     TrStr = .cell(3, 4).Range.Fields(1).Result
    'MsgBox ("TrStr is " & TrStr & "and is " & Len(TrStr) & " long")
End If
If TrStr = "" Then TrStr = "Empty"

Thanks,
Jeff
 
If all you want is the result of formfields in the document, you could ignore the cells altogether and simply loop through the formfields collection. For example:
Code:
Sub Demo1()
Dim FmFld As FormField
For Each FmFld In ActiveDocument.FormFields
  MsgBox FmFld.Result
Next
End Sub
or, for only formfields in tables:
Code:
Sub Demo2()
Dim Tbl As Table, FmFld As FormField
For Each Tbl In ActiveDocument.Tables
  For Each FmFld In Tbl.Range.FormFields
    MsgBox FmFld.Result
  Next
Next
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks Paul, but in this case it isn't what I wanted. Sometimes the cells contain FormFields and sometimes they don't. I am only interested in the visible contents.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top