Jump to content

Solidworks VBA for renaming components in an assembly


Recommended Posts

Posted

This macro works in Solidworks 2024. Would someone please help me to get it working properly for 2025?

 

See attached file.

Thanks.

jn_2.txt

Posted

What is the error you get?

 

SolidWorks changed a few VBA commands from 2024 to 2025, you'll probably get better results on a SolidWorks/Dassault Systems forum.

 

A quick search on SolidWorks VBA changes between 2024 and 2025 might help.

Posted (edited)

You can wrap the inputbox with ucase(trim(  or even add things infront like i did with description eliminating lines of code. 

 

old
    ' --- Ask user if they want to Add or Remove description ---
    action = InputBox("Type 'A' to Add description or 'R' to Remove description:", "Action Choice", "A")
    action = UCase(Trim(action))
    If action <> "A" And action <> "R" Then MsgBox "Invalid input.": Exit Sub
    
    ' --- Optional prefix ---
    prefix = ""
    Dim userInput As String
    userInput = InputBox("Enter component location prefix: T for Top, B for Bottom, leave blank for none:", "Prefix Option")
    userInput = UCase(Trim(userInput))
    If userInput = "T" Then prefix = "Top_"
    If userInput = "B" Then prefix = "Bot_"
    
    ' --- Optional description (only if adding) ---
    If action = "A" Then
        description = InputBox("Enter description to append (leave blank for none):", "Optional Description")
        description = Trim(description)
        If description <> "" Then description = "_" & description
        If description <> "" Then
            CopyToClipboardAPI (description)
            MsgBox "Description '" & description & "' copied to clipboard."
        End If
    End If
    
New
    ' --- Ask user if they want to Add or Remove description ---
    action = UCase(Trim(InputBox("Type 'A' to Add description or 'R' to Remove description:", "Action Choice", "A")))
    If action <> "A" And action <> "R" Then MsgBox "Invalid input.": Exit Sub
    ' --- Optional prefix ---
    Dim userInput As String
    userInput = UCase(Trim(InputBox("Enter component location prefix: T for Top, B for Bottom, leave blank for none:", "Prefix Option")))
    Select Case True
      Case userInput = "T"
        prefix = "Top_"
      Case userInput = "B"
        prefix = "Bot_"
      Case Else
        prefix = ""
    End Select
    ' --- Optional description (only if adding) ---
    If action = "A" Then
      description = "_" & Trim(InputBox("Enter description to append (leave blank for none):", "Optional Description"))
      If description <> "_" Then ;old code would still allow "_" to be copied to clipboard this skips and should prob exit sub if description is only "_" ?
        CopyToClipboardAPI (description)
        MsgBox "Description '" & description & "' copied to clipboard."
      Else
        MsgBox "Description is Blank " & vbCrLf & " Exiting Command" 
        Exit Sub
      End If
    End If

 

Didn't run past fso. please describe what your running into.

Edited by mhupp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...