Hi Sabaka,
I don't mind solutions that aren't particularly elegant -- I use 'em
all the time <g> -- but unfortunately this one doesn't work in all
situations.
The problem is that Word doesn't treat "decorative fonts" -- Symbol,
the Wingdings family, and a number of others -- the same way it treats
"normal" fonts when you use the Insert > Symbol dialog. There's a good
discussion of this at
http://word.mvps.org/faqs/macrosvba/FindReplaceSymbols.htm. The bottom
line is that if you run your macro and insert something from a
decorative font, the character it places at the beginning of the
document is always a left parenthesis regardless of what character you
selected in the dialog.
To work around this, you need to use the technique indicated in the
FAQ article: capture the character number and font name from the
dialog and store them separately. The demo macros below show how to do
that.
As for where to store the info, there are several possibilities, and
the choice depends on what functionality you want to provide.
- Storing them in the body of the document is generally not good for
the reasons you've already given.
- You can store them in document variables, as the demo below does.
That way they're "invisible" except to macros. Doc variables are
stored in the file when you save the document. This choice means that
each document has its own "most recent symbol", which could be useful
in some scenarios (e.g., if you send the document to someone else) but
probably isn't what you want.
- You could instead use the System.PrivateProfileString property to
store the info either in the registry or in a separate text file. This
makes the "most recent symbol" available system-wide (but it doesn't
travel with a document that you send to someone). This is probably the
best choice.
Sub InsertSymbol()
'
'Intercepts Word Command for Inserting a symbol,
'proceeds with the insert symbol command, then
'stores info about the symbol in doc variables
'for later use
Dim symb As String, fnt As String
Dim MyRange As Range
' Insert a special character
With Dialogs(wdDialogInsertSymbol)
.Show
Selection.MoveStart unit:=wdCharacter, Count:=-1
symb = CStr(.Charnum)
fnt = .Font
Selection.Collapse wdCollapseEnd
End With
' Place the symbol's charnum & font in document variables
' where they can be read by another macro that needs to know
' the value of the most recently used symbol
ActiveDocument.Variables("SymbolMRU").Value = symb
ActiveDocument.Variables("SymbolMRUfont").Value = fnt
End Sub
' the "other macro", for example...
Sub InsertRecentSymbol()
On Error GoTo bye
Selection.Collapse wdCollapseEnd
Selection.InsertSymbol _
CharacterNumber:=CLng( _
ActiveDocument.Variables("SymbolMRU").Value), _
Font:=ActiveDocument.Variables("SymbolMRUfont").Value, _
Unicode:=True
Selection.Collapse wdCollapseEnd
bye:
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP
Here is a thought.
If the special symbol is stored in a place that isn't readily accessible,
why not store it yourself in a location of your choosing? The code which
follows stores it at the start of the document. It would require that you
remember to manually delete the symbol at the end of your session (though I
suppose you could intercept a close event and do it with code then).
It's not a partiuclarly elegant solution. I would like to know how one can
save a variable after it goes out of scope when the Macro finishes so that it
could be used by another module (and so you don't have store it in the
document.)
Sub InsertSymbol()
'
'Intercepts Word Command for Inserting a symbol,
'proceeds with the insert symbol command, then
'places copy of the symbol inserted at start of
'document for later reuse
'
Dim symb As String
Dim MyRange As Range
' Insert a special character
Dialogs(wdDialogInsertSymbol).Show
' Assign the special char to the variable symb
Set MyRange = ActiveDocument.Range(Start:=Selection.End - 1,
End:=Selection.End)
symb = MyRange.Text
' Place a copy of the symbol at the start of the document
' where it can be read by another Macro that needs to know
' the value of the most recently used symbol
With ActiveDocument.Content
.InsertParagraphBefore
.InsertBefore symb
End With
End Sub
KR said:
Does anyone know where the information for the most recently used symbol can
be accessed via VBA? It must be stored somewhere, because it is the leftmost
symbol of the recently used symbols in the insert symbol dialogue box.
I often use the same symbol lots of times in a single document, so I want to
assign a macro to a custom button that simply inserts whatever the last
symbol was that was inserted in the document. It'll still be better than
using the keyboard shortcut, for example [<alt>+0176], assuming I could even
remember the keyboard shortcut... :-/
Thanks!
Keith