Hello @A.Acheson,
This code is not completely correct because there are 2 pages in the tested .idw drawing. The first page does not have the condition that the part name is "-MOD" or "-MOD2". On page 2, this condition is met, but the program generates both pages in .dxf format, even though the condition says that I only need page 2.
The second dxf generator was included because if the .idw file at the beginning of the program has the number of drawing pages: 1, it will not output the file to .dxf at all.
I wrote a new code which separates whether it should generate only a specific sheet or the whole document but throws the following error message:
Public Sub PublishDXF()
Dim doc As DrawingDocument
Set doc = ThisApplication.ActiveDocument
' Check if the active document is a DrawingDocument
If Not TypeOf doc Is DrawingDocument Then
MsgBox "The active document is not a .idw file!", vbExclamation
Exit Sub
End If
' Check if the document has sheets
If Not doc.Sheets.Count > 0 Then
MsgBox "The document does not contain any sheets!", vbInformation
Exit Sub
End If
Dim pageCount As Integer
pageCount = 0
' Count the sheets and check for the specified part name
For Each drawingSheet In doc.Sheets
' Check if the sheet has a model associated with it
If drawingSheet.DrawingViews.Count > 0 Then
Dim drawingView As drawingView
Set drawingView = drawingSheet.DrawingViews.Item(1)
Dim modelDoc As Document
Set modelDoc = drawingView.ReferencedDocumentDescriptor.ReferencedDocument
If TypeOf modelDoc Is PartDocument Then
Dim partDoc As PartDocument
Set partDoc = modelDoc
' Check if the part name matches the criteria
Dim partName As String
partName = Left(partDoc.DisplayName, Len(partDoc.DisplayName) - 4)
If partName Like "*-MOD" Or partName Like "*-MOD2" Then
' Export only this sheet to DXF format
ExportSheetToDXF doc, drawingSheet, pageCount
End If
End If
End If
' Increment the page count
pageCount = pageCount + 1
Next drawingSheet
' If no sheets matched the criteria or there is only one sheet, export the entire document
If pageCount = 0 Or doc.Sheets.Count = 1 Then
ExportDocumentToDXF doc
End If
End Sub
Private Sub ExportSheetToDXF(ByVal doc As DrawingDocument, ByVal sheet As sheet, ByVal pageCount As Integer)
' Export the specified sheet to DXF format
Dim DXFAddIn As TranslatorAddIn
Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
Dim oContext As TranslationContext
Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
Dim oOptions As NameValueMap
Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium
Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Vault\exportdxf.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
Dim fileName As String
fileName = "C:\Users\deak.peter\Desktop\PDF-DXF program\" & Left(doc.DisplayName, Len(doc.DisplayName) - 4) & "-MOD" & ".dxf"
oDataMedium.fileName = fileName
DXFAddIn.SaveCopyAs sheet, oContext, oOptions, oDataMedium
End Sub
Private Sub ExportDocumentToDXF(ByVal doc As DrawingDocument)
' Export the entire document to DXF format
Dim DXFAddIn As TranslatorAddIn
Set DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
Dim oContext As TranslationContext
Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
Dim oOptions As NameValueMap
Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium
Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
Dim strIniFile As String
strIniFile = "C:\Vault\exportdxf.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
Dim fileName As String
fileName = "C:\Users\user\Desktop\PDF-DXF program\" & Left(doc.DisplayName, Len(doc.DisplayName) - 4) & ".dxf"
oDataMedium.fileName = fileName
DXFAddIn.SaveCopyAs doc, oContext, oOptions, oDataMedium
End Sub
