Working within a SOLIDWORKS PDM workgroup, updating information on a PDM datacard for multiple files manually can quickly become tedious and prone to mistakes. Whether you’re a new designer, a project manager revising data, or an administrator entering details, doing this task by hand often slows down workflows and introduces inconsistencies.
The good news is you can automate this entire process with a simple VBA macro. In this guide, you’ll learn how to use a VBA macro to interact with and update a PDM datacard, helping streamline your work and reduce repetitive data entry in SOLIDWORKS and SOLIDWORKS PDM.
For this tutorial, we’ll use SOLIDWORKS 2023 and SOLIDWORKS PDM Professional 2023. You’ll see how to build a macro that can read and update datacard variables inside a PDM vault.
Step 1: Creating a New Macro
Start by opening SOLIDWORKS and following these steps alongside me–
- Navigate to Tools > Macro > New.
- Name the new macro “DataCard”.
- This will open a Visual Basic for Applications (VBA) editor. There, you can start writing your code.
This guide is an extended version of work previously done for automating PDM with VBA. If needed, you can always catch up on our previous tutorial guides about checking files into another vault at our Blue Byte YouTube channel.
Step 2: Setting References and Connecting to the Vault
Before you can start editing the PDM files, we need to make sure that the references are correctly connected to the PDM vault. Let’s do that–
- In the VBA editor, navigate to Tools > References.
- Look for PDMWorks Enterprise in the list of references (this is the PDM library) and select it.
- Then simply click “OK” to save the reference.
Now, let’s define the essential variables:
Dim edmVault As IEdmVault5
Dim filePath As String
Dim swFile As IEdmFile5
Dim swFolder As IEdmFolder5
Dim variableName As String
Dim varEnum As IEdmEnumeratorVariable5
Step 3: Connecting to the Vault
We’ll now establish a connection to the PDM vault and log in automatically. This is a critical step that authenticates your macro and authorizes it to retrieve your files and data cards in your vault. The code logs in automatically using your current Windows credentials, so it’s simple to accomplish and allows your automation to run smoothly.
Set edmVault = New EdmVault5
edmVault.LoginAuto "YourVaultName", 0 ' Replace "YourVaultName" with your actual vault name
Step 4: Getting the File and Folder
Once you are logged in to the vault, you need to identify and retrieve the file that you want to modify. The best practice to do so is to use the complete file path.
Simply right-click on the file in your File Explorer and select “Copy as Path”.
Note: This is just one of the methods you’d want to follow for SolidWorks PDM data card examples. Other methods typically include searching for files that exist within the API. However, I’d say the file path method I’m using is the simplest.
vbaCopy codefilePath = "C:\PDM_Vault\YourFile.SLDPRT" ' Replace with your actual file path
Set swFile = edmVault.GetFileFromPath(filePath, swFolder)
Step 5: Checking Out the File
Before making any sort of changes to a SolidWorks PDM data card, you must check out the file. This is part of PDM’s version control process. The check-out process places the file in your exclusive control, so others can’t change it at the same time, and thus prevent data corruption.
You’ll receive an error if you attempt to edit a variable on an unchecked-out file.
If Not swFile.IsLocked Then
swFile.LockFile swFolder.ID, 0
End If
Step 6: Accessing Datacard Variables
Now that the checking is done, we can go ahead and access, view, and modify the datacard variables. We’ll use the enumerator to modify the variables. It’s an easy way of updating these variable fields.
The following example sets the “Description” field on the data card of the selected file to “New Description”. You can do the same to make changes for any other data card variables, such as part number, revision, or material.
Set varEnum = swFile.GetEnumeratorVariable
variableName = "Description" ' Example variable
varEnum.SetVar variableName, "@", "New Description"
This example sets the “Description” field on the data card of the selected file to “New Description”.
Step 7: Testing and Finalizing
If you run the macro now, you should be able to see that the data card now shows the new value.
By now… you’ve successfully integrated this powerful automation technique that’ll now significantly improve your process by removing manual data input and ensuring data consistency for all your documents.
Summary
Following along with me, now you know-
- How to set up a VBA macro in SOLIDWORKS.
- How to connect to a SOLIDWORKS PDM vault using the API.
- How to check out files and modify data card variables.
Was the guide helpful so far? If so, don’t forget to check out our YouTube channel and subscribe to make sure you don’t miss out on any future updates. Follow us on LinkedIn for more tips and tricks on automating SOLIDWORKS and PDM processes.