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!

Comparing a value in word table 1

Status
Not open for further replies.

davefish

Technical User
Jul 26, 2002
169
GB
I have a table in word with many cells, some of which are related by catagory. I need to identify the text in one of those cells, and depenadant upon the entry, change the second. Although I can write to a given cell using:-

With ActiveDocument.Tables(1)
.Cell(1, 1).Select
Selection.Range.Text = "Large"

End With

I cannot seem to read this value to trigger an if statement

If Tables(1).Cell(1, 1).Range.Text = "Large" Then
MsgBox ("Large")
End If


Am I missing something??

DaveFish
 
Hi DaveFish,

Yes, I can see that that might be confusing.

When you pick up the Range.Text of a Cell it includes the end-of-cell marker which you need to strip off. There are various ways to do it depending on what else you are doing. Here's one way to make your code work
Code:
If ActiveDocument.Tables(1).Cell(1, 1).Range.Text = "Large" & Chr(13) & Chr(7) Then

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi Tony,

Well, fancy that!

Many thanks for your help

DaveFish
 
Just to follow up from Tony's post,
Code:
Dim strCellText As String
strCellText = ActiveDocument.Tables(1).Cell(1,1).Range.Text
strCellText = Left(strCellText, Len(strCellText)-2)
will make the variable strCellText whatever is the text in the cell - with the end of cell marker stripped off. Now you can test against the variable.
Code:
If strCellText = "Large" Then

Gerry
 
Thanks Gerry,

That will help in simplifying my code as well.

Regards

Dave
 
Sorry Gerry,

Just a quick question. What's the best event to trigger my if statement. I wish this to happen when I leave the cell, but I have no AfterUpdate event!!

Regards

Dave
 
Hi Dave,

If your document can be implemented as a protected form, I'd go for that. That way, you can trigger your macro from the formfield. You do that by editing the form's properties and nominating the macro to run - on entry and/or or exit.

Also, depending on what you're trying to achieve with the test, you might instead be able to use a formula field that references the formfield's bookmark value in your protected form, instead of using vba, to update the second cell. You do that by editing the form's properties and setting the 'calculate on exit' property.

Cheers
 
macropod has a good answer. I have a question. What was triggering the code in your original post? Use that.

But if you use formfields, as macropod suggests, use the Calculate on exit, and an onExit macro.

Gerry
 
Hi Gerry,

Why would Dave need both 'Calculate on exit' and an onExit macro? Surely either alone would do?

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top