Create new textstyle if one doesn't exist

Create new textstyle if one doesn't exist

Anonymous
Not applicable
2,475 Views
7 Replies
Message 1 of 8

Create new textstyle if one doesn't exist

Anonymous
Not applicable

I've learned how to create MText through VBA and utilize an existing Font Style, is it possible to have VBA check if a Font Style exists in the dwg? If so, it would continue the macro, if not then it would create a specified style. I can't find anything online about this other than directly changing an object's style upon creation, not about creating a style from scratch if the style doesn't exist.

 

 'CHECKED BY DATE
    Set MTextObj4 = ThisDrawing.ModelSpace.AddMText(corner4, widthN, text4)
        With MTextObj4
            .AttachmentPoint = acAttachmentPointMiddleLeft
            .InsertionPoint = corner4
            .StyleName = StyleName
            .Height = height1
        End With

 ... where StyleName = "PDF Search", the existing style.

 

0 Likes
2,476 Views
7 Replies
Replies (7)
Message 2 of 8

Ed__Jobe
Mentor
Mentor

Here's a quick function. If it returns false, use ThisDrawing.Textsyles.Add()

 

Public Function HasTextStyle(StyleName As String) As Boolean
    Set HasTextStyle = False
    Dim name As String
    For Each name In ThisDrawing.TextStyles
        If name = StyleName Then HasTextStyle = True
    Next name
End Function

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

Thanks for the swift reply, Ed! When I plug in this code, it keeps stating "Object required". Adding a Dim call for HasTextStyle As Object yields a duplication.

0 Likes
Message 4 of 8

Ed__Jobe
Mentor
Mentor

You don't dim it. You're not creating an instance of the function, you just call it.

 

If HasTextStyle("MyStyleName") = false then

  Dim myStyle As AcadTextStyle

   Set myStyle = ThisDrawing.TextStyles.Add("MyStyleName")

   myStyle. 'set some style properties here

End If

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 8

norman.yuan
Mentor
Mentor

@Ed__Jobe wrote:

Here's a quick function. If it returns false, use ThisDrawing.Textsyles.Add()

 

Public Function HasTextStyle(StyleName As String) As Boolean
    Set HasTextStyle = False
    Dim name As String
    For Each name In ThisDrawing.TextStyles
        If name = StyleName Then HasTextStyle = True
    Next name
End Function

The red line code should be:

 

HasTextStyle = False '' That is, "Set" should be removed, because the function's return type is a value type, not an Object type.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 8

Ed__Jobe
Mentor
Mentor

Oops, I thought I took that out. Thanks for catching it.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 7 of 8

Anonymous
Not applicable
Cool. I got the code to accept into the form, how can I call them function from a button in the same form? I get "Argument not optional" with direct calls of HasTextStyle on the button.
0 Likes
Message 8 of 8

Anonymous
Not applicable

I've been toying with our template border and the VBA layouts, here is what I have:

(This is to tie into my first posting here on the forums of Draw multiple lines )

 

What I have written requires a pre-existing Text Style of "PDF Search" (hence why I started this thread of how to verify/create).

 

Please let me know what you think.  When "Accept&Close" is clicked, the Subs are run back to back, there is a separate form for another part of the title block available.

 

0 Likes