VBA API user form for Global Parameters

 

i don't know much about VBA or API but was trying to use ChatGPT to help me with that. I wanted a simple user form to udate global parameters in apart template. i'm going to paste what i tried below. Any help would be grately appreciated. 

 

🔧 GOAL:

To build a user-friendly VBA macro with a form that allows users to input values, then programmatically update existing global variables (from the Equations Manager) inside a part template.

✅ WHAT WAS IMPLEMENTED:

1. Created a UserForm in VBA

  • Form included text boxes for all global parameters:
    • "basic cell X", "length", "LS Height", "Caliper", etc.
  • Used control names like txtbasiccellX to match each global variable.

2. Wrote a SetGlobal helper function

  • Located inside the UserForm module.
  • Purpose: Loop through EquationMgr, find a matching variable, delete it, and add a new equation using .Add3.

3. CommandButton1_Click ran the update logic

  • Collected values from textboxes.
  • Called SetGlobal() for each parameter.
  • Called ForceRebuild3 to update the model.

❌ WHAT DIDN’T WORK:

swEqMgr.Add3(...) silently failed

  • After deleting a global variable, the new one was not added back.
  • Result: variable was removed permanently from the Equations table.
  • No error was thrown, but variable disappeared.

swEqMgr.SetEquation(...) didn’t update anything

  • Tried to update an existing variable’s equation in place.
  • The call ran without error but had no effect on the model.

❌ Attempted fallback strategies

  • Wait delays (to allow model rebuilds): no effect.
  • SetGlobalVariable(...) calls: not supported or failed silently.
  • Confirmed that GetEquationMgr.GetCount correctly found the equations, but all API-based editing methods failed in SW 2025.

🔍 ROOT CAUSE (Highly Likely):

⚠️ SolidWorks 2025 EquationMgr API seems to be partially broken or restricted:

  • EquationMgr.Add3() is known to delete equations but fails to add them properly in some versions (especially 2023+).
  • This behavior was observed across multiple testing macros and confirmed with visual checks inside the model.

💡 WORKAROUNDS TRIED:

  • Attempted SetEquation() method — no effect.
  • Attempted swModel.SetGlobalVariable() — method doesn't exist or fails silently.
  • Attempted linking to custom properties — not connected to equations.
  • Tried switching to Design Table approach — also failed due to SolidWorks not recognizing global variables directly.

🧾 SAMPLE TEST VARIABLES:

These were used throughout the macro and testing:

textCopyEdit"basic cell X" = 3
"basic cell Y" = 5
"length" = 16
"width" = 9
"LS Height" = 6
"SS Height" = 6
"Caliper" = 0.024
"LS Center Cell Count" = 4
"LS Center Cell Dimension" = 3.2
"SS Center Cell Count" = 2
"SS Center Cell Dimension" = 3
"LS Aircell Dimension" = 0.5
"SS Aircell Dimension" = 0.5

🙋‍♂️ KEY QUESTIONS TO ASK THE FORUM:

  1. Why does EquationMgr.Add3(...) fail to add global variables in SW 2025 after deletion?
  2. Is there a working method to programmatically update existing global variables in SW 2025 via VBA or macro?
  3. Has the behavior of EquationMgr changed in recent versions (2023–2025)?
  4. Are there alternatives for user-driven parameter input that reliably tie into global variables?