Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi all,

    I've got a template that makes documents with a bunch of fields in 'em.
    One set of nested fields looks like this:

    { if { DOCPROPERTY Unbounds \* MERGEFORMAT } = "Y" { DOCPROPERTY UnboundsDate \* MERGEFORMAT } ""}

    That is, I have two custom document properties, and if the first one is true, the field drops in a date set by the second one; if it's false, if drops in nothing.

    Now I'm updating the template so that we want it to drop in an alternative bit of text if the first property is true. For new documents based on this template, obviously I can just type that text in between the "" in that field.

    But (here's the question), I want to be able to update old documents already made from the previous version of this template. So I need the code to look at that field, find the "" in it, and replace with "some text". How do I do that (especially without fouling up the other field that's nested within it)?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Here's a simple example; you can add bells and whistles:

    Code:
    Sub SearchFields()
      Dim f As Boolean
      Application.ScreenUpdating = False
      ' Store current display setting
      f = ActiveWindow.View.ShowFieldCodes
      ' Display field codes
      ActiveWindow.View.ShowFieldCodes = True
      ' Move to start of document
      Selection.HomeKey Unit:=wdStory
      With Selection.Find
    	.ClearFormatting
    	.Wrap = wdFindStop
    	' Search for DOCPROPERTY
    	Do While .Execute(FindText:="DOCPROPERTY")
    	  ' If found, ...
    	  Selection.Collapse Direction:=wdCollapseEnd
    	  ' ... search for "" after it
    	  If .Execute(FindText:="""""") Then
    		' Insert some text between the quotes
    		Selection.MoveLeft
    		Selection.MoveRight
    		Selection.TypeText "De dum de dum"
    	  End If
    	Loop
      End With
      ' Restore original display setting
      ActiveWindow.View.ShowFieldCodes = f
      Application.ScreenUpdating = True
    End Sub

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I entered your field code into a Word document, made sure that field codes were showing (Alft-F9) and then looked at ActiveDocument.Fields(1).Code - this had a value of
    if DOCPROPERTY Unbounds \* MERGEFORMAT  = "Y"  DOCPROPERTY UnboundsDate \* MERGEFORMAT  ""

    I replaced every " in this string with "" and then wrote a very short Macro that checked each field code for this value.

    Code:
    Sub UpdateFieldCode()
     Dim fld As Field
     Dim rng As Range
    	 
    	 For Each fld In ActiveDocument.Fields
    		 If fld.Code = _
    		 " if DOCPROPERTY Unbounds \* MERGEFORMAT  = ""Y""  DOCPROPERTY UnboundsDate \* MERGEFORMAT  """"" Then
    			 Set rng = fld.Result
    			 fld.Delete
    		 ' call your code that inserts the correct field codes here
    		 End If
    	 Next fld
     End Sub
    This seems to do the right thing.

  4. #4
    Star Lounger
    Join Date
    Aug 2001
    Location
    Hampshire, England
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for these prompt replies, guys. I think I would eventually have resorted to using Find, but imagined there might be some more elegant method. There are other "" that I don't want to touch, but fortunately I know exactly where this set of fields is (they're in a particular table cell) so I can replace the line

    Selection.HomeKey Unit:=wdStory

    with one to select the relevant cell only, (and drop the Do While loop, as within that cell the first one it finds will be the one I want) and then it does exactly what I wanted.

    Cheers, much appreciated.

Posting Permissions

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