Google Answers Logo
View Question
 
Q: Need MS Word macro - print document and save form data ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Need MS Word macro - print document and save form data
Category: Computers > Programming
Asked by: hamble-ga
List Price: $30.00
Posted: 12 Jun 2003 01:03 PDT
Expires: 12 Jul 2003 01:03 PDT
Question ID: 216361
I have a very simple MS Word 'form', consisting of three fields.

The purpose of this is to print a identity badge, where the operator
can change nothing of the design, except the personalised details that
go on the badge, i.e. name, company, status.

However, I want to be able to store the data for badges produced in an
external file. This could either be through an ODBC connection, or
simply a dump to a text file (with timestamp ideally). The user would
then have a customised button labelled 'Print & Save' which prints the
document, appends the form data to whatever file, then clears the form
ready for the next entry.

I'm sure this is trivial (if you know how!) but can't find anything to
help me quickly.

Can someone produce me such a macro?

Clarification of Question by hamble-ga on 12 Jun 2003 01:13 PDT
I can't work out how to set the expiry date on this, but I need to
resolve this in a hurry. Today would be nice <grin>!

Ben

Request for Question Clarification by hammer-ga on 12 Jun 2003 03:48 PDT
Does each badge make its own text file, or can each badge write a line
to an existing file? In other words, if you print three badges, do you
want three text files, or one text file with three lines?

- Hammer

Request for Question Clarification by hammer-ga on 12 Jun 2003 03:49 PDT
Also, which version of Word?

- Hammer

Clarification of Question by hamble-ga on 12 Jun 2003 04:08 PDT
Word 2000 (PC).

Ideally appended to the same file (maybe fields separated by
commas/tabs?), but separate files would also be ok (though presumably
harder).

The user should never be prompted with anything: the macro will know
which printer to print to and which file to dump the data to.

Thanks
Ben

Request for Question Clarification by hammer-ga on 12 Jun 2003 04:10 PDT
Sorry to keep asking questions, but there are a lot of variables in
this. It would actually help if you could upload your document to
somewhere where we could see it. The commands to actually do your task
are the same, but how to get the information out of the Word doc
varies depending on how you built it.

In lieu of that, when you say you have "fields", are you using: 
1. Word fields
2. Active X controls (like text boxes and combo boxes) on your Word
document
3. A UserForm

If you aren't sure, then describe how you made the field.

- Hammer
Answer  
Subject: Re: Need MS Word macro - print document and save form data
Answered By: hammer-ga on 12 Jun 2003 05:23 PDT
Rated:5 out of 5 stars
 
Hello Ben,

As you are in a hurry, for the sake of speed, I just built you an
example of how to do this. I used a UserForm, because it offers the
prettiest interface (for things like your Print Button) and the most
control. When you open the document, the UserForm will automatically
appear. If you fill in the fields and click Save and Print, the badge
will print to the default printer. You can look at the code behind the
UserForm and the Document by pressing Alt-F11 to open the Visual Basic
code editor.

The UserForm uses the PrintOut method to print the form. It uses the
Write method to write the file. The are Word Help pages on both these
commands.

I'm not sure what your proficiency level is, so please let me know if
you need anything clarified.

You can download the sample document from:
http://www.hammerdata.com/Google/BadgeMaker.doc

Again, please let me know if you need anything explained further. I'll
be glad to help.

- Hammer

Request for Answer Clarification by hamble-ga on 12 Jun 2003 05:49 PDT
Hello Hammer,

Thanks for your prompt response. As you say, probably the easiest
thing is to upload my document so far.
http://www.ordinate.co.uk/badge.doc

Firstly, let me just let you know that the document you uploaded is
completely blank - no content, buttons or macros.

As you can see, what I am using is word form fields. The form then
gets locked so that the user can only type in the appropriate places.
Note that there is a full-page graphic sitting behind all of this, but
for confidentiality reasons, I've had to remove this! (Yes, the page
is small - credit card size).

Ideally I need to make the MEDIA label at the bottom a variable bit of
text (options of MEDIA, GUEST, STAFF, VISITOR probably) though they
may want different (but set) colours for this text depending on the
choice. I'm not overly worried about this, because I can always set up
one document for each.

What I need to have happen is the user simply type onto the badge the
name and organisation of the individual (so they are typing on
something exactly as it will look printed), then click a button to
print the badge to the default printer, and the data gets saved.

Proficiency level is high - I'm a FoxPro programmer who has dabbled in
Word Automation - but have run out of time to solve this problem, and
surprisingly a web search gave me nothing!

Ben

Clarification of Answer by hammer-ga on 12 Jun 2003 05:58 PDT
I'll take a look at your document. The document I uploaded would look
blank if the UserForm did not appear. You must allow macros to run in
order to see the UserForm. Since you know what you're doing, look at
the code behind the document. It should be very obvious what should be
happening.

- Hammer

Clarification of Answer by hammer-ga on 12 Jun 2003 06:12 PDT
Ben,

I've looked at your document. You may want to take another look at my
sample before we proceed. The Word Form Fields that you are using are
severely limited and there is no nice-looking command button
available. You'd have to use a MACROBUTTON field. The UserForm method
allows for pretty formatting and far more control over your variable
text/color needs. You also don't have the document protection issues
that occur with the Form Fields.

I've uploaded a screenshot of what you should see in Word when you
open BadgeMaker:
http://www.hammerdata.com/Google/badge.gif

Please let me know how you want to proceed.

- Hammer

Clarification of Answer by hammer-ga on 12 Jun 2003 06:18 PDT
Just in case:

Here is the module for the Document itself:


Private Sub Document_Open()
    ' Show the UserForm when this
    ' document is opened.
    ShowBadgeMaker

End Sub

Private Sub ShowBadgeMaker()
    BadgeMaker.Show
End Sub

-------------------------------------------

Here is the module for the UserForm.

Private Sub cmdSave_Click()
Dim strFilename As String
Dim strTimestamp As String

    ' Change this filename to the one you
    ' want to use.
    strFilename = "d:\jobs\google\badges.txt"
    strTimestamp = Date
    
    Open strFilename For Append As #1
    Print #1, strTimestamp; Tab; txtName.Value; Tab; txtCompany.Value;
Tab; txtStatus.Value
    Close #1
    ' Make the button invisible so it doesn't print
    cmdSave.Visible = False
    ' Print the badge
    BadgeMaker.PrintForm
    ' Reset the form
    cmdSave.Visible = True
    txtName.Value = ""
    txtCompany.Value = ""
    txtStatus.Value = ""
End Sub


- Hammer

Request for Answer Clarification by hamble-ga on 12 Jun 2003 06:24 PDT
Hi Hammer,

Just had a quick look at the screenshot.

I can see the advantages of that, but unfortunately the person I did
this for last year had it as a word form such as supplied - it was
exactly them typing on to the picture of the badge.

As for a button, can't a toolbar button be created to run the macro? 

As for your sample, I now see the code (was looking in the macro ALT
F8 section at first. However, I still don't see how to start the
procedure? (I may be techinically proficient, but clearly my MS Word
skills are lacking, eh?!)

I appreciate all your help and swift responses on this.

Ben

Clarification of Answer by hammer-ga on 12 Jun 2003 06:44 PDT
The UserForm can be made to look exactly like the badge. As a FoxPro
user, the form editor should be quite familiar. I didn't know what
your badge looked like, so I just mocked something together.

1. Press Alt-F11 to open the Visual Basic Editor
2. The Project Tree should have (among other branches)
Project(BadgeMaker) - Forms - BadgeMaker
3. Double-click on BadgeMaker and the UserForm should appear in Edit
mode. The controls are very similar to all the other Microsoft form
editors. You can add colors, labels, fields, whatever you want until
it looks exactly like your badge.


If the UserForm is not opening when you open the Document, then your
Security settings are likely preventing macros from running.
1. Close all open documents, but do not close Word itself.
2. Choose Tools - Macro - Security.
3. If the Security Level is High, then that's your problem. Set it to
Medium.
4. Open BadgeMaker.doc.
5. When it asks you about macros, choose Enable Macros. 

Meanwhile, I'll try to write you a macro that you can attach to a
toolbar button, if you still want to go that route.

- Hammer

Request for Answer Clarification by hamble-ga on 12 Jun 2003 07:01 PDT
I see, yes security was set to high.

Ideally though (despite the flexibility that I will lose) I need to do
this as the form using the style in my sample... I need this to look
identical to last year (where they enter the details and click the
print button), but have the added extra of saving the data in the
background.

I presume I can acccess the elements of the form fields that I have
placed somehow, but don't know the code.

Clarification of Answer by hammer-ga on 12 Jun 2003 07:16 PDT
OK, here's a macro that you can attach to a toolbar button. It assumes
that the document is protected. If the document is not protected, then
it needs to be.

Tools - Protect Document - Forms

Note that the code unprotects, then reprotects the Document. This
should automatically reset the FormFields to their defaults.


Private Sub SaveThisBadge()
Dim strFilename As String
Dim strTimestamp As String

    ' Change this filename to the one you
    ' want to use.
    strFilename = "d:\jobs\google\badges.txt"
    strTimestamp = Date
    
    ActiveDocument.Unprotect
    Open strFilename For Append As #1
    Print #1, strTimestamp; Tab;
ActiveDocument.FormFields("Name").Result; Tab;
ActiveDocument.FormFields("Organisation").Result
    Close #1
    ' Print the badge
    ActiveDocument.PrintOut Range:=wdPrintAllDocument
    ActiveDocument.Protect wdAllowOnlyFormFields

End Sub


- Hammer

Clarification of Answer by hammer-ga on 12 Jun 2003 07:25 PDT
Ben,

One correction:
To attach this to a toolbar button, don't use Private. The declaration
should just be:

Sub SaveThisBadge()

- Hammer
hamble-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Excellent, very helpful and swift. Now he's shown me the answer, it
looks so easy, yet its so frustrating when you don't know the methods
or properties to use! Thanks Hammer!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy