First VB macro - opening a draft file

Hey Guys,


ive started to learn VB so that i can automate some of my drafting tasks. my main project right now is replacing the old sheet style with our fancy new version. 

ive spent the last week going through VB tutorials on youtube, trying to grasp the basics.



i certainly am not after a full solution. just help on a good starting place.

im really keen on learning VB and feel somewhat confident that i can look through the code to grasp the basic understanding of what its doing.

Im looking for a snippet of code that can open a specific draft file stored locally.

can anyone help?






Re: First VB macro - opening a draft file



See if some of these tutorials on Solid edge coding have the answers you seek:

Solid Edge Documents: New, Open, Save, Export, etc.: CSharp  |  VB.Net  | CLI  |  MFC




Re: First VB macro - opening a draft file

[ Edited ]



I was in a similar position a while back.


I copied some code from others to create a vb macro.

this macro when run while a model file is open, checks for a draft file with the same name in the current Dir. and opens it , or runs the create a new draft from the current model command.


i found that trying to see how others did things was easier that trying to write from scratch.

hope it helps bit, 


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim objApp As SolidEdgeFramework.Application = Nothing
        Dim objRev As RevisionManager.Application = Nothing
        Dim objRevDft As RevisionManager.Document = Nothing
        Dim objDocuments As SolidEdgeFramework.Documents = Nothing
        Dim objDocument As SolidEdgeFramework.SolidEdgeDocument = Nothing
        Dim objPart As SolidEdgePart.PartDocument = Nothing
        Dim objdraft As SolidEdgeDraft.DraftDocument = Nothing

        Dim pathname As String
        Dim partname As String
        Dim dftname As String

            objApp = Marshal.GetActiveObject("SolidEdge.Application")
            objRev = CreateObject("RevisionManager.Application")
            objDocument = objApp.ActiveDocument
pathname = objDocument.Path ' Edited into code partname = objDocument.FullName objRev.WhereUsedCriteria("*.dft", True) = pathname objRevDft = objRev.FindWhereUsed(partname) If IsNothing(objRevDft) Then Call objApp.StartCommand(CommandID:=39001) Me.Close() Application.DoEvents() End If dftname = objRevDft.FullName Me.Close() objApp.Visible = True objDocuments = objApp.Documents objPart = objDocuments.Open(objRevDft.FullName) Application.DoEvents() Catch ex As Exception Finally If Not (objApp Is Nothing) Then Marshal.ReleaseComObject(objApp) objApp = Nothing End If If Not (objRev Is Nothing) Then Marshal.ReleaseComObject(objRev) objRev = Nothing End If If Not (objRevDft Is Nothing) Then Marshal.ReleaseComObject(objRevDft) objRevDft = Nothing End If If Not (objDocuments Is Nothing) Then Marshal.ReleaseComObject(objDocuments) objDocuments = Nothing End If If Not (objDocument Is Nothing) Then Marshal.ReleaseComObject(objDocument) objDocument = Nothing End If If Not (objPart Is Nothing) Then Marshal.ReleaseComObject(objPart) objPart = Nothing End If If Not (objdraft Is Nothing) Then Marshal.ReleaseComObject(objdraft) objdraft = Nothing End If End Try End Sub

Also a search on google for "solid edge api" brings up a programmers guide, a good place to start.


Any questions fire away - always found this forum happy to help



Added missing line to code to correct mistake shown below..




Re: First VB macro - opening a draft file

Hey Kevin, thanks very much. that's exactly what i was after.

Re: First VB macro - opening a draft file

Hi @kevin_parker


Is this code tested? I am consistently getting 'Nothing' for objRevDft and it runs the command with ID 39001

I have the draft of the current model in the same directory as the .PAR file.






Re: First VB macro - opening a draft file

Hello Tushar,

The code is tested I use it on a ribbon bar constantly,
It connects to the running application for an open document, (model or assembly) then uses revision manager to find the linked draft to open.
We only have one draft file for each model file.

( I deleted some comments before posting, so will check to see if I also deleted something else by accident).

Re: First VB macro - opening a draft file

[ Edited ]

Hi @Tushar,


Have you given a value to the pathname variable?


Maybe in your case could be:


pathname = Path.GetDirectoryName(partname)




Otherwise I think 

objPart = objDocuments.Open(objRevDft.FullName)

 should be

objdraft = objDocuments.Open(dftname)


Hope it helps.

Héctor Pelayo Izquierdo
Engineering Department, JAE Ingeniería y Desarrollos, S.L.
Solid Edge ST9 MP5 | NX 9
Development: VB.NET

Re: First VB macro - opening a draft file


yes you are correct.
It seems that a line was missing to declare the variable pathname,

pathname = objDocument.Path

should be before the line "partname = objDocument.FullName"

My original code had comments and parts that I had amended and commented out, this line must have also been removed.

Really sorry for the confusion, I hope that you managed to get your application working in the end..


Re: First VB macro - opening a draft file

Kevin: that's awesome. It works like a charm.

@Hector Thank you so much for helping to get this working.



Re: First VB macro - opening a draft file

I have been looking for a way to enter the draft file of  an open model for sometime. I am excited to use this program. I have tried to make it work but am hardly familar with VB. I am having trouble recognizing:


and Application.DoEvents().

If someone could help me out that would be awesome!

Thank you in advance.