PDA

View Full Version : Solved: Using InsertBefore together with InsertSymbol



bcn
11-09-2012, 05:22 AM
Hi,

My knowledge of VBA is close to null. Right now I am writing some macros and trying to make something out of the code I find scattered on the web. So please bear with me. :)

I'm trying to figure out how to use the InsertBefore together with the InsertSymbol methods in the same macro. I can use each separately, but don't know how to use them together. Tried a lot of things but they don't work.

So the first case would be something like this:

Sub AddParens()
.Selection.InsertBefore "("
.Selection.InsertAfter ")"
End Sub

The problem is I don't want to add a normal text, but a symbol. Something like this:

.InsertSymbol Font:="Wingdings 3", CharacterNumber:=-3971, _
Unicode:=True

I want to add this symbol before, and another one at the end of the selection.

I would apopreciate any help.

Thanks in advance!

Daniel

gmaxey
11-09-2012, 06:14 AM
Your might try:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = Selection.Range.Duplicate
oRng.Collapse wdCollapseStart
oRng.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True
Set oRng = Selection.Range.Duplicate
oRng.Collapse wdCollapseEnd
oRng.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True
End Sub

I can't remember how to determine the CharacterNumber for various symbols. How did you determine -3971?

bcn
11-09-2012, 07:21 AM
Your might try:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = Selection.Range.Duplicate
oRng.Collapse wdCollapseStart
oRng.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True
Set oRng = Selection.Range.Duplicate
oRng.Collapse wdCollapseEnd
oRng.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True
End Sub

I can't remember how to determine the CharacterNumber for various symbols. How did you determine -3971?
Greg,

Thanks a lot! Works wonderful!

I think I got the character number from running a macro to insert the special character. The closing one is 3972.

What would you do if you wanted the same macro to apply a style to the selected text and then add the two symbols?

To do the first thing I would use

Selection.Style = ActiveDocument.Styles("Button")

`Button' is the name of the style. But I don't know how to insert it into your code without damaging it.

Thanks a lot in advance,

Daniel

bcn
11-09-2012, 07:39 AM
Greg,

Thanks a lot! Works wonderful!

I think I got the character number from running a macro to insert the special character. The closing one is 3972.

What would you do if you wanted the same macro to apply a style to the selected text and then add the two symbols?

To do the first thing I would use

Selection.Style = ActiveDocument.Styles("Button")

`Button' is the name of the style. But I don't know how to insert it into your code without damaging it.

Thanks a lot in advance,

Daniel
OK, I added the

Selection.Style = ActiveDocument.Styles("Button")

on top of your code and it works perfectly well.

I really appreciate your help, Greg. Thanks a lot!

Daniel

gmaxey
11-09-2012, 07:49 AM
You're welcome. Glad I could help.

bcn
11-10-2012, 06:33 AM
Greg:

I have another one, similar to the InsertBefore and InsertAfter, if you don't mind.

I want to add double quotes before and after the selection, and apply a style to the selection plus the quotes. So at the beginning we have:

Hello, I am applying a style to this text that was normal until now

To make it look like this:

Hello, I am applying a style to "this text" that was...

(The only thing the style does is apply italics and it is called "GUI italics". Oh, and the double quotes should be curly quotes, not straight quotes.)

Thanks a lot again!

Daniel

gmaxey
11-10-2012, 08:06 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
With Selection
.InsertBefore Chr(147)
.InsertAfter Chr(148)
.Style = "GUI Italics"
End With
End Sub