Open a file with a PDM data card button
Remember Hannah’s blog post showing some of the clever things you can do with SOLIDWORKS PDM data card buttons? The last topic in the article was showing how to use a data card button to open a file.
I thought it would be fun to take a deeper dive into her concept to see how much further we could go.
What if the AcroRd32.exe is in multiple locations within your company?
It seems unlikely that everyone in a company would have their Adobe reader installed in “C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader”. Even if they did, future versions likely won’t use that same path. Therefore, maintaining the path to the executable file would be a headache.
When you use the command type “Command String”, the “Command” text box is simply running a DOS command. “START” is a DOS command for opening a file. It opens the file in the associated program.
Thus use:
START "C:\PDM_Professional\PDFs\%3.pdf"
“Start” opens the file in your preferred Adobe program; the location of the application’s executable file doesn’t matter.
What if the file to open is located in the vault?
If you want to use a data card to open a file and it’s not in the vault, you have a problem. DOS doesn’t know that PDM exists. It doesn’t understand that a file may not always be in the local cache. So if the button is opening a different file, i.e. a standard operating procedure document, and that file is not in the local cache, when the data card command runs, the application is going to complain that it could not find the requested file. So instead use:
Explorer "C:\VAULT NAME\SOPs\SOPManual.docx"
The “Explorer” command will use Windows Explorer to open the file just as if you double-clicked on it. As we all know, if you double-click on a file, and the file is not in the local cache, the file will first be cached automatically, then opened in the associated application.
What if the file is in the vault and I want to make sure the user is getting the most recent version?
Oh, that’s a toughie. Like I said, the “Explorer” command above works just as if you double-clicked on it. If you have an older version cached and don’t have “Always work with latest version of files” turned on, you’ll get an older version of the file. We’re going to need to get to the programming interface. Sorry PDM Standard guys, you’re going to have to stick with just the “Explorer” command, PDM Standard doesn’t have a programming interface.
We’re going to dust off Powershell. The goal here is to still just paste some text into the “command” data card button property. I don’t want to have to install anything on any computers. No setup. The Powershell code below would work if you opened a Powershell window and typed these lines in one at a time. If you’re curious, here is a quick summary:
- Line 2 creates a variable connected to PDM
- The code connects to the vault in Line 3
- Line 4 is a hack. It is the easiest way I have found to create the folder variable needed for Line 5’s “GetFileFromPath”
- Line 5 connects the code to the required file
- The GetFileCopy method in Line 6 tells PDM to get the latest version of the file to the local cache
- Instead of DOS’ START command, Powershell uses start-process in Line 7
In this example, let’s assume the file we want the button to open is “C:\VAULT NAME\SOPs\SOPManual.docx”. In this case, line 1 becomes
$filename = 'C:\VAULT NAME\SOPs\SOPManual.docx'
You can give the file any extension. Just make sure you have an application associated with it.
Recall from Hannah’s post the vault name can be passed as the second parameter – %2. So, line 3 becomes:
$vault.LoginAuto('%2',0)
At this point, we have two remaining issues. The data card button “command” box limits us to 255 characters. If the file file name is very long we’re going to run over that limit. So let’s change the variable names to $n for file name, $v for vault, $fo for folder, and $f for file. This will give us a little more space.
Lastly, how do we get all of this in the command text box and tell PDM we want Powershell to execute the code? You tell PDM to run Powershell with the Powershell.exe command. Then, just place the code you want Powershell to run inside of quotes. You can put multiple Powershell commands together on one line if you separate them with semicolons.
Finally, here is the final result! (Change c:\vault name\SOPs\SOPManual.docx with your file name, ignore the word wrap, paste in all in as one long line of text.)
powershell.exe "$n='c:\vault name\SOPs\SOPManual.docx';$v=New-Object -COMObject ConisioLib.EdmVault;$v.LoginAuto('%2',0);$fo=$v.RootFolder;$f=$v.GetFileFromPath($n,$fo);$f.GetFileCopy(0);start-process $n"
It certainly isn’t pretty but its easy, just copy this code, change the file name and paste into the button properties!