Ask and Bookmarks and replacing text values

G

Gordzilla

I'm relatively new to VBA and have inherited about 700 documents created from
a protected template.

I need to change the value of a date on all 700 documents.
I originally wrote a macro that opened each file, and did a search and
replace.
This worked...or so I thought. If you look at a document on the screen, the
new date appears, if you do a print preview or print the document it reverts
back to the original date.
Turns out the date in question is entered using a user dialog box.

If I toggle Field codes I can see the following.
{Ask startdate "Enter Start Date (yyyy-mm-dd)" \d "yyyy-mm-dd" \*
MERGEFORMAT} {REF startdate \* MERGEFORMAT}

From what I understand this is a bookmark field.
Can someone assist me with code to change the date that is stored in the
field with the new one?
I have two other fields that require similar changes too, that are in the
exact coding format.



Gordzilla
 
G

Graham Mayor

If you are content to use replace to fix the new date then copy the field
{SET startdate "2006-09-26"} to the clipboard (after first changing the date
there to whatever you want the new startdate to be).
Then in your replace routine replace ^d Ask startdate with ^c.
Ensure the display of fields is toggled to show the field construction and
after the replacement you had better add a routine to update the fields also

Dim sView As String
sView = ActiveWindow.View.ShowFieldCodes
ActiveWindow.View.ShowFieldCodes = True

<run your replace function here>

ActiveWindow.View.ShowFieldCodes = sView
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview

should suffice.

Note that if there are other ASK fields in the document updating the fields
will prompt you for the new information in the ASK field.

You may find http://www.gmayor.com/batch_replace.htm useful


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Gordzilla

I would prefer to be able to select the specific bookmarks and change them as
I can't do a simple search and replace any more because the 'old' date value
is now elsewhere in the document and shouldn't change.
As for "ASK" fields there about 50 so I would prefer if they don't prompt.
 
G

Graham Mayor

50 Ask fields? Which genius thought that one up?

This 'simple' date change is going to be a major undertaking. I can't see
how we can proceed with this without seeing the document(s). Without an in
depth knowledge of the structure of the document it is not possible to see
how the various fields interact.

If you cannot update the fields, then you are also going to have to locate
the cross references to the StartDate bookmark and update them individually.
It might be simpler to look for all the other fields in the document and
convert them to text so they cannot change. All depends on what you want to
achieve with these documents.

The following code will unlink all the fields but the startdate fields.

Dim iFld As Integer
For iFld = ActiveDocument.Fields.Count To 1 Step -1
With ActiveDocument.Fields(iFld)
If InStr(1, .Code, "StartDate") = 0 Then
.Unlink
End If
End With
Next iFld

You can then use my previous suggestion to change the startdate content.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Gordzilla

I agree, but unfortunately this is what I'm stuck with. I'm afraid I cant'
post the documents as the format and content a confidential to our client.
I can explain a little more about the document.
There are no external data sources, everything is stored within each word
file.
Based on the field code, I assumed this is a bookmark, correct me if I'm
wrong.

{Ask startdate "Enter Start Date (yyyy-mm-dd)" \d "yyyy-mm-dd" \*
MERGEFORMAT} {REF startdate \* MERGEFORMAT}

The start date and other "ask" fields are not cross reference anywhere else
within the document. They only appear in the one location where the above
code is.
I know that the field is number 9 in the field index and if I use a msgbox
to display the item using:

MsgBox ActiveDocument.Fields(9).Result

the startdate value is displayed. I was hoping that there would be a simple
method.
like ActiveDocument.Fields(9).Result = "2007-01-01"

Sorry if I'm not providing enough info, but I haven't had to deal with VBA
and fields before.
 
G

Graham Mayor

Startdate is certainly a bookmark - you Ask for the bookmark content then
write that content with the REF field.

The problem is all the other ASK and REF fields which you want to leave as
they are. It was for this reason I suggested in my last post that they be
unlinked and thus converted to text, so it doesn't matter if the fields are
updated. The code I posted earlier will do that, leaving only the startdate
field to deal with.

You cannot do search and replace into fields readily - though you can write
fields with vba. I therefore suggested replacing the ASK field with a SET
field to incorporate the new date that you wanted, by first copying that SET
field to the clipboard and use the replace option to replace the ASK field
with the clipboard content and then update the fields to make the REF field
show the new startdate.

Both pieces of code can be incorporated in a batch processing macro.

I would try it out on a couple of copy documents in a folder of their own ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top