PDA

View Full Version : Macro for switching Fields



emperoremp
06-19-2008, 06:06 AM
Does anyone know of a VBA code for MS Word relating to macros? I need to create a macro that changes from field to field in a template file. Normally I switch fields by pressing F11 and it goes to the next field from the previous field and so on. I need to make a macro that does it. Can anyone help me out here? PS it is for work.

Thanks

CreganTur
06-19-2008, 06:39 AM
I'm guessing that by 'fields' you mean Form Fields in the body of your document.

There has to be more you're trying to accomplish than just moving to the next formfield. Are you wanting this so that when a User enters data into a formfield that it will move to the next formfield that needs to be filled in?

If that's what you want, then you might want to look into using a UserForm that has textboxes for all of the fields that need to be filled in. That way, the user can fill in all of the info in one place, and then click a button on the userform that would populate the formfields with the required data.

If that's what you're after, then I've attached an example doc that shows how you can use textboxes on a userform, and then insert that text into the body of the letter. Although, this example uses Bookmarks instead of formfields.

HTH, and good luck:hi:

emperoremp
06-19-2008, 06:41 AM
Hi, that is not what I am looking for. Its hard to explain. Its the macro fields. Example in the template you have:

[hcp_title] [hcp_first_name]
[requesting organization's name)
(street),
(city), (province/state),
(country) (postal code)

When you hit F11, it selects hcp_title, and when you hit F11 it goes to hcp's first name and so forth. Does this help?

CreganTur
06-19-2008, 06:57 AM
When you hit F11, it selects hcp_title, and when you hit F11 it goes to hcp's first name and so forth. Does this help?

What is the purpose for moving between the formfields if you don't change their values or interact with them in some way? What are you trying to do to the formfields?

emperoremp
06-19-2008, 07:02 AM
Basically, the fields that don't have a macro button, I need to convert them to a macro button, and then need to add brackets.

fumei
06-19-2008, 09:23 AM
You guys are talking about (I think) two different things.

emperoremp, you must be more clear.

"Does anyone know of a VBA code for MS Word relating to macros? "

VBA code and macro code are the same thing. In other words, VBA code "relating to macros" is meaningless, as ALL macros use VBA code.

Also, the phrase "macro fields" does not mean anything. Macros are macros, Fields are fields.

Please explain, clearly and explicitly, what you mean by:

"Basically, the fields that don't have a macro button, I need to convert them to a macro button, and then need to add brackets."

On the face of it, this is not possible. I don't really know what you mean by convert them to a macrobutton.

What are the fields in the first place? Are they, or are they not, formfields? As you have not posted an actual document I do not know, but they do not sound like formfields.

Randy, I would be careful about having a Cancel button on a userform execute Application.Quit. Unloading the userform, yes. Exiting Word itself? Hmmmm. What if they had other documents open....which I did.

Bad dog.

CreganTur
06-19-2008, 09:53 AM
Bad dog.

http://img115.imageshack.us/img115/1296/cajmutn7xi2.jpg

Sorry. I hope you didn't loose much because of that.


Randy, I would be careful about having a Cancel button on a userform execute Application.Quit. Unloading the userform, yes. Exiting Word itself? Hmmmm. What if they had other documents open....which I did.

I changed the Cancel button to:
ActiveDocument.Close

fumei
06-19-2008, 10:44 AM
Since it is a Cancel button on a userform, why not just use it to unload the userform? What if - and I did - want to do nothing with the userform for now, i.e. Cancel the userform? I wanted to just cancel the userform, and look at the code.

If you have ActiveDocument.Close...then clicking Cancel dumps the file, and I can't look at the code.

I am not saying there are no valid reasons for closing the file - there certainly can be. But why in this case?

VERY cute dogs btw.

CreganTur
06-19-2008, 11:14 AM
I am not saying there are no valid reasons for closing the file - there certainly can be. But why in this case?


For the example document it doesn't make sense- I agree. I'll adjust that to an "Unload Me"

I can also agree that the name "cancel" does create the assumption that the UserForm will be canceled... so for the cases where I'm planning on closing the doc, I'll change it to Close.

The reason for this is is that I'm automating a series of legal form letters for my employer. They cannot change the body or formatting of the letter at all, outside of the options provided by the UserForm, which is why I'm going with a 'Close' button instead of a 'Cancel' button.


VERY cute dogs btw.

It's from some FW e-mail that I got a while ago- I just liked the pic.
I love Huskey's, but I've always felt that forcing one to live in NC is quite unfair, thanks to our hellish heat/humidity combo. Plus the Wife and I love Labs, so we rescued a Black Lab from the local animal shelter a couple years ago.

fumei
06-19-2008, 01:29 PM
"They cannot change the body or formatting of the letter at all, outside of the options provided by the UserForm"

So you are using protecting for forms?

CreganTur
06-19-2008, 01:37 PM
So you are using protecting for forms?

Not very good protection.

I'm still having a hard time with templates, for one thing. I made one of the finished letters as a template, but when I opened it the document_open event wouldn't fire to display the UserForm... plus there's still a lot about how to correctly use a template that I don't know.

Basically, my protection is that I've disabled the Save and SaveAs events. My close event clears all of the bookmarks that were populated earlier, and the very last line of code in my Close event sets the save flag to true, so that the "do you want to save" message doesn't appear. That way the user can only close the doc, and when they open it again it's ready for use.

Not the most optimal way to go about this I would imagine.

fumei
06-20-2008, 09:29 AM
But Randy, you do not need to use any actual formfields to use protecting for forms.

If you protect for forms, the Sections checked are locked to any edits by the user. You can have zero formfields and still use this.

I am not exactly sure what you are doing, but using Tools > Protect document does a good job of:

"They cannot change the body or formatting of the letter at all, outside of the options provided by the UserForm"

As for:

"I made one of the finished letters as a template, but when I opened it the document_open event wouldn't fire to display the UserForm"

If you have a template, and it is cloned to a new document, and THAT document is saved as a template (a .dot file), it will NOT have the Document_Open event that was in the first .dot file.

Why would you save a cloned document as a new template?

BTW: it seems the OP no longer cares about the question that was asked. emperoremp, if you have figured out what you wanted to know on your own:

1. Please mark this thread as Solved.
2. Tell us what you came up with as a solution.

CreganTur
06-20-2008, 10:34 AM
But Randy, you do not need to use any actual formfields to use protecting for forms.


Sorry, misunderstood what you meant.

If I protect the document, then won't that block the VBA behind my UserForm from being able to update the bookmarks in the body of the letter with values from the formfield?

Or would I work around that by unprotecting it via vba at beginning of the code's run, and them protecting it as a last step?


Why would you save a cloned document as a new template?


Maybe I'm misunderstanding you. I created this letter from scratch in .doc format. Then I saved it as a template using Save As ->Template.

To test it I then opened word, selected Templates on My Computer, and then selected the newly created template.

Should I have saved it origianlly, and worked on it as a template instead?

fumei
06-20-2008, 02:05 PM
Obviously I am not understanding.

"I made one of the finished letters as a template, but when I opened it the document_open event wouldn't fire to display the UserForm"

My bolding.

If you made it from scratch, did you write a Document_Open event? Normally, template files do not use Document_Open, as the purpose (generally) is to have something happen when a new document is cloned - i.e using Document_New.

You say Document_Open wouldn't fire. Unless you wrote one, it wouldn't.

"but when I opened it " Careful here. Do you mean open the template file itself?

Demo attached. The userform displays on Doc_Open. The file is protected for forms, but there are NO formfields.

The userform commandbutton code does indeed unprotect, and fills the bookmarks - and note that it fills the actual bookmark range, NOT after it. So it can be repeatedly filled/replaced. It then protects again.

You can bring up the userform again by click "Show Form" on the top toolbar.

Note that you can NOT, repeat not, edit anything in the document. Not even any text that is inserted into the bookmark ranges.

The ONLY way to change anything is via the userform, and the ONLY thing it can change is the contents of the bookmark ranges.

fumei
06-20-2008, 02:07 PM
BTW: I just figured out the image you have for yourself.

emperoremp
06-25-2008, 12:43 PM
This problem has been solved. Thanks for the help

fumei
06-25-2008, 12:49 PM
Do tell. It would be polite to post what your solution was.

CreganTur
06-25-2008, 12:59 PM
gerry,

thanks for the demo- haven't been able to adapt it for my word projects yet, but it does reveal some great information!


have something happen when a new document is cloned - i.e using Document_New.

I'll try that out as soon as I'm able to work on my word project again. Now that I think about it, using Document_New makes sense with Template since, like you stated, it's creating a "New" clone of the template, instead of opening it.


note that it fills the actual bookmark range, NOT after it.
I like that- increases reusability of currently open clone of template