Loading a macro from a toolbar button

I have written a VBA macro that prints the layout name of the selected paper space page on that page. This is useful for referring to a particular layout during the design process.

Sub ThisLayoutText()
Dim layText As AcadText
Dim objLayout As AcadLayout
Set objLayout = ThisDrawing.ActiveLayout
Dim layName As String
layName = objLayout.Name

Dim insPt(0 To 2) As Double: insPt(0) = 5: insPt(1) = 6
Dim dHeight As Double
dHeight = 4

Set layText = ThisDrawing.PaperSpace.AddText(layName, insPt, dHeight)

layText.Update
ThisDrawing.Application.ZoomExtents
End Sub

This works fine but I want to run the macro from a toolbar button. How do I create a new button and link it to my macro?

Regards

Jeremy

Comments

  • This is a POP menu example.

    [Drainage schedule]^C^C(vl-vbaload "P:/AutoDESK/VBA/Drainage.dvb") (vl-vbarun "Drainage")

  • Thanks for your responses. I have moved on from text to attributes -see code below:

    Sub AttributeProps_2()
    Dim layText As AcadText
    Dim objLayout As AcadLayout
    Set objLayout = ThisDrawing.ActiveLayout
    Dim LayoutName As String
    Dim dHeight As Double
    Dim blockObj As AcadBlock
    Dim sBlockName As String
    Dim objEnt As AcadEntity
    Dim insPt(0 To 2) As Double: insPt(0) = 4: insPt(1) = 4: insPt(2) = 0

    LayoutName = objLayout.Name
    dHeight = 2.4
    sBlockName = "LayBlock"
    Set blockObj = ThisDrawing.Blocks.Add(insPt, sBlockName)

    Dim attributeObj As AcadAttribute
    Dim sPrompt As String: sPrompt = "Attributes:"
    Dim sTag As String: sTag = "Tag"
    Dim sValue As String: sValue = LayoutName
    Dim blockRefObj As AcadBlockReference
    

    Set attributeObj = blockObj.AddAttribute(dHeight, acAttributeModeNormal, sPrompt, insPt, sTag, "PAGE: " & sValue)
    Set blockRefObj = ThisDrawing.PaperSpace.InsertBlock(insPt, sBlockName, 1, 1, 1, 0)
    MsgBox "The block " & sBlockName & " has been inserted."

    End Sub

    This is very frustrating. I want to clear the previous attribute before adding a changed layout name but I can't see how to!

    I have done a bit of autocad lisp programming but I find VBA easier

    Also it is difficult to enter code like above in this comment box - is there an alternative?

    Thanks in advance

    Jeremy

  • See the Intro to block attributes

    https://developer.bricsys.com/bricscad/help/en_US/CurVer/DevRef/source/Objects_Attribute.htm

    You need to insert the block first, then assuming it HasAttributes, retrieve the attached attributes using GetAttributes

    https://developer.bricsys.com/bricscad/help/en_US/CurVer/DevRef/source/Methods_GetAttributes.htm

    From here you would iterate through the attribute objects to find the one you wish to update and update its TextString property with your value.

    https://developer.bricsys.com/bricscad/help/en_US/CurVer/DevRef/source/Properties_TextString.htm

    VBA is kind of deprecated. If this is more comfortable to you than LISP, then you may want to explore switching to .NET

    Did you check into using a FIELD instead referencing CTAB? This can be embedded into the ATTDEF of a block definition to give a non programming method to achieve the same. This method works find for display and printing. The downside to this method is that it wouldn't work if you planned to extract the data using DATAEXTRACTION or similar as CTAB would be whatever the current layout was at time of extraction.