Set the filename for a document created from a template

D

Don Wilkinson

I'm new to macros and the like, so please be gentle to me and explicit with
me. :)

I have a Word template that is a form my users will fill out. I am planning
to have them send the complete form to me in email.

This form contains a text field box with the bookmark name "EmpName".

When they click on the shortcut to my template, they of course get a new
Word doc based on the template, with the temporary document name "document#".
If they then use File --> Send To --> Email Recipient (As Attachment)
feature, the file attachment is named "document#".

I want a way to grab the data from the EmpName field and use that to
generate the filename given when the document is attached. For example "User
Setup - EmpName".

I understand I may need to have the user press a key or Save the document
first.

I imagine I will need a bit of info about the Properties of the Text field
so that it "calculates on exit" or whatever. But, I KNOW I'll need the
script bits to do the filename updating, etc.

I really hope you can help me, of course, but I'm also excited that I will
learn something too!

Thanks in advance,
Don
 
T

td

Hello Don
I will leave the experts to answer your question but: Why use EmpName? Why
not use the user's Windows logon name? Using the logon name means the user
does not have to type./select anything.
Cheers
td
 
D

Doug Robbins - Word MVP

Use the following code in a macro that is run on exit from the "EmpName"
form field

ActiveDocument.SaveAs ActiveDocument.Bookmarks("EmpName").Result & " -
completed form"

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Don Wilkinson

TD,
the "EmpName" field refers to a thrid party, not the current user.

Thanks,
Don
 
D

Don Wilkinson

Doug,
thank you. I will go and try it. Assuming I can figure out the basics of
using code from Word form... That's my homework.

I actually expect myself to learn some things on my own. Not to have
everything handed to me. ;-)

Don
 
D

Don Wilkinson

Hello Doug,

I created a macro in my form template. I tested the new form and when I
enter data into the EmpName field and move to the next field (exit) I get
this error:

"Compile Error: Method or Data member not found."

and the ".Result" part of the macro code is highlighted in the VB debug
window. This is Word 2003SP2, btw. I don't have a reference to supported
methods; is .Results "good"? :)

Here is my code block for the macro in TemplateProject-Modules-Module1:

Sub SaveFormWithName()
ActiveDocument.SaveAs ActiveDocument.Bookmarks("EmpName").Result & "
- completed form"
End Sub

Note: when I pasted your code sample, I added indent. Also, the editor
wordwrapped the string and added an extra doublequote to the end of the line,
which looked to me like it would break the string. So, I 'corrected' it to
what you see above.

If I get it working, I'll probably recode a bit more to something like;

Sub SaveFormWithName()
ActiveDocument.SaveAs & "UserSetupRequest-"
ActiveDocument.Bookmarks("EmpName").Result
End Sub
 
D

Doug Robbins - Word MVP

The
ActiveDocument.SaveAs ActiveDocument.Bookmarks("EmpName").Result & "
- completed form"
needs to be all on one line or have a Visual Basic line break character
inserted like

ActiveDocument.SaveAs ActiveDocument.Bookmarks("EmpName").Result & _
" - completed form"

Note that there is a space between the ampersand and the underscore
character.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Don Wilkinson

Hello again, Doug,

I very much appreciate your time. Having been in you position many times, I
know that it's often diffcult, and rarely attempted, to help someone with
their script problems. It's often something as simple but as elusive as a
misplaced space character and the like. :)

In this case, I think that my post may have workwrapped in such a way as to
mislead you you as to the cause of the error I reported or something. I
don't think I am having a syntax problem due to not using the proper
'continued on next line' character or the like.

As I specifically showed before, the debugger is complaining about the
*method* and it's highlighting ".Result".

I reemphasize this because my script was all on one line - linewrapping does
not seem a likely cause to my problem. Furthermore, I've tried changing the
code to match what you gave me in format and got the same error.

Finally, I changed the code to:

Sub SaveFormWithName()
ActiveDocument.SaveAs ActiveDocument.Bookmarks("EmpName").Result
End Sub

And got the SAME error. Next idea? :)

Thanks,
Don
 
D

Doug Robbins - Word MVP

Hi Don,

Sorry the problem is an error in the code that I gave you. It should have
been:

ActiveDocument.SaveAs ActiveDocument.FormFields("EmpName").Result etc.

The .Result is a property of a FormField, not a bookmark
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - 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