My form is not working in a Word Template, but it does in a normal

D

Dan

I made a word document that shows a custom form when it is opened. In that
form I add some data to fields (name, address...etc). Once I click the
submit button a function replaces some text fields on the document with my
user data.

ie. [First Name] gets replaced with Dan...etc

it works fine in the word document, but when I save the document as a
Template and try it doesnt replace the text. It shows the form, but when I
click submit it doesnt replace the text. Here is a part of the text replace
function. This is my first Word macro can someone give me a hand?\



With ThisDocument.Content.Find
.Text = "[First Name]"
.Replacement.Text = txtFirstName.Text
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchSoundsLike = False
.Execute Replace:=wdReplaceAll
End With
 
R

Rob

Are you opening a new document from the template? And the code executes but
doesn't work? Not sure but you might not want to use ThisDocument. Try
ActiveDocument or something like this and see if it works.

With Selection.Find
.Text = "[First Name]"
.Replacement.Text = txtFirstName.Text
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

I'm not sure why you're matching whole word and not sure if that is part of
the problem since mathing whole word isn't an option in the normal replace
dialog if there are non-alpha caharcters in the string.
 
D

Dan

I was opening it from the Template and it wasnt working. I tried the
Selection.Find (I think I had tried it last night , but it didnt work) and it
didnt work, but ActiveDocument.Content.Find worked... Thanks!

For anyone who comes across this later on, this is what worked for me.

With ActiveDocument.Content.Find
..Text = "[First Name]"
..Replacement.Text = txtFirstName.Text
..Format = False
..MatchCase = True
..MatchWholeWord = True
..MatchSoundsLike = False
..Execute Replace:=wdReplaceAll
End With
 

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