Hi @aries.1482. There are multiple possibilities for failure there, but the main thing that I suggest is narrowing the amount of code you put into your Try block of code to the minimum needed for each potentially error prone line of code. The main two places that may need this are where you are attempting to retrieve the title block definition from the source, and from the destination. That leaves your Save & Close lines outside, where they will be obeyed either way. I also moved the block of code for getting the source title block definition up above the loop, because you only need to retrieve it once.
Try this edited version of your code:
Imports System.Windows.Forms
Sub Main
'Set active drawing as source drawing
Dim oSourceDocument As DrawingDocument
oSourceDocument = ThisApplication.ActiveDocument
'Get the source title block definition.
Dim oSourceTitleBlockDef As TitleBlockDefinition
Try
oSourceTitleBlockDef = oSourceDocument.TitleBlockDefinitions.Item("VMC-TP")
Catch oEx As Exception
Logger.Error("The specified TitleBlockDefinition was not found in the 'Source'. Exiting rule. {0}", oEx.Message)
'MsgBox("The specified TitleBlockDefinition was not found in the 'Source'. Exiting rule.", vbExclamation, "iLogic")
Exit Sub
End Try
'Open the destination drawing to copy the title block into.
Dim idwPaths = LoadIdwPaths()
Logger.Debug("Files to replace titleblock:" & vbCrLf & String.Join(vbCrLf, idwPaths))
For Each idwPath As String In idwPaths
If Not System.IO.File.Exists(idwPath) Then
Logger.Warn("File not found{0}{1}", vbCrLf, idwPath)
Continue For
End If
Dim oNewDocument As DrawingDocument = ThisApplication.Documents.Open(idwPath)
'Check if exist titleblock named VMC-TP
Dim ExistTitleBlock As TitleBlockDefinition
Try
ExistTitleBlock = oNewDocument.TitleBlockDefinitions("VMC-TP")
Catch oEx As Exception
Logger.Error("Specified TitleBlockDefinition not found in 'Destination'. {0}", oEx.Message)
'MsgBox("Specified TitleBlockDefinition not found in 'Destination'. Skipping to next.", vbInformation, "iLogic")
Continue For
End Try
If ExistTitleBlock.IsReferenced = True Then
Dim oSheet As Sheet
'Delete if exist titleblock named VMC-TP
For Each oSheet In oNewDocument.Sheets
If oSheet.TitleBlock.Definition Is ExistTitleBlock Then
oSheet.TitleBlock.Delete
End If
Next
ExistTitleBlock.Delete
'Get the new title block definition.
Dim oNewTitleBlockDef As TitleBlockDefinition
oNewTitleBlockDef = oSourceTitleBlockDef.CopyTo(oNewDocument)
'Add new title block reference
For Each oSheet In oNewDocument.Sheets
oSheet.Activate
Call oSheet.AddTitleBlock(oNewTitleBlockDef)
Next
oNewDocument.Save
oNewDocument.Close
Else
oNewDocument.Close
End If
Next
End Sub
Function LoadIdwPaths() As String()
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Filter = "All files|*.*|Csv files|*.csv|Text files|*.txt"
openFileDialog.FilterIndex = 2
If openFileDialog.ShowDialog <> DialogResult.OK Then Return New String() {}
Dim fileName = openFileDialog.FileName
'Select appropriate encoding,
'especially when you use some non-ASCII chars in path and file names
Dim encoding = System.Text.Encoding.UTF8
Dim lines = System.IO.File.ReadAllLines(fileName, encoding)
Return lines
End Function
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)