CAD Forum - tips, tricks, discussion and utilities for AutoCAD, Inventor, Revit and other Autodesk products [www.cadforum.cz] ARKANCE | CONTACT - CZ | SK | EN | DE
Over 1.092.000 registered users (EN+CZ). AutoCAD tips, Inventor tips, Revit tips. Try the new precise Engineering calculator. New AutoCAD 2026 commands and variables.

CAD tip CAD tip # 14527:

Question

CAD 
 %  platform  category 
A
Use the following iLogic macro to easily search and bulk-replace arbitrary text in Inventor IDW or DWG drawings. You can use either regular expression mode or plain text string search/replace (see USEREGEX settings in line #2).

Text will be replaced in general notes, leader notes, dimensions, tables (except BOM/parts lists), title blocks and sketched symbols.

Create the following iLogic macro and run it on the active sheet in your Inventor drawing.

'Search/Replace drawing texts - 2025, www.cadforum.cz
Class ReplaceClass
Dim USEREGEX = True 'regular expressions, case sensitive OR plain substring, insensitive
Dim repcount As Integer = 0

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing document must be active", vbCritical, "")
		Exit Sub
	End If
	Dim oSheet As Sheet = ThisDrawing.Document.ActiveSheet
	Dim oTextBox As TextBox
	Dim oldText As String
	Dim newText As String
	Dim oldprompt As String = "case insensitive match, no wildcards"
	Dim newprompt As String = ""

	'prompt for search/replace strings:
	If USEREGEX Then
		oldprompt = "regular expression; case sensitive; use () for capture groups, \d as number, .* as wildcards, \ as escape"
		newprompt = "regular expression; use $1,$2 as capture groups, \ as escape"
	End If
	oldText = InputBox("String to find (old):" & vbCrLf & vbCrLf & oldprompt, "iLogic - Replace", "old")
	If oldText = "" Then Return 'exit rule
	newText = InputBox("String to replace '" & oldText & "' with:" & vbCrLf & vbCrLf & _
						newprompt & vbCrLf & vbCrLf & _
						"(replaces notes, leaders, dimensions, tables, sketches, titleblocks)", _
						"iLogic - Replace", "new")
	If newText = "" Then Return 'exit rule

	'error handling
	On Error Resume Next
	
	'look in General Notes
	For Each oGeneralNote In oSheet.DrawingNotes.GeneralNotes
		oText = oGeneralNote.FormattedText
		oText = ReplaceText(oText, oldText, newText)
		oGeneralNote.FormattedText = oText
	Next

	'look in Leader notes
	For Each oLeaderNote In oSheet.DrawingNotes.LeaderNotes
		oText = oLeaderNote.FormattedText
		oText = ReplaceText(oText, oldText, newText)
		oLeaderNote.FormattedText = oText
	Next

	'look in Dimensions
	For Each oDimension In oSheet.DrawingDimensions
		oText = oDimension.Text.FormattedText
		oText = ReplaceText(oText, oldText, newText)
		oDimension.Text.FormattedText = oText
	Next

	'look in Tables
	For Each oTable In oSheet.CustomTables
		oTable.Title = ReplaceText(oTable.Title, oldText, newText) ' Title
		Dim oRows As Inventor.Rows = oTable.Rows
		For Each oRow As Inventor.Row In oRows
			For Each oCell As Inventor.Cell In oRow
				oCell.Value =  ReplaceText(oCell.Value,oldText, newText) ' Cells
			Next 'oCell
		Next 'oRow
	Next

	'look in Title blocks
	Dim oTitleBlock = oSheet.TitleBlock
	For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
		oText = oTitleBlock.GetResultText(oTextBox)
		oText = ReplaceText(oText, oldText, newText)
		oTitleBlock.SetPromptResultText(oTextBox, oText)
	Next

	'look in Sketched symbols
	For Each oSymbol In oSheet.SketchedSymbols
		For Each oTextBox In oSymbol.Definition.Sketch.TextBoxes
			oText = oSymbol.GetResultText(oTextBox)
			oText = ReplaceText(oText, oldText, newText)
			oSymbol.SetPromptResultText(oTextBox, oText)
		Next
	Next
	
	MsgBox("Performed " & repcount & " replacements","Replacements")

End Sub

Function ReplaceText(oText As String, oldText As String, newText As String)
	Dim prev As String
	prev = oText
	If USEREGEX Then
		oText = System.Text.RegularExpressions.Regex.Replace(oText,oldText,newText)
		If oText <> prev Then repcount = repcount + 1
	Else
		If oText = oldText Or oText.Contains(oldText) Then
			oText = Replace(oText, oldText, newText)
			repcount = repcount + 1
		End If
	End If
	Return oText
End Function

End Class
Inventor
100% *CAD
6.5.2025    631×   FAQ  
Share the tip:
Digg it! Del.icio.us Facebook Twitter Google bookmark Technorati StumbleUpon
applies to: Inventor ·

See also:
Tip 14489:Multiline header of a parts list in an Inventor drawing.
Tip 14236:How to add color background (underlay mask) to Inventor dimension texts?
Tip 13911:How to print all sheets at once from Inventor?
Tip 13703:How to generate a dynamic QR code or Data Matrix on an Inventor drawing?
Tip 13599:How to read iProperties from Inventor models and drawings in AutoCAD DWGs?


Back    All CAD Tips



Have we helped you? If you want to support the CAD Forum web service, consider buying one of our CAD applications, or our custom software development offerings, or donating via PayPal (see above). You may also add a link to your web - like this "fan" link: CAD Forum - tips, utilities, blocks for Autodesk products
CAD: 
OS: 
Categ: 
Text:
 FAQ
  



Featuring:
Export 3D VRML scenes from AutoCAD and Inventor
VRMLout More info


Please use these tips at your own risk.
ARKANCE is not responsible for possible problems that may occur as a result of using any of these tips.
TOPlist