Table Cell Targeting

B

Benz

Hello,

I'm new to creating userfroms in Word. I did fairly well when working with
Excel-Userforms but now im having some trouble and would appreciate any help.

My question is how do you link a textbox with a specific cell in a word
Table? I need it so when the user pulls up the form in the textbox they see
whatever text is in the cell, they can then edit or type and it will then a
change event would change the cell's text to match.

I figured out how to use bookmarks to refer to a specific location but I
could not get it to load back in the textbox.

I appreciate any help, I hope it is a simple line of code im missing.

Ben Z.
 
J

Jay Freedman

If you're using a bookmark to transfer data from the userform to the table,
you can use the same bookmark to transfer it in the opposite direction.
However, there is a catch -- you have to make sure the bookmark still exists
after the first transfer (read
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm). For
example, if your userform code now says

ActiveDocument.Bookmarks("LastName").Range.Text = _
TextBoxLastName.Text

then you need to change it to

Dim oRg As Range
Set oRg = ActiveDocument.Bookmarks("LastName").Range
oRg.Text = TextBoxLastName.Text
ActiveDocument.Bookmarks.Add Name:="LastName", Range:=oRg

Now that the bookmark is available (assuming the user hasn't accidentally
deleted it), you can write this in the UserForm_Initialize() procedure:

If ActiveDocument.Bookmarks.Exists("LastName") Then
TextBoxLastName.Text = _
ActiveDocument.Bookmarks("LastName").Range.Text
End If

If you can't rely on the bookmark, but you know which table and which cell
contain the text, you can address it directly, for example:

Dim oRg As Range
Set oRg = ActiveDocument.Tables(1).Cell(1, 2).Range
oRg.MoveEnd wdCharacter, -1
TextBoxLastName.Text = oRg.Text

The MoveEnd is needed to exclude the cell marker from the range.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
O

old man

Hi,

Here is a bit of code - this assumes that the cell that contains the source
is called bkmrk1 and the textbox has a bookmark called t2.

Also note that I use a range object as soon as I can (may be able to use the
range object sooner...) and that after I select the cell I decrease the
selection by one character so I don't copy the cell itself (all the cell info
is in the last character) just the contents.

Don't address a cell by its offset because this can change as rows/columns
are added or removed.)

Dim r1 As Range
Selection.GoTo What:=wdGoToBookmark, Name:="bkmrk1"
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Set r1 = Selection.Range
r1.End = r1.End - 1
r1.Copy

ActiveDocument.Bookmarks("t2").Range.Paste

old man
 
O

old man

Hi,

Here is better code then the one I just posted - this will always preserve
the target bookmark (taken from MVP Word site:
http://word.mvps.org/FAQS/MacrosVBA/InsertingTextAtBookmark.htm)


Dim r1 As Range
Selection.GoTo What:=wdGoToBookmark, Name:="bkmrk1"
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Set r1 = Selection.Range
r1.End = r1.End - 1

Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("t2").Range
BMRange.Text = r1.Text
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "t2", BMRange


old man
 
B

Benz

Thank you, Jay and "Old Man" , this works great!

Jay Freedman said:
If you're using a bookmark to transfer data from the userform to the table,
you can use the same bookmark to transfer it in the opposite direction.
However, there is a catch -- you have to make sure the bookmark still exists
after the first transfer (read
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm). For
example, if your userform code now says

ActiveDocument.Bookmarks("LastName").Range.Text = _
TextBoxLastName.Text

then you need to change it to

Dim oRg As Range
Set oRg = ActiveDocument.Bookmarks("LastName").Range
oRg.Text = TextBoxLastName.Text
ActiveDocument.Bookmarks.Add Name:="LastName", Range:=oRg

Now that the bookmark is available (assuming the user hasn't accidentally
deleted it), you can write this in the UserForm_Initialize() procedure:

If ActiveDocument.Bookmarks.Exists("LastName") Then
TextBoxLastName.Text = _
ActiveDocument.Bookmarks("LastName").Range.Text
End If

If you can't rely on the bookmark, but you know which table and which cell
contain the text, you can address it directly, for example:

Dim oRg As Range
Set oRg = ActiveDocument.Tables(1).Cell(1, 2).Range
oRg.MoveEnd wdCharacter, -1
TextBoxLastName.Text = oRg.Text

The MoveEnd is needed to exclude the cell marker from the range.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top