Macro to change character spacing

J

Jon

I'd like to write a macro that allows the user to alter the character
spacing of characters that have been previously selected. When I run the
macro, I'd like a small box to appear where the user can type in the
required character spacing. How do I make a box appear, and how do I get it
to disappear when the user presses the Enter key, and how do I read the data
that was typed in. Also, presumably this would be a string. How do I convert
it to a number in VBA?
 
J

Jay Freedman

Jon said:
I'd like to write a macro that allows the user to alter the character
spacing of characters that have been previously selected. When I run the
macro, I'd like a small box to appear where the user can type in the
required character spacing. How do I make a box appear, and how do I get it
to disappear when the user presses the Enter key, and how do I read the data
that was typed in. Also, presumably this would be a string. How do I convert
it to a number in VBA?
Hi Jon,

There's no point reinventing the wheel here. Use the Format > Font
dialog, which already has the needed fields and will automatically
change the spacing when the user clicks OK. Here's all the code you
need:

With Dialogs(wdDialogFormatFont)
.DefaultTab = wdDialogFormatFontTabCharacterSpacing
.Show
End With
 
J

Jon

Thanks Jay. That is one way, but I'm looking for something that doesn't
confuse the user. I only want to have one box for the user to enter the
data. I'd also like to use this method to change other things in the future.
 
J

Jay Freedman

Hi Jon,

OK, in that case, what you want is to create a userform (a custom
dialog box) as described at
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm, along with a
macro that calls the userform's .Show method. The form should contain
a text field for entering the number, a label to tell the user what to
enter, an OK button, and probably a Cancel button. To make the OK
button respond automatically to the Enter key, set its Default
property (in the Properties pane) to True. Similarly, to make the
Cancel button respond to the Esc key, set its Cancel property to True.

The code for the OK button's Click event should first make sure the
text in the field is a number, and then use that number to set the new
value of Selection.Font.Spacing. You're correct that the user's entry
is a string that should be converted to a number (data type Single),
using the CSng function.

Assuming the OK button is named cmdOK and the text field has the
default name TextBox1, the code would be

Private Sub cmdOK_Click()
If IsNumeric(TextBox1.Text) Then
Selection.Font.Spacing = CSng(TextBox1.Text)
Unload Me
Else
MsgBox "Please enter a number"
End If
End Sub

If you want to get fancy, you can also check the value of
TextBox1.Text to make sure it's within a reasonable range before using
it.

--
Regards,
Jay Freedman http://aspnet2.com/mvp.ashx?JayFreedman
Microsoft Word MVP

Jon said:
Thanks Jay. That is one way, but I'm looking for something that doesn't
confuse the user. I only want to have one box for the user to enter the
data. I'd also like to use this method to change other things in the future.
 
J

Jon

Thanks Jay, I'll try that.


Jay Freedman said:
Hi Jon,

OK, in that case, what you want is to create a userform (a custom
dialog box) as described at
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm, along with a
macro that calls the userform's .Show method. The form should contain
a text field for entering the number, a label to tell the user what to
enter, an OK button, and probably a Cancel button. To make the OK
button respond automatically to the Enter key, set its Default
property (in the Properties pane) to True. Similarly, to make the
Cancel button respond to the Esc key, set its Cancel property to True.

The code for the OK button's Click event should first make sure the
text in the field is a number, and then use that number to set the new
value of Selection.Font.Spacing. You're correct that the user's entry
is a string that should be converted to a number (data type Single),
using the CSng function.

Assuming the OK button is named cmdOK and the text field has the
default name TextBox1, the code would be

Private Sub cmdOK_Click()
If IsNumeric(TextBox1.Text) Then
Selection.Font.Spacing = CSng(TextBox1.Text)
Unload Me
Else
MsgBox "Please enter a number"
End If
End Sub

If you want to get fancy, you can also check the value of
TextBox1.Text to make sure it's within a reasonable range before using
it.
 

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