PDA

View Full Version : Solved: Data Appearing in Wrong Table Cell



Belch
12-12-2005, 05:00 AM
Hi all,

I have a table in a Word template with bookmarks in a number of the cells.
When a document is created from the template, there is some VBA code that should insert text in the cell where a certain bookmark is.
However for one cell this is not happening. Instead, the text is being inserted in the top left (i.e. first) cell of the table (where there is no bookmark and therefore no text should appear).

Does anyone know why this might be happening? I have provided the code below:


Public Sub InsertTextAtBookmark(ByVal sBookmark As String, _
ByVal sText As String)

ActiveDocument.Bookmarks(sBookmark).Select
Selection.SelectCell
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText (sText)

End Sub


In most cases this does not happen, only on a couple of the templates.
Any info is appreciated, let me know if you need any other info.

Thanks,

fumei
12-12-2005, 01:01 PM
You need to provide more code than that. We need more details.

What fires InsertTextAtBookmark?

Would it be possible to supply a file for us to look at?

Belch
12-13-2005, 06:19 AM
InsertTextAtBookmark is called from the routine PersonsAge below:


Public Sub PersonsAge(sAgeBookmark As String)
Dim iAge As Integer
iAge = modDate.DateDiff("STARTDATE", "DOB", "yyyy")
modSpecial.InsertTextAtBookmark sAgeBookmark, iAge
End Sub


As you can see InsertTextAtBookmark is called from a different module to PersonsAge.

PersonsAge also uses DateDiff (also from another module), but I am fairly certain that DateDiff works ok as I have had no problems with it with other templates. Also it is a very large collection of code which I'd rather not paste on here (not sure how much of the code my company will allow me to show). All DateDiff does is return a number.

PersonsAge is called with the following code:

Application.Run "modClient.PersonsAge", "AGE"

This should insert the age of the person in the cell where the "AGE" bookmark is, but instead the age is appearing in the top left cell (which has no bookmark in it at all).

Hope this is enough info, again if you need any more let me know.
As mentioned above this code is for work and I'm not sure how much I can provide - I will try to give as much as I can!
Thanks,

TonyJollans
12-13-2005, 01:14 PM
Can you not step through the code and see which statement is moving the Selection?

I would, however, suggest that you don't use the Selection at all ...ActiveDocument.Bookmarks(sAgeBookmark).Range.Text = iAgewould be more efficient.

fumei
12-13-2005, 09:33 PM
Or even NO bookmark. If it is a cell in a table, why not write the text directly to the cell Range?
ActiveDocument.Tables(1).Cell(1, 2).Range.Text = "Whatever"

Belch
12-14-2005, 04:27 AM
Those tips work fine, thanks.

I tried outputting the age without using Selection and it seems to output fine.

However now, instead of the age figure appearing in the top left cell there is a zero appearing. I have checked for hidden bookmarks, and deleted the entire row and added a fresh one but it still appears.
Therefore I used the ActiveDocument.Tables(1).Cell(1,1).Range.Delete command and that gets rid of the erroneous number.

It's a bit of a quick fix but I don't have time really to look into it further.
Thanks again for the input from both of you.

I also meant to mention, the reason we are using bookmarks instead of inputting directly into the cell is that over time bits may be added to the documents which require cells to be split or altered and therefore maintaining it would take longer if code pointed to specific cells.

fumei
12-14-2005, 07:28 AM
You may also want to try:
ActiveDocument.Bookmarks(sAgeBookmark).Range. _
.InsertAfter Text:= iAge

Belch
12-14-2005, 07:45 AM
That would actually be very useful to other stuff in the documents. I didn't know much about using the Range property but it will help me quite a bit, cheers.