Message 1 of 4
Inventor 2024 VBA Revit Exporting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
In Inventor 2024, I am trying to use a VBA macro to open a Solidworks assembly file on my computer, save it as a Revit file (which I can do manually using File>Save As>Revit) to my computer (along with a few other file types), and then the program ends. However, I can't seem to find a method to save the Revit file (.rvt) to my disk, I can only create objects within the code. See the middle section of the code below, lines 53-70).
This help doc is all I have: https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-RevitExport.
Any help is appreciated 🙂
Sub File_Exports_Macro()
' Declare the application and document objects
Dim invApp As Application
Dim invDoc As Document
Dim swFilePath As String
Dim savePath As String
Dim partNum As String
Dim fileExtension As String
Dim fullFilePath As String
' Initialize the Inventor application
Set invApp = ThisApplication
' Prompt user for the SolidWorks file path to open
swFilePath = InputBox("Enter the full path of the SolidWorks file to open:", "Open SolidWorks File")
If swFilePath = "" Then
MsgBox "No file path provided. Operation cancelled.", vbExclamation
Exit Sub
End If
' Open the SolidWorks document
On Error Resume Next
Set invDoc = invApp.Documents.Open(swFilePath)
If invDoc Is Nothing Then
MsgBox "Failed to open the SolidWorks file. Please check the path and try again.", vbCritical
Exit Sub
End If
On Error GoTo 0
' Prompt user for the part number
partNum = InputBox("Enter the part number:", "Part Number")
If partNum = "" Then
MsgBox "No part number provided. Operation cancelled.", vbExclamation
invDoc.Close (True)
Exit Sub
End If
' Prompt user for the save directory
savePath = InputBox("Enter the directory path where you want to save the converted files:", "Save Directory")
If savePath = "" Then
MsgBox "No save path provided. Operation cancelled.", vbExclamation
invDoc.Close (True)
Exit Sub
End If
' Ensure the savePath ends with a backslash
If Right(savePath, 1) <> "\" Then
savePath = savePath & "\"
End If
' Save as .RVT
On Error Resume Next
Dim oAssemblyDoc As AssemblyDocument
Set oAssemblyDoc = invDoc
If oAssemblyDoc.ComponentDefinition.ModelStates.ActiveModelState.ModelStateType = ModelStateTypeEnum.kSubstituteModelStateType Then
oAssemblyDoc.ComponentDefinition.ModelStates.Item(1).Activate
Set oAssemblyDoc = ThisApplication.ActiveDocument
End If
Dim oRevitExportDef As RevitExportDefinition
Set oRevitExportDef = oAssemblyDoc.ComponentDefinition.RevitExports.CreateDefinition
oRevitExportDef.Location = savePath
oRevitExportDef.FileName = partNum & " RVT.rvt"
oRevitExportDef.Structure = kEachTopLevelComponentStructure
oRevitExportDef.EnableUpdating = True
' Create RevitExport
' invDoc.SaveAs doesn't work, what goes here??
' Save as .DWG
fileExtension = ".dwg"
fullFilePath = savePath & partNum & " DWG" & fileExtension
invDoc.SaveAs fullFilePath, True
' Save as .IGES
fileExtension = ".iges"
fullFilePath = savePath & partNum & " IGES" & fileExtension
invDoc.SaveAs fullFilePath, True
' Save as .STL
fileExtension = ".stl"
fullFilePath = savePath & partNum & " STL" & fileExtension
invDoc.SaveAs fullFilePath, True
' Close the document
invDoc.Close (True)
' Inform the user that the operation is complete
MsgBox "File converted and saved as .RVT, .DWG, .IGES, and .STL successfully!"
End Sub