Skip to main content
12-Amethyst
January 19, 2021
Solved

VBA Excel - ImportNewModel

  • January 19, 2021
  • 1 reply
  • 6246 views

Hi,

I'm working on an Excel VBA tool that imports a lot of pvz files into Creo and saves them as .asm's. Works like a charm when I do this by hand (obviously, these are not real CAD models but facet geometry).

If I run the code below, I get a Type Mismatch error on te line starting with Set Model = session.ImportNewModel...

The weird thing is Creo actually imports the pvz, because if I do File > Open > In Session, I can see and open my example asm (testjaap.asm).

Does anyone know how I can get the model opened?? I tried all kinds of things, but nothing works so far...

Thnx!
Jaap

code:

Sub import_pvzs()
Dim asynconn As New pfcls.CCpfcAsyncConnection
Dim conn As pfcls.IpfcAsyncConnection
Dim session As pfcls.IpfcBaseSession
Dim model As IpfcModel
Set conn = asynconn.Connect("", "", ".", 5)
Set session = conn.session
Set model = session.ImportNewModel("C:\local\test_buildassyfromtextfile_transformation_matrix\TEST_ASSY_A0120069, Box Packer\A0133910, CRASH CAMS - Cabinet Module.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "testjaap", Nothing)
conn.Disconnect (2)
End Sub

Best answer by lhoogeveen

Strange. Your code worked for me.

 

Ideas:

  • Do you have Tools>References>'Creo VBA API Type Library for Creo Parametric' set?
  • Creo 7 introduced part multi bodies - not sure what impacts this has on the Creo VB API
  • VBA 'Option Explicit' is turned on? I don't think this would affect this but I usually have this off.
  • Is mdl set a public variable somewhere else in VBA?
  • Could always open a case with PTC.

1 reply

24-Ruby III
January 19, 2021

Hi,

sample code for STEP import...

Dim mdl As IpfcModel = session.ImportNewModel(
 "D:\WorkDir\Example\heatSink.step",
 EpfcNewModelImportType.EpfcIMPORT_NEW_STEP,
 EpfcModelType.EpfcMDL_PART,
 "MyNewPart",
 Nothing)
mdl.Display()

 

jkramer12-AmethystAuthor
12-Amethyst
January 20, 2021

Thnx for your reply!

Unfortunately, this seems to be the syntax for Visual Studio, not for Excel.

In Excel I'm not allowed to combine the Dim and the command. And if I split the line in:

Dim mdl As IpfcModel
Set mdl = session.ImportNewModel("C:\local\test_buildassyfromtextfile_transformation_matrix\TEST_ASSY_A0120069, Box Packer\A0133910, CRASH CAMS - Cabinet Module.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "testjaap", Nothing)

I get the Type Mismatch error, even though session.ImportNewModel is an IpfcModel according to the documentation (and according to your example).

Anyone have a clue how to cope with these kind of situations in VBA Excel? It's not the first time Excel bugs me with object type problems...

Thnx!

17-Peridot
January 20, 2021

I was able to get this to work in Excel VBA:

Dim mdl As IpfcModel
Set mdl = session.ImportNewModel("C:\Users\Public\Documents\testasm.pvz", EpfcNewModelImportType.EpfcIMPORT_NEW_PRODUCTVIEW, EpfcModelType.EpfcMDL_ASSEMBLY, "mynewpart", Nothing)

'Make sure model is displayed and active.
Dim oWindow As pfcls.IpfcWindow
Set oWindow = session.GetModelWindow(mdl) 'If no model was open in Creo session, model automatically displays.
If oWindow Is Nothing Then Set oWindow = session.CreateModelWindow(mdl) 'Create window before displaying model
mdl.Display
oWindow.Activate 'Active window with new model in it

 The "mynewpart" name doesn't seem to work when importing PVZ assemblies though.