Find replace text in a text box

O

Ocean

I need to use VBA to open a doc file find a placeholder in a text box and
then replace it with a date. Eg the text is XXXXXX. and this needs to be
replaced with date field. I can do this fine if the XXXXXX is in a normal
documnet, but when I try and do it for a text box it cant find it. Can anyone
please help?
 
J

Jezebel

Find and Replace finds text in a textbox if you set .Wrap = wdFindContinue
(equivalent to using 'ALL' in the Search drop down, as opposed to UP or
DOWN).

But an easier approach is to use a DocProperty field, then in code all you
need do is set the property and update fields.
 
O

Ocean

Thanks for this - can you explain this bit further :

But an easier approach is to use a DocProperty field, then in code all you
need do is set the property and update fields.

Many thanks
 
J

Jezebel

1. Go to File > Properties. On the Custom tab add a property called whatever
you like (eg 'MyDate'). Assign a dummy value.
2. In the body of the document, wherever you want this value to appear,
insert a DocProperty field: {DocProperty MyDate}
3. In your code, set the property using

ActiveDocument.CustomProperties("MyDate") = "22-Mar-2006"
ActiveDocument.StoryRanges(wdTextFrameStory).Fields.Update
 

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