Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run this macro for more than 1 label

Status
Not open for further replies.

calvinb

Technical User
Jan 22, 2012
47
CA
I have a macro that works to format a formfield in a label but I want to use it over and over for more than just one label. The macro puts a space between 6 characters in a postal code when the user presses tab to leave the field. It is called from the Exit event in the formfield in Word 2003 VBA. The 1st label is named "label_1", the second is "label_2", etc. The only way I can think of to have it work for each label is to write a macro for each label name but I'm sure there is an easier way. Can anyone show me how to code this?
Here's the macro I have now:

Code:
Sub FormatPostalCode()
'
' FormatPostalCode Macro
' Postal code consists of six characters with a space in the middle
' type: a1c4b5 and press tab changes it to A1C 4B5
'
ActiveDocument.FormFields("label_1").Select
strLeft = Left(Selection.Text, 3)
strRight = Right(Selection.Text, 3)
ActiveDocument.FormFields("label_1").Result = strLeft & "  " & strRight

End Sub


Thanks
 


Code:
Function FormatPostalCode(PostalCode As String)
    FormatPostalCode = Left(PostalCode, 3) & " " & Right(PostalCode, 3)
End Function
You supply the postal code in the function.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks, Skip but I can't figure out how to supply the function with the postal code!!
I put your function into a module in my document and in the Lost-Focus event for the textbox I wrote:
Code:
Sub TextBox5_LostFocus()
PostalCode = "testin"   'to supply PostalCode to function
FormatPostalCode        'to call the function
MsgBox PostalCode       'to see if it worked
End Sub

When I tab out of the textbox to trigger the above I get:
"Compile error
Argument not optional"
and it is highlighting the "FormatPostalCode" line in the TextBox5_LostFocus() subroutine

What am I doing wrong?
 

Code:
Sub TextBox5_LostFocus()
  with TextBox5   
    .Text = FormatPostalCode(.Text)
  end with
End Sub
You supply a value by entering it into the textbox and then exiting.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks, Skip. That works perfectly for one label. I have a full page of labels all set up the same. When I need labels, I open the label sheet template and fill in the name, address, Postal Code etc. fields. So label one has a textfield called Textbox 5 for it's Postal Code and it works fine when I make that label. When I make the second or third, etc. labels, it doesn't work any more because the textbox names are different. The Postal Code for the second label is "Textbox10", for third label it is "Textbox15", etc. The label increases by 5 for each new label.
How can I modify your code so it would work no matter which label I happened to pick on the sheet?
And just for my interest sake, if you were making a sheet of labels like I have, would you use ActiveX controls or Legacy formfield controls? I find the old formfield controls easier to work with. I can just click a checkbox in the properties to capitalize the whole field but in ActiveX that option is not available!!
I appreciate your help with this. I thought it would be a simple procedure but I've had a lot of problems trying to get it to work - caused mostly by my lack of knowledge!! I would love to take a comprehensive course on VBA for Word from beginner to expert but can't seem to find anything online.
 



Are you just doing a Lable MailMerge?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
No, where I work, I have to mail stuff to clients. Usually only a few a day so I made a full page of mailing labels that automatically capitalize everything (That's the way the Canadian post office prefer addresses!!) and I press the space bar to put the space between the first 3 and last 3 digits of our postal codes.
Since I like to automate everything and want to learn VBA, I decided to let the VBA code put in the space for me when I tabbed out of the Postal Code field. But since I'm not as young as I used to be, I'm having trouble figuring it all out!!
My original label sheet was using the old legacy formfields but I assumed newer was better and am trying to use the ActiveX fields now but I'm not so sure, they are better than the old formfields. I have Word 2010 which has yet another type of textfields (Content Controls) but I can't see any way to format them for capitals or how to use them with VBA so I tried ActiveX.
So, I'm not doing a mail merge. Just a few labels at a time but was mostly interested in learning a bit about VBA.
 


My original label sheet was using the old legacy formfields but I assumed newer was better
Well there is not! Mail Merge is still the best way. Your SOURCE DATA ought to be changed to include the space in the Postal Code.

Then the only post processing, once lables are produced, is to FONT > FORMAT UPPERCASE.

I'm a big VBA guy, but not to replace a really good feature in MS Word like MailMerge!!!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,
I have never used mail merge. I always regarded it as being for large mailings not just for the occasional 3 or 4 labels. I am going to have a look at it now that you suggest it. Perhaps, I'm making things more complicated than there is any need for. When I print just a few labels, I am reusing sheets of labels so right off, I am wondering how you would pick which label to start printing to with a mail merge but there may be a way. I'll check it out... Thanks!!!
 



You can make selections in the Edit Recipient List.

You can SEE the actual merge values using Preview Results.

For instance, I have used MailMerge, some 15+ years ago, to print data from only ONE record.

It does not need to be a MASS mailing.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks, Skip. I will definitely have a look at mail merge. The label document template I use is shared with other people in my office where all they have to do is type in the addresses and print the labels. The formatting is done automatically (Capitalization and space in postal code). So, unless, mail merge is this simple, my fellow workers would not use it.

So, I would be still interested in using your code in any label they may choose in the label sheet if that is possible.

Thanks for everything, Skip. I would like to have your knowledge!!!
 


The data goes in a TABLE. I'd make an Excel Workbook that can 'automatically' do those things while the data is entered.

Then you connect the workbook source to the MailMerge doc and you can put your addresses in any kind of label you like.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


Wow! I just re-read your last post. I'd bet that that address get TYPED 3-4 times in the course of business. If that is the case, WHAT A WASTE! There IS a better way!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top