Consulting

Results 1 to 8 of 8

Thread: Solved: Data Appearing in Wrong Table Cell

  1. #1
    VBAX Regular
    Joined
    Oct 2005
    Location
    Westhoughton, England
    Posts
    47
    Location

    Solved: Data Appearing in Wrong Table Cell

    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:

    [VBA]
    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
    [/VBA]

    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,
    Neil Belch
    Senior Software Technician
    CDL Production Services Ltd

    The views opinions and judgements expressed in this message are solely those of the author.
    The message contents have not been reviewed or approved by CDL.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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?

  3. #3
    VBAX Regular
    Joined
    Oct 2005
    Location
    Westhoughton, England
    Posts
    47
    Location
    InsertTextAtBookmark is called from the routine PersonsAge below:

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

    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:

    [VBA]Application.Run "modClient.PersonsAge", "AGE"[/VBA]

    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,
    Neil Belch
    Senior Software Technician
    CDL Production Services Ltd

    The views opinions and judgements expressed in this message are solely those of the author.
    The message contents have not been reviewed or approved by CDL.

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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 ...[vba]ActiveDocument.Bookmarks(sAgeBookmark).Range.Text = iAge[/vba]would be more efficient.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Or even NO bookmark. If it is a cell in a table, why not write the text directly to the cell Range?
    [vba]ActiveDocument.Tables(1).Cell(1, 2).Range.Text = "Whatever"[/vba]

  6. #6
    VBAX Regular
    Joined
    Oct 2005
    Location
    Westhoughton, England
    Posts
    47
    Location
    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.
    Last edited by Belch; 12-14-2005 at 04:29 AM. Reason: Adding comment
    Neil Belch
    Senior Software Technician
    CDL Production Services Ltd

    The views opinions and judgements expressed in this message are solely those of the author.
    The message contents have not been reviewed or approved by CDL.

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You may also want to try:
    [vba]ActiveDocument.Bookmarks(sAgeBookmark).Range. _
    .InsertAfter Text:= iAge [/vba]

  8. #8
    VBAX Regular
    Joined
    Oct 2005
    Location
    Westhoughton, England
    Posts
    47
    Location
    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.
    Neil Belch
    Senior Software Technician
    CDL Production Services Ltd

    The views opinions and judgements expressed in this message are solely those of the author.
    The message contents have not been reviewed or approved by CDL.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •