Help with setting style & font with VBA

Help with setting style & font with VBA

Anonymous
Not applicable
3,188 Views
12 Replies
Message 1 of 13

Help with setting style & font with VBA

Anonymous
Not applicable
I've attached this code to be run from 2 different buttons, one for the
RomanD and the other for the RomanS. The co. I work for basically uses these
2 fonts in their drawings and I was trying to automate the switching back
and forth. The code works some of the time but not all the time and I can't
figure out what's wrong. Can anyone help?

The following code is in a module and the buttons work OK

'===============================
Option Explicit
Public SDimScale As Single

'-------------------------------------------------------------------

' ROMAN-D code

Public Sub SetFontRomanD()
Dim objTextStyle As AcadTextStyle
Set objTextStyle = ThisDrawing.TextStyles("standard")
objTextStyle.fontFile = "romand.shx"
SDimScale = ThisDrawing.GetVariable("dimscale")
objTextStyle.Height = SDimScale * 0.1875 'set the font to a
height of 3/16"
End Sub

' ROMAN-S code

Public Sub SetFontRomanS()
Dim objTextStyle As AcadTextStyle
Set objTextStyle = ThisDrawing.TextStyles("standard")
objTextStyle.fontFile = "romans.shx"
SDimScale = ThisDrawing.GetVariable("dimscale")
objTextStyle.Height = SDimScale * 0.09375 'set the font to a
height of 3/32"
End Sub
'===============================

TIA
DonB
0 Likes
3,189 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable
It seemed to work for me. New text objects came in at the 2 different sizes
and styles. Existing text with "Standard" style changed appearance but not
size.
I added "ThisDrawing.Regen acAllViewports" as the last line in the subs so
I could see changes right away.

When it's not working, what specifically does it do or not do?

James
0 Likes
Message 3 of 13

Anonymous
Not applicable
OK let me try to explain. Normally the text in the drawing is RomanS at
3/32" height but when we label a VIEW as an example the text is supposed to
be RomanD at 3/16" height. I would then switch back to RomanS at 3/32. The
text at RomanD doesn't seem to "STICK". I'll come back to the drawing later
and the RomanD text is 3/16" high but is then RomanS.

I would like the text to stay at 3/16" RomanD and the small text to stay at
3/32" RomanS

TIA

Don


"DonB" wrote in message
news:48407A5698F9FA80228A4C65460FF230@in.WebX.maYIadrTaRb...
> I've attached this code to be run from 2 different buttons, one for the
> RomanD and the other for the RomanS. The co. I work for basically uses
these
> 2 fonts in their drawings and I was trying to automate the switching back
> and forth. The code works some of the time but not all the time and I
can't
> figure out what's wrong. Can anyone help?
>
> The following code is in a module and the buttons work OK
>
> '===============================
> Option Explicit
> Public SDimScale As Single
>
> '-------------------------------------------------------------------
>
> ' ROMAN-D code
>
> Public Sub SetFontRomanD()
> Dim objTextStyle As AcadTextStyle
> Set objTextStyle = ThisDrawing.TextStyles("standard")
> objTextStyle.fontFile = "romand.shx"
> SDimScale = ThisDrawing.GetVariable("dimscale")
> objTextStyle.Height = SDimScale * 0.1875 'set the font to
a
> height of 3/16"
> End Sub
>
> ' ROMAN-S code
>
> Public Sub SetFontRomanS()
> Dim objTextStyle As AcadTextStyle
> Set objTextStyle = ThisDrawing.TextStyles("standard")
> objTextStyle.fontFile = "romans.shx"
> SDimScale = ThisDrawing.GetVariable("dimscale")
> objTextStyle.Height = SDimScale * 0.09375 'set the font to
a
> height of 3/32"
> End Sub
> '===============================
>
> TIA
> DonB
>
>
0 Likes
Message 4 of 13

Anonymous
Not applicable
I'm not too great with TextStyles, but instead of changing the fontFile of
"Standard", do you want two TextStyles ("D" and "S") that always point to
the same FontFiles. Then your macro would activate one or the other text
style?

Do you want to affect existing text Objects when you change, or just have
the new settings apply to text objects you subsequently create?

James
0 Likes
Message 5 of 13

Anonymous
Not applicable
Hi James

I only want to have the settings apply to text that I will be entering and
not anything that has been entered previously, when I change back to the
default which would be RomanS at 3/32" all the text I then enter should be
"S" at 3/32" until I switch back to "D" style again. I do not want to affect
any text already entered in the drawing.

TIA

DonB

"James Belshan" wrote in message
news:132E621BBE9590E0FD2F4F76248685ED@in.WebX.maYIadrTaRb...
> I'm not too great with TextStyles, but instead of changing the fontFile of
> "Standard", do you want two TextStyles ("D" and "S") that always point to
> the same FontFiles. Then your macro would activate one or the other text
> style?
>
> Do you want to affect existing text Objects when you change, or just have
> the new settings apply to text objects you subsequently create?
>
> James
>
>
0 Likes
Message 6 of 13

Anonymous
Not applicable
> The co. I work for basically uses these 2 fonts in their drawings ...

Don,

It would be good to know what manual method is used to switch between the
fonts. If I use the "style" command in ACAD to change the font name from
romand to romans.shx, all the text objects that have "Standard" style change
font. So does your company have separate styles defined for the two fonts?
Or is all text "Standard"?

After playing around some more with text, it seems to me like the best way
to get 2 different fonts to "stick" is to have 2 styles called romanD and
romanS, and have your macros set one or the other style as active...
0 Likes
Message 7 of 13

Anonymous
Not applicable
OK James Thanks for the help.

I did try setting a D and an S style but that didn't seem to help either.

Oh well! I'll keep on trying.

Regards

DonB
0 Likes
Message 8 of 13

Anonymous
Not applicable
Sub ToggleActiveTextStyle()

Dim SDimScale As Double
SDimScale = ThisDrawing.GetVariable("dimscale")

' if styles already exist, .Add just selects them
With ThisDrawing.TextStyles.Add("romanS")
.fontFile = "romanS.shx"
End With
With ThisDrawing.TextStyles.Add("romanD")
.fontFile = "romanD.shx"
End With

If ThisDrawing.ActiveTextStyle.Name = "romanS" Then
MsgBox "Changing to D"
ThisDrawing.TextStyles("romanD").Height = SDimScale * 0.1875
ThisDrawing.ActiveTextStyle = ThisDrawing.TextStyles("romanD")
Else
MsgBox "Changing to S"
ThisDrawing.TextStyles("romanS").Height = SDimScale * 0.09375
ThisDrawing.ActiveTextStyle = ThisDrawing.TextStyles("romanS")
End If

End Sub
0 Likes
Message 9 of 13

Anonymous
Not applicable
this code (previous post) seems to work for me. As I toggle back and
forth, and create text, the old text never changes its height or font. One
consideration would be whether you care if you have different text styles.
To me it doesn't seem like that big of a deal, after seeing more about how
they work.

Some stuff I learned about Text styles...
1 - changing the font file of a text style automatically changes the display
font of all text that uses the style.
2 - changing the height of a text style does not change the height of
existing text that uses the style.
2a - however, changing the style of a text entity (in the Properties window)
causes its height to change to that of the style.

about #1, this is why it seems like your company must already be using
multiple text styles in order to make different fonts.

Good luck,
James
0 Likes
Message 10 of 13

Anonymous
Not applicable
James

Thanks a lot for your work on this and your help is certainly appreciated.
The trouble with where I work is that the "Standard" is loosely controlled
with everyone getting there by their own method. But you're supposed to
follow it in your drawings.
I will try out your code and report back.

Best Regards

DonB
0 Likes
Message 11 of 13

Anonymous
Not applicable
Hello again James

Well I tried your code but I can't get it to work. It seems to get stuck on
RomanS and won't change from that. When I get a chance I'll play around with
it some more. Thanks for all your help in trying to resolve this problem.

Regards

DonB

"DonB" wrote in message
news:48407A5698F9FA80228A4C65460FF230@in.WebX.maYIadrTaRb...
> I've attached this code to be run from 2 different buttons, one for the
> RomanD and the other for the RomanS. The co. I work for basically uses
these
> 2 fonts in their drawings and I was trying to automate the switching back
> and forth. The code works some of the time but not all the time and I
can't
> figure out what's wrong. Can anyone help?
>
> The following code is in a module and the buttons work OK
>
> '===============================
> Option Explicit
> Public SDimScale As Single
>
> '-------------------------------------------------------------------
>
> ' ROMAN-D code
>
> Public Sub SetFontRomanD()
> Dim objTextStyle As AcadTextStyle
> Set objTextStyle = ThisDrawing.TextStyles("standard")
> objTextStyle.fontFile = "romand.shx"
> SDimScale = ThisDrawing.GetVariable("dimscale")
> objTextStyle.Height = SDimScale * 0.1875 'set the font to
a
> height of 3/16"
> End Sub
>
> ' ROMAN-S code
>
> Public Sub SetFontRomanS()
> Dim objTextStyle As AcadTextStyle
> Set objTextStyle = ThisDrawing.TextStyles("standard")
> objTextStyle.fontFile = "romans.shx"
> SDimScale = ThisDrawing.GetVariable("dimscale")
> objTextStyle.Height = SDimScale * 0.09375 'set the font to
a
> height of 3/32"
> End Sub
> '===============================
>
> TIA
> DonB
>
>
0 Likes
Message 12 of 13

Anonymous
Not applicable
> It seems to get stuck on
> RomanS and won't change from that.

That shouldn't happen if you open a new DWG and run the code.
If you're running it in an existing dwg, the only reason I can think that
this might happen is that it already has style "romanS" in it, but with a
different case (i.e. ROMANs)
So, you could try making your IF-THEN case insensitive:

Msgbox "The active style is " & ThisDrawing.ActiveTextStyle.Name
If lcase$(ThisDrawing.ActiveTextStyle.Name) = "romans" Then
...

Good luck,
James
0 Likes
Message 13 of 13

Anonymous
Not applicable
OK James

I'll try that. I didn't know that I had to worry about the case. I'll let
you know.



Don

"James Belshan" wrote in message
news:208446B9C42363B5C2E080C2D22B06C5@in.WebX.maYIadrTaRb...
> > It seems to get stuck on
> > RomanS and won't change from that.
>
> That shouldn't happen if you open a new DWG and run the code.
> If you're running it in an existing dwg, the only reason I can think that
> this might happen is that it already has style "romanS" in it, but with a
> different case (i.e. ROMANs)
> So, you could try making your IF-THEN case insensitive:
>
> Msgbox "The active style is " & ThisDrawing.ActiveTextStyle.Name
> If lcase$(ThisDrawing.ActiveTextStyle.Name) = "romans" Then
> ...
>
> Good luck,
> James
>
>
0 Likes