Execute code on all open drawings (idw)

Execute code on all open drawings (idw)

tobias
Collaborator Collaborator
365 Views
6 Replies
Message 1 of 7

Execute code on all open drawings (idw)

tobias
Collaborator
Collaborator

Hi,

 

I have a lot of drawings where I need to change the TitleBlock and I als need to delete a few old TitleBlocks from the drawing.

 

To change the TitleBlock for a single idw I have the following working code but it needs to through all open idw's.

 

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet
Dim sSheetName As String
 
For Each oSheet In oDoc.Sheets
oSheet.Activate

'If TitleBlock is A, switch to B
If ActiveSheet.TitleBlock="A" Then
ActiveSheet.TitleBlock = "B"


Else
'do nothing
End If

Next
Tobias
The Netherlands
Inventor Pro 2025, Vault Pro 2025, AutoCad Electrical 2025.

If a response answers your question, please use ACCEPT SOLUTION to assist other users later.
0 Likes
Accepted solutions (1)
366 Views
6 Replies
Replies (6)
Message 2 of 7

Andrii_Humeniuk
Advisor
Advisor

Hi @tobias . To access documents that are already open, you need to use the Inventor.Documents method. Example:

Dim oInvApp As Inventor.Application = ThisApplication
For Each oDDoc As DrawingDocument In oInvApp.Documents.OfType(Of DrawingDocument)
	Dim sSheetName As String
	For Each oSheet As Sheet In oDDoc.Sheets
		oSheet.Activate()	
		'If TitleBlock is A, switch to B
		If ActiveSheet.TitleBlock="A" Then
			ActiveSheet.TitleBlock = "B"	
		Else
		'do nothing
		End If	
	Next	
Next 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 7

tobias
Collaborator
Collaborator

Thanks @Andrii_Humeniuk,

 

But it still does only the current drawing, not all open drawings.

 

 

Tobias
The Netherlands
Inventor Pro 2025, Vault Pro 2025, AutoCad Electrical 2025.

If a response answers your question, please use ACCEPT SOLUTION to assist other users later.
0 Likes
Message 4 of 7

Andrii_Humeniuk
Advisor
Advisor

Sorry, you are right, you need to add the oDDoc.Activate() method. See the example below.

Dim oInvApp As Inventor.Application = ThisApplication
For Each oDDoc As DrawingDocument In oInvApp.Documents.OfType(Of DrawingDocument)
	oDDoc.Activate()
	Dim sSheetName As String
	For Each oSheet As Sheet In oDDoc.Sheets
		oSheet.Activate()	
		'If TitleBlock is A, switch to B
		If ActiveSheet.TitleBlock="A" Then
			ActiveSheet.TitleBlock = "B"	
		Else
		'do nothing
		End If	
	Next	
Next 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 5 of 7

tobias
Collaborator
Collaborator

Hi @Andrii_Humeniuk,

 

It now goes through all open drawings 👍

but it only executes the TitleBlock change on the first open drawing.

 

 

Thanks for your help!

Tobias
The Netherlands
Inventor Pro 2025, Vault Pro 2025, AutoCad Electrical 2025.

If a response answers your question, please use ACCEPT SOLUTION to assist other users later.
0 Likes
Message 6 of 7

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Then your code should look like this:

Dim oInvApp As Inventor.Application = ThisApplication
For Each oDDoc As DrawingDocument In oInvApp.Documents.OfType(Of DrawingDocument)
	oDDoc.Activate()
	Dim oBlock As TitleBlockDefinition
	oBlock = TryCast(oDDoc.TitleBlockDefinitions("B"), TitleBlockDefinition)
	If oBlock Is Nothing Then
		MessageBox.Show("Failed to get TitleBlock in active document.", "Error")
		Continue For
	End If
	Dim sSheetName As String
	For Each oSheet As Sheet In oDDoc.Sheets
		oSheet.Activate()
		'If TitleBlock is A, switch to B
		If oSheet.TitleBlock.Name = "A" Then
			oSheet.TitleBlock.Delete()
			oSheet.AddTitleBlock(oBlock)
		Else
		'do nothing
		End If	
	Next	
Next 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 7 of 7

tobias
Collaborator
Collaborator

Hi @Andrii_Humeniuk,

 

I'm impressed. Thank you so much, works great!

 

 

Tobias
The Netherlands
Inventor Pro 2025, Vault Pro 2025, AutoCad Electrical 2025.

If a response answers your question, please use ACCEPT SOLUTION to assist other users later.
0 Likes