• Can’t add field to table cell (Word 97)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Can’t add field to table cell (Word 97)

    Author
    Topic
    #379471

    FYI…

    Problem (1) & (2) was solved by inserting the line

    tmpRange.Move Unit:=wdCharacter, Count:=-1

    before each …Fields.Add … line!
    Thanks (& find more info there) to clapping StuartR & his bananas post 127993, hidden (or didn’t I search good enough?) at the end of some other thread…

    Problem (3) was a tough one as it was hard to make texts & fields appear in the right order (something wrong with me defining ranges… but maybe with Word too, as it was pretty weird). For those interested: this is the result… Please leave some comment if you find it crap bummer/worth the effort clever!
    ================
    Public Sub testsub()

    Dim myRange, tmpRange As Range

    ‘Insert table in footer
    Set myRange = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
    myRange.Delete
    Set tmpRange = myRange
    myRange.Tables.Add tmpRange, 1, 3

    ‘Make table borders invisible
    myRange.Tables(1).Borders.OutsideLineStyle = wdLineStyleNone

    ‘Insert file name field in column 1
    Set tmpRange = myRange.Tables(1).Columns(1).Cells(1).Range ‘.Duplicate
    tmpRange.Move Unit:=wdCharacter, Count:=-1
    tmpRange.Fields.Add tmpRange, wdFieldFileName

    ‘Insert fields & text “Pg.x/y” in column 2
    Set tmpRange = myRange.Tables(1).Columns(2).Cells(1).Range ‘.Duplicate
    tmpRange.Move Unit:=wdCharacter, Count:=-1
    tmpRange.Fields.Add tmpRange, wdFieldSectionPages
    tmpRange.InsertBefore “/”
    tmpRange.SetRange myRange.Tables(1).Columns(2).Cells(1).Range.Start, myRange.Tables(1).Columns(2).Cells(1).Range.Start
    tmpRange.Fields.Add tmpRange, wdFieldPage
    tmpRange.InsertBefore “Pg.”

    ‘Insert date last saved field in column 3
    Set tmpRange = myRange.Tables(1).Columns(3).Cells(1).Range
    tmpRange.Move Unit:=wdCharacter, Count:=-1
    tmpRange.Fields.Add tmpRange, wdFieldEmpty, “SAVEDATE @ “”dd/MM/yy”””, PreserveFormatting:=True

    ‘Update fields
    ActiveDocument.Fields.Update

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #632019

      I’m writing a Word macro/procedure to add a three-columns-one-row table to the document’s footer containing filename (left), page x/y (middle) & date saved (right). I get a run time error message indicating that the fields.add method is not available.

      Can anyone tell me
      (1) what I’m doing wrong for having no access to the fields.add method in the footer table cell?
      => I assume there must be some incompatibility between the table cell as a range and the fields.add method.
      Why? Everything works when adding the field in the ‘body’ of the document (so fields.add works), when adding it without a table in the footer of the document (so footer range seems to be no obstacle either) or when adding text to footer table cells using e.g. tmpRange.text = “text”, and tmpRange.insertafter = “text” methods (so adding text to table cells works too).
      (2) a better way to get the result?
      => I could avoid the table and use one centered paragraph in which I insert something like left field – tabtabtab – middle fields & text – tabtabtab – right field, measuring a bit untill it always +- fits on one line. This might work if the field name length doesn’t vary (too) much (the other fields lengths remain stable). But that seems such a hassle fi there’s an alternative…
      (3) optional: how to add the mixture of text & fields for ‘page X/Y’ in the middle column… (I got all mixed up with how to properly use ranges & range methods here.)

      Any help is greatly appreciated. I lost already more hours than I can afford, so I’m ready for giving up the fight… but it’s too much a pity, being (probably) so close.
      So… thanks in advance!

      Hans

      P.S. My purpose: I want to standardise (+- uniform layout) 2000 word documents for delivery to our partners. I use an Access file which executes a Word macro (stored in the normal template) on multiple documents (using a table in which the file paths are previously automatically ‘loaded’ into). So it works great, if ( crybaby) the macro does…
      FYI: I only use the RANGE object as I’ll probably hide the Word application to speed up the processing and I’ve read (!?) that the SELECTION object might not work in these circumstances.

    Viewing 0 reply threads
    Reply To: Can’t add field to table cell (Word 97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: