2 Replies Latest reply on Nov 24, 2015 12:09 PM by Ivana Kolin

    Macro hanging on one step - Changing Text

    Patrick Malone

      I am attempting by this macro to change a piece if text.

      I have a few hundred drawings and I need to change a specific piece of text to a specified value.

      The current value is not always the same but it is always in the same place.

       

      What I have at this point goes as far as selecting the text (it is highlighted)

      I get a debugger error and the text is not changed.

       

      Can someone please help me get past this one step.

       

      ================= CODE: ======================

       

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Dim swSelMgr        As SldWorks.SelectionMgr

      Dim swNote          As SldWorks.Note

      Dim bRet            As Boolean

      Dim strNote01       As String

       

      Sub main()

       

      Set swApp = _

      Application.SldWorks

       

      strNote01 = "My Text"

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.69567916699979, 1.27146115981563E-02, 0, False, 0, Nothing, 0)

      Part.EditTemplate

      Part.EditSketch

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2203@Sheet Format10", "NOTE", 0.72358507099049, 1.51692975973382E-02, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2203@Sheet Format10", "NOTE", 0.72358507099049, 1.51692975973382E-02, 0, False, 0, Nothing, 0)

       

      Set swNote = swSelMgr.GetSelectedObject6(1, 0)   ' -> ROUTINE GETS AN ERROR RIGHT HERE

       

      bRet = swNote.SetText(strNote01)

       

      swModel.ForceRebuild3 (True)

       

      swModel.ClearSelection2 (True)

       

       

      Part.ClearSelection2 True

      End Sub

       

      ============== =======================

       

      As a Part 2 to this question,

      I want to set this piece of text to this value:    $PRP:"Scale"

       

      Other attempts at this won't allow me to specify this value.

       

      ie:  strNote01 = " $PRP:"Scale" "

       

      I don't understand the syntax required

        • Re: Macro hanging on one step - Changing Text
          Simon Turner

          You don't seem to be setting swSelMgr anywhere.

          Add this line:

          Set swSelMgr = Part.SelectionManager

           

          Also, before the line "Set swNote = swSelMgr.GetSelectedObject6(1, 0)", check that SelectByID2 has actually worked.

          If boolstatus = False Or swSelMgr.GetSelectedObjectCount2(-1) <> 1 Then 'Something has gone wrong

          • Re: Macro hanging on one step - Changing Text
            Ivana Kolin
            Option Explicit
            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim boolstatus As Boolean
            
            Dim swNote As SldWorks.Note
            Dim swSelMgr        As SldWorks.SelectionMgr
            Dim strNote01       As String
            Sub main()
            strNote01 = "My Text"
            Set swApp = Application.SldWorks
            
            
            Set swModel = swApp.ActiveDoc
            Set swSelMgr = swModel.SelectionManager
            
            boolstatus = swModel.Extension.SelectByID2("DetailItem5@Drawing View1", "NOTE", 0.772005084745763, 0.601420338983051, 0, False, 0, Nothing, 0)
            
            Set swNote = swSelMgr.GetSelectedObject6(1, -1)   ' -> ROUTINE GETS AN ERROR RIGHT HERE
            
            
            boolstatus = swNote.SetText(strNote01)
            swModel.ForceRebuild3 True
            swModel.ClearSelection2 True
            
            
            End Sub