Font Switching Macro?

N

New Greek Student

I'm very new at writing macros for Word and am hoping for some help.

I need to write a macro that will allow me to switch between my current font
and the Symbol font with one keystroke. I don't know how to store the
name of the current font as a variable. Following is what I believe would be
the simplest script, but I don't know how to make it work with VBA.

If Selection.Font.Name is not = "Symbol"
Then
OldFont=.Selection.Font.Name
.Selection.Font.Name="Symbol"
Else
.Selection.Font.Name=OldFont
End If

Thanks for your help.
 
L

Lerxst

I suspect you mean you need to toggle back and forth
between the two fonts. In this case storing to a variable
won't work because the variable will lose its value once
the script is done executing. Instead write it out using
the system.privateprofilestring method.

Sub ToggleSymbolFont
If selection.font.name <> "Symbol" then
System.PrivateProfileString
("ToggleSymbolFont","Memory","OriginalFont")
=selection.font.name
selection.font.name="Symbol"
Else
selection.font.name=System.PrivateProfileString
("ToggleSymbolFont","Memory","OriginalFont")
End Sub
 
C

Chad DeMeyer

Just to clear up potential confusion:

There are two kinds of "variable" in Word VBA. There is the kind that you
declare using Dim statements, etc., which have limited lifetime and scope.
Then there are the kind that are stored with a document, which are the kind
that Greg Maxey uses in the code he provided.
For a comprehensive discussion of ways to persist data between macros and
macro sessions, refer to "Storing values when a macro ends",
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/off2000/html/wohowstoringvalues.asp.

Regards,
Chad
 
G

Greg

Lerxst,

Your solution definately looks more polished than the one
I cobbled together :). I found it relatively easy to use
a docvariable to store the data, the problem I encountered
was if the user first applied the macro while using the
symblol font. The result was a ugly error code. With the
help of Dave Lett I found a way around this and it seems
to work well. At first I thought that you had solved that
issue without even trying. On further study I realized
that the value in the ini file is saved from document to
document. I noticed if the value is say TNR, the user
starts a new file with Courier as the default font,
manually applies symbol, then toggles, the toggle will go
to TNR. Just a nit I know. I posted an improvement of
the code Chad mentions in the VBA general ng. Have a look
if you are interested.
 
L

Lerxst

Glad an adequate solution has been found. From the
information given in the original post, it seemed to me
that it would be a toss up between using a doc variable
and using an ini file, but I can see that a doc variable
has its advantages for this macro.

I'm a bit confused though. Is Greg also the "New Greek
Student" who originally posted? And from some of the
comments it seems this discussion was simultaneously
discussed elsewhere. Has it? Just wondering, because the
discussion seems to have some gaps in it. I would be glad
to read the other comments that I think took place on
some other venue.
 
G

Greg

Lerxst,

Sorry for the confusion. In addtion to you, there are two
Greg's, a Chad, and a Dave involved. I am Greg Maxey
which appears when I use OE at home and just Greg when
posting from the NG portal at work. My first post never
displayed here in the NG portal, but I see in in the
GoogleGroups and in OE. That is odd. After posting I
went over to the VBA General group and ask a question on
my code related to an error that was generated. Subject
is "Help with object deleted error." Dave Lett jumped in
provided some great assistance and some cleaned up code.
Haven't heard anything from the OP since he first posted.

Thanks.
 
L

Lerxst

Okay. Thanks for clearing that up. By the way, I'm
curious: Is this for some sort of Greek language learning
tool? In the past I've tinkered with creating a set of
macros to automatically transform Greek terms into
English transliterations and vice versa. I lost hope
after encountering too many ambiguities between omega and
omicron, eta and epsilon. Accents and breathing marks
aren't fun either. Well if that's what you're up to and
you have success, I'd sure be interested in finding out
about it. My suspicion is that it would be necessary to
work from a giant database of whole terms, if one could
get his or her hands on such a thing, rather than trying
to decode terms character by character.

All the best.
 
N

New Greek Student

I am the original poster. Thank you, all of you, for your help. This macro
was for the purpose of being able to take notes in my Greek I class and
having the ability to make notes of the Greek vocabulary words as "Greek"
words and not transliterations.

I appreciate everyone's help. (BTW, it's probably a good thing that the
docvariables were used as opposed to ini files since I'm on Word for Mac
2004.

Thanks.
 
A

Alan Silver

Chad DeMeyer said:
For a comprehensive discussion of ways to persist data between macros
and macro sessions, refer to "Storing values when a macro ends",
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/off2000
/html/wohowstoringvalues.asp.

Chad,

Thanx for posting this link. I just battled with this issue last week !!

What is the difference between document variables and custom document
properties ? Is there any advantage to using one over the other ?

Thanx
 
C

Chad DeMeyer

Alan,

Both document variables and custom document properties can be displayed in a
document using field codes. However, document variables can only be
created, deleted, or changed using VBA; while custom document properties can
be created, deleted, or changed through the Word GUI
(File>Properties>Custom). I think there is also a way to somehow bind a
custom document property to a bookmarked range in a document, such that its
value is determined by the contents of the bookmarked range, but I have
always steered clear of that because of the fragility of bookmarks in a
multi-user environment.

Regards,
Chad
 

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

Similar Threads


Top