In this guide, we’ll explore how to interact with SOLIDWORKS PDM datacard variables using VBA. This is particularly useful for automating tasks in PDM and SOLIDWORKS, streamlining your workflow.
For this demonstration, I’m using SOLIDWORKS 2023 and SOLIDWORKS PDM Professional 2023. The example will involve creating a VBA macro to access and modify datacard variables in a PDM vault.
Step 1: Creating a New Macro
To begin, open SOLIDWORKS and follow these steps:
- Go to
Tools > Macro > New
. - Name the new macro
DataCard
. - The Visual Basic for Applications (VBA) editor will open, allowing us to start coding.
This tutorial builds on previous work done on automating PDM through VBA. You can find our earlier tutorial on checking files into another vault on our Blue Byte YouTube channel.
Step 2: Setting References and Connecting to the Vault
Before we can manipulate PDM files, we need to set the references correctly and connect to the PDM vault.
- In the VBA editor, go to
Tools > References
. - Look for
PDMWorks Enterprise
in the list of references (this is the PDM library) and select it. - Click
OK
to save the reference.
Now, let’s define the essential variables:
1 |
Dim edmVault As IEdmVault5<br>Dim filePath As String<br>Dim swFile As IEdmFile5<br>Dim swFolder As IEdmFolder5<br>Dim variableName As String<br>Dim varEnum As IEdmEnumeratorVariable5<br> |
Step 3: Connecting to the Vault
We’ll now establish a connection to the PDM vault and log in automatically.
1 |
Set edmVault = New EdmVault5<br>edmVault.LoginAuto "YourVaultName", 0 ' Replace "YourVaultName" with your actual vault name |
Step 4: Getting the File and Folder
We need to retrieve the file from the vault using its file path. You can copy the full file path by right-clicking a file in File Explorer and selecting “Copy as Path”.
1 2 3 |
vbaCopy code<code>filePath = "C:\PDM_Vault\YourFile.SLDPRT" ' Replace with your actual file path Set swFile = edmVault.GetFileFromPath(filePath, swFolder) |
Step 5: Checking Out the File
Before modifying a data card variable, the file must be checked out.
1 |
If Not swFile.IsLocked Then<br> swFile.LockFile swFolder.ID, 0<br>End If<br> |
Step 6: Accessing Datacard Variables
Now that the file is checked out, we can access the datacard variables. We’ll use the enumerator to set the variables.
1 |
Set varEnum = swFile.GetEnumeratorVariable<br>variableName = "Description" ' Example variable<br>varEnum.SetVar variableName, "@", "New Description"<br> |
This example sets the “Description” field on the data card of the selected file to “New Description”.
Step 7: Testing and Finalizing
Once you’ve run the macro, you should see the data card updated with the new value.
Summary:
In this tutorial, we demonstrated how to:
- Set up a VBA macro in SOLIDWORKS.
- Connect to a SOLIDWORKS PDM vault using the API.
- Check out files and modify data card variables.
This is a powerful way to automate your PDM workflows, making file management more efficient.
If you found this tutorial helpful, subscribe to our YouTube channel and follow us on LinkedIn for more tips on automating SOLIDWORKS and PDM processes. Feel free to leave your questions in the comments, and I’d be happy to help!