Skip to main content

Featured Post

Why I Wrote The Sheet Mechanic (And Why Calculations Aren’t Enough)

For engineers who already know the math—but still lose projects. For the last few years, I’ve been sharing technical guides here on Mechanical Design Handbook —how to size a motor, how to calculate fits, and (as you recently read) how to choose between timing belts and ball screws. But after 25 years in industrial automation, I realized something uncomfortable: Projects rarely fail because the math was wrong. They fail because: The client changed the scope three times in one week. A critical vendor lied about a shipping date (and no one verified it). The installation technician couldn’t fit a wrench into the gap we designed. University taught us the physics. It didn’t teach us the reality. That gap is why I wrote my new book, The Sheet Mechanic . This is not a textbook. It is a field manual for the messy, political, and chaotic space between the CAD model and the factory floor. It captures the systems I’ve used to survive industrial projec...
NEW RELEASE: Stop trying to be a Hero. Start being a Mechanic. Get "The Sheet Mechanic" on Amazon »
Disclosure: As an Amazon Associate, I earn from qualifying purchases.

Solid Edge Motion Simulation with Excel VBA - Part 3

In a previous example, I showed how to control variables in a Solid Edge sketch using a direct link to an Excel spreadsheet. In this example, I am going to show how to use VBA (Visual Basic for Applications) in Excel to control the variables in a Solid Edge sketch directly via the API.

Advertisement

1. Setup Excel VBA

Create a new file in Microsoft Excel.

Press Alt+F11 to enter the Microsoft Visual Basic window as shown below.

Excel VBA Editor Window
Figure 1: The VBA Editor environment.

Right-click in the Project Explorer on the new file name (in this example, Book2) > Insert > Module.

2. Add References

To make Solid Edge type libraries available to the Visual Basic Object Browser, click References on the Tools menu (or Project menu depending on your version). On the References dialog box, select the Solid Edge libraries you want to access (specifically the Solid Edge Framework Type Library and Solid Edge Part Type Library).

VBA References Dialog selecting Solid Edge libraries
Figure 2: Linking the Solid Edge API libraries to Excel.

All variable automation is accessed through the Variables collection and Variable objects. The Variables collection serves two purposes: it allows you to create and access variable objects, and it allows you to work with dimensions as variables.

Note: When debugging programs that interact with the Variable Table, it helps to have the Variable Table displayed while stepping through the program. The Variable Table shows the results as they are executed.

3. The VBA Code

Write the following code into the VBA code area as shown below:

Advertisement

You can copy & paste the following code to your Excel VBA module:

' =================================================================
' Example of using VBA code to control variables in Solid Edge document
' By AkeBlogger
' http://mechanical-design-handbook.blogspot.com/
' =================================================================

Sub Simulate()
On Error Resume Next

    'Declare the program variables.
    Dim iAngle As Single
    Dim objApp As Object
    Dim objVariables As Object
    Dim objVariable As Object
   
    '------------------------------------------------------------------
    ' Checking for the active document
    Set objApp = GetObject(, "SolidEdge.Application")
    If Err Then
        MsgBox "You must open Solid Edge before executing this module!!", vbOKOnly + vbExclamation, "Error"
        Exit Sub
    Else
        'Connect to a running instance of Solid Edge.
        Set objApp = GetObject(, "SolidEdge.Application")
        'Access the Variables collection.
        Set objVariables = objApp.ActiveDocument.Variables
    End If
        
    'SIMULATION STARTS!
    For iAngle = 0 To 360 Step 2
        ' Changing Cam_angle from 0, 2, 4, ..., 360 deg.
        Call objVariables.Edit("Cam_angle", iAngle)
        DoEvents ' Allow the system to update the screen
    Next iAngle
   
End Sub

4. Prepare Solid Edge

Open the Solid Edge file from the previous example and delete the formula in the Variables Table as shown below. In this example, we will control the variable (dimension) using VBA code directly, so the link to the external spreadsheet must be removed.

5. Run Simulation

We are now ready for simulation. Resize the VBA Windows as shown below so you can see both applications, then click run to see the result.

Watch the following video for the actual result!

Comments

Popular posts from this blog

Dowel Pins & Locating Pins: The Basics of Fixture Design

Dowel pins are precision cylindrical pins used for accurate part alignment in assemblies. They control position, not clamping force. This guide explains tolerances, fits, sizing rules, and design best practices. Figure 1: A typical fixture setup. Notice how dowel pins (silver) provide precise location, while bolts (not shown here) provide the clamping force. In the world of Precision Engineering , the difference between a high-quality product and a scrap part often comes down to microns. While bolts hold parts together, they are terrible at positioning them. This is where Dowel Pins and Locating Pins become essential components in industrial tooling . Advertisement What is a Dowel Pin? Dowel pins are precision-ground fasteners used to secure the relative position of two parts. They are typically machined to extremely tight tolerances (often within 0.0001 inches) and are available in materials like: Hardened Steel: For high-wea...

Hoeken's Linkage: Kinematics and Walking Robot Design

Figure 1: Animated simulation of the Hoeken’s Linkage showing the characteristic "tear-drop" coupler curve. 🚀 New Design Guide Available Don't just read about it—build it. Check out our new tutorial: How to Design a Hoeken’s Linkage in Excel (with Free VBA Simulator) » Introduction to the Hoekens Linkage The Hoekens linkage is a specialized four-bar mechanism designed to convert rotational motion into an approximate straight-line motion. While it serves a similar purpose to other straight-line generators, its unique coupler curve—a "tear-drop" shape—makes it exceptionally useful for intermittent motion and walking machines. One of the most fascinating aspects of kinematic theory is the concept of "Cognates." The Hoekens linkage is actually a cognate linkage of the Chebyshev Straight-line Mechanism . This means that while the physical structure and link lengths differ, they can generate...

Chebyshev Linkage Design: Ratios & Straight-Line Motion

Figure 1: The Chebyshev linkage converts rotary input into approximate straight-line output. Introduction to the Chebyshev Linkage The Chebyshev linkage is a four-bar mechanical linkage that converts rotational motion into approximate straight-line motion . It was invented by the 19th-century Russian mathematician Pafnuty Chebyshev , who was deeply involved in the theoretical problems of kinematic mechanisms. His goal was to improve upon existing designs, such as the Watt Straight-line Mechanism , which James Watt had used to revolutionize the steam engine. While Watt's design produces a lemniscate (figure-eight) curve with a straight section, the Chebyshev linkage is often preferred in specific machinery because the straight-line portion of the path is parallel to the line connecting the two fixed ground pivots. Search for Mechanism Design & Robotics Books Advertisement Design Ratios and Geometry The gen...