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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word table references 1

Status
Not open for further replies.

Codman

Technical User
Nov 25, 2003
44
GB
I have a word doc with nine tables, some of which are used to show a reference to the filename. As there are other links to a database which need to be brokenon saving/printing, I have done this programatically, but need to retain the filename field in table 1, 3 & 7.
No problem if the number of tables were constant, but users have the ability to delete and add accordingly. I'm also having problems with bookmarks, as the originals documents were in word 97 and we're now on word 2003. It seems I can bookmark ok in 2003!


Here's my code Which deletes the bookmarks in 97 but not 2003??

Can anyone help?

CodMAn



Private Sub Document_Close()
If ActiveDocument.Name <> "Q452 Rev 0 master.doc" Then
ActiveDocument.Fields.Unlink
ActiveDocument.Tables(1).Cell(1, 3).Range.Select
Selection.Delete
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True

Selection.GoTo What:=wdGoToBookmark, Name:="AA"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True

Selection.GoTo What:=wdGoToBookmark, Name:="BB"
Selection.Find.ClearFormatting
Selection.MoveRight Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True


ActiveDocument.Save
End If

End Sub
 
Hi Codman,

I get the impression that you're killing the filename fields then trying to recreate them. Rather than using bookmarks, which your users might delete before the code gets run, why not simply preserve the filename fields:
Code:
Sub UnlinkFields()
Dim pRange As Word.Range
Dim oFld As Field
With ActiveDocument
    For Each pRange In .StoryRanges
        Do
            For Each oFld In pRange.Fields
                If oFld.Type <> wdFieldFileName Then oFld.Unlink
            Next
            Set pRange = pRange.NextStoryRange
        Loop Until pRange Is Nothing
    Next
End With
End Sub

Note that the above code will work on fields in headers, footers, text boxes, etc too. If you don't want that, a slightly different process could be followed, but the principle's the same.

Cheers
 
Hi Macropod,

I hear what you saying, and that makes sense. However, to complicate matters I only wish to preserve some of the fields! I'm pulling customer names and adresses from a database as external links, and also have {FILENAME} as a field in the header of each table. I want to break the links to the external D/B but maintain the {FILENAME} field which could appear in up to 4 tables. Does your code still apply, or do I have to re-think??

Rgs

CodMan
 
Hi Codman,

The example you gave in your original post implied that it was only Filename fields that you wanted to preserve.

Its now not clear whether you only want to preserve some filename fields, or all filename fields and some other fields as well.

The code I posted unlinks all fields in the document except for filename fields. This could be modified to work in tables only, and/or to exclude other fields from the unlinking also. Choosing between tables to determine which filename fields to unlink or preserve would take more work, and I'd suggest finding something other than bookmarks to base the process on.

Reagards
 
Hi Codman,

would it be possible that you mark the filename fields to delete with "\* Mergeformat" (or with "\* Caps" or something like this) and those to preserve without this - or vice versa?

Then you could get the distinction with Mid("Caps",...) on "ActiveDocument.Fields(1).code".

Rgds Markus
 
Hi Macropod,

Sorry for getting back earlier, but the code worked fanstastically. I confused the issue with finger trouble.

CodMan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top