×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VBA Word, Tables & Fields

VBA Word, Tables & Fields

VBA Word, Tables & Fields

(OP)
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

RE: VBA Word, Tables & Fields

How about

CODE

If Len(Trim(.cell(3, 4).Range.Text)) = 0 Then
    'No Text
Else
    ....
end If 


---- Andy

There is a great need for a sarcasm font.

RE: VBA Word, Tables & Fields

(OP)
Trim will remove whitespace, but it won't remove the field, so it still thinks that something is there when I try it.

Jeff

RE: VBA Word, Tables & Fields

Just ask how many fields are there.

CODE --> VBA

.cell(r,c).range.fields.count 

RE: VBA Word, Tables & Fields

(OP)
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

RE: VBA Word, Tables & Fields

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

.cell(r,c).range.fields
 
and hit Ctrl-Space?

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

---- Andy

There is a great need for a sarcasm font.

RE: VBA Word, Tables & Fields

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]

RE: VBA Word, Tables & Fields

(OP)
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

RE: VBA Word, Tables & Fields

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]

RE: VBA Word, Tables & Fields

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close