Title Block definition issues with VBA

Title Block definition issues with VBA

Anonymous
Not applicable
2,570 Views
14 Replies
Message 1 of 15

Title Block definition issues with VBA

Anonymous
Not applicable

Hey guys!

I am trying to create a macro that will go into my active dwg and change the cage code and revision history title for me to whatever I want (preferably pull it from a template but whatever works really).

 

the issue I'm having is getting the actual definition to change in the dwg. I think I am missing something simple but I can't seem to figure it out. Here is what I have so far-

 

 

Public Sub UpdateRevHistoryTitleandCageCode()

'get sheet
Dim oCurSheet As Sheet
Set oCurSheet = ThisApplication.ActiveDocument.ActiveSheet

'get revision table
Dim oRevTable As RevisionTable
Set oRevTable = oCurSheet.RevisionTables.Item(1)
'get title
Dim rhTitle As String
rhTitle = oRevTable.Title

'set title to something else
'rhTitle.Definition = test

'get title block
Dim oTitleBlock As TitleBlock
Set oTitleBlock = oCurSheet.TitleBlock
'get cage code
Dim cgCode As String
cgCode = oTitleBlock.Definition.Sketch.TextBoxes.Item(19).Text

MsgBox rhTitle & (" ") & cgCode

End Sub

 

everything that isnt commented out works fine, but i cant seem to get 'rhTitle.Definition = test to change the definition in my dwg. I can get it to change my variable but that's not what I want 

 

I found this (that I dont fully understand) 

 

https://forums.autodesk.com/t5/inventor-customization/change-title-block-using-visual-basic/td-p/374...

 

oTitleBlock = oSheet.AddTitleBlock(oTitle, , sPromptStrings)

 

where I believe sPromptStrings is my "insert override text here" and oSheet in this case is the active sheet?

Im a little lost here so any input would be much appreciated!

 

Thanks for your time,

DM

0 Likes
Accepted solutions (1)
2,571 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable

test is never defined.

if you want the text test in the rhTitleRev.Definition you'll need to enclose it in quotes.

If you want the a variable called test in the rhTitleRev.Definition you'll need to define it.

 

 

hope this helps

kl

 

0 Likes
Message 3 of 15

MechMachineMan
Advisor
Advisor

If you are not planning on replacing a title block with a completely different one, you don't need to use the Add method.

 

Also, your idea to use the RevisionTable.Title.Definition is wrong.

 

RevisionTable.Title is a string, and thus does not have a 'definition' method available on it.

http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-389727B4-C159-4502-8E9C-12E883EBCC03

 

Your fixed code is as below:

Notes:

- Comments that explain 'what' aren't really useful. The line "ThisApplication.ActiveDocument.ActiveSheet" LITERALLY says it's getting the active sheet. The comment saying "the below line that says it's getting the active sheet is getting the active sheet" is redundant and just makes there be more lines of code read.

- Secondly, if you are only accessing an object once, you typically don't need to assign it to it's own variable (ie; I don't need to assign the titleblock to it's own object if I'm only grabbing 1 child of the titleblock object once).

 

Public Sub UpdateRevHistoryTitleandCageCode()

    Dim oCurSheet As Sheet
    Set oCurSheet = ThisApplication.ActiveDocument.ActiveSheet

    Dim oRevTable As RevisionTable
    Set oRevTable = oCurSheet.RevisionTables.Item(1)
    
Call MsgBox("The current rev table title is: " & oRevTable.Title)
oRevTable.Title = "test"

Msgbox "The revision table title was just changed. It is now: " & oRevTable.Title Dim cgCodeTB as TextBox Set cgCodeTB = oCurSheet.TitleBlock.Definition.Sketch.TextBoxes.Item(19) Call MsgBox ("Old cgcodetb = " & cgCodeTb.Text)

cgCodeTb.Text = "new cg code"

Call Msgbox("The new cgcode is: " cgCodeTb.Text")

Call Msgbox("The cg code changing actually just changed your definition. This is not standard practice." & vblf & _ "If you want variable text in your titleblock, you should be using prompted entries." & vblf & vblf & _
"Use programming help or google to look up 'GetResultText' and 'SetPromptResultText' to learn more on using prompted entries via iLogic & vba & vb.net")
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 4 of 15

Anonymous
Not applicable

@Anonymous, I keep getting Invalid Qualifier error; but good catch!

0 Likes
Message 5 of 15

Anonymous
Not applicable

@MechMachineMan, thanks for the input.

it works well for the oRevTable, but when I try this

 

Dim oTitleBlock As TitleBlock
Set oTitleBlock = oCurSheet.TitleBlock

Dim cgCodeTB As TextBox
Set cgCodeTB = oTitleBlock.Definition.Sketch.TextBoxes.Item(19)
cgCodeTB.Text = "stuff"


MsgBox ("Revision History Title and Cage Code have been updated. ")
MsgBox ("current cage code is ") & cgCodeTB.Text

 

I keep getting

runtime error -2147467259 (80004005)

Method 'Text' of object 'TextBox' failed

 

I just switched to 2019, but I doubt that's relevant

0 Likes
Message 6 of 15

Anonymous
Not applicable

@MechMachineMan,

Thanks for the input!

 

I keep getting 

METHOD TEXT OF TEXTBOX FAILED error message with the following

 

Dim oTitleBlock As TitleBlock
Set oTitleBlock = oCurSheet.TitleBlock

Dim cgCodeTB As TextBox
Set cgCodeTB = oTitleBlock.Definition.Sketch.TextBoxes.Item(19)
cgCodeTB.Text = "0K8A4"

 

it works fine for the oRevtable Title, but here i get a runtime error -2147467259(80004005)

0 Likes
Message 7 of 15

MechMachineMan
Advisor
Advisor

@Anonymous

 

Have you looked into my suggestion above?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 8 of 15

Anonymous
Not applicable

@MechMachineMan for some reason, My replies dont show up. its happened twice now.

 

i keep getting 

Method 'text' of object 'textbox' failed

with a runtime error -2147467259 (80004005)

 

same thing happens with oRevTable.title sometimes (not all the time)

 

I think part of the problem is that not every title block is the same; we have older drawings that need to be updated and doing it manually is a pain. i think instead of hard coding the item of the collection object, I need to search for whatever item has the title "cage code" and rename that way. of course thats all well but i cant even get the current one to work Smiley LOL

 

I've been using the code you suggested and staring at that link for a while but I dont see what i'm doing wrong. 

0 Likes
Message 9 of 15

MechMachineMan
Advisor
Advisor

Hmm. That is interesting about the hidden posts. I definitely didn't see them until now!

 

This also seems like a bug to me... maybe @chandra.shekar.g could take a look into it?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 10 of 15

Anonymous
Not applicable

So this is what I have so far 

 

Public Sub ExecuteOrder66()

 

     'get sheet
Dim oCurSheet As Sheet
Set oCurSheet = ThisApplication.ActiveDocument.ActiveSheet

     'get revision table
Dim oRevTable As RevisionTable
Set oRevTable = oCurSheet.RevisionTables.Item(1)
     'change title
oRevTable.Title = "revision stuff"

      'get title block
Dim oTitleBlock As TitleBlock
Set oTitleBlock = oCurSheet.TitleBlock
     'get cage code text box and redefine its text
Dim cgCodeTB As TextBox
Set cgCodeTB = oTitleBlock.Definition.Sketch.TextBoxes.Item(19)
'cgCodeTB.Text = "other stuff"

MsgBox ("Revision History Title and Cage Code have been updated.") & ("Current cage code is ") & cgCodeTB.Text

End Sub

 

the line 'cgCodeTB.Text = "other stuff" has been commented out because I always get the run-time error.

 

Dumb question- is there a way to identify when a title block in the drawing resources was created? I ask because we have older title blocks with different definitions that we routinely have to replace (Pre 2007 and such). I want to add an if statement after the rev history title stuff, that will check what version of title block is in the drawing. If it is an older title block, it will be replaced with a different one off of the standard template I have. If it is a newer one, then it will simply rewrite the text as it is supposed to with the above code.

 

0 Likes
Message 11 of 15

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

In order to edit TittleBlock, need to enter edit mode of TittleBlockDefinition and then text can be edited. Try below VBA code to edit tittle of TitleBlockDefintion.

 

 

Public Sub ExecuteOrder66()

    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oCurSheet As Sheet
    Set oCurSheet = oDoc.ActiveSheet
    
    Dim oRevTable As RevisionTable
    Set oRevTable = oCurSheet.RevisionTables.Item(1)
    
    oRevTable.Title = "revision stuff"
    
    Dim oTitleBlock As TitleBlockDefinition
    Set oTitleBlock = oCurSheet.TitleBlock.Definition
     
    
    Dim oSketch As DrawingSketch
    Set oSketch = oTitleBlock.Sketch
    
    Call oTitleBlock.Edit(oSketch)
    
    Dim cgCodeTB As TextBox
    Set cgCodeTB = oSketch.TextBoxes.Item(19)
    cgCodeTB.Text = "Other stuff"
     
    Call oTitleBlock.ExitEdit 
MsgBox ("Revision History Title and Cage Code have been updated.") & ("Current cage code is ") & cgCodeTB.Text End Sub

To get file version of inventor, try the solution provided in below link.

 

https://forums.autodesk.com/t5/inventor-customization/determine-inventor-file-version/td-p/5042998

 

@MechMachineMan - Thanks for notifying me in post.

 

Please feel free to contact if there is any queries.

 

If solves problem, click on "Accept as solution" / give a "Like".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 12 of 15

Anonymous
Not applicable

@chandra.shekar.g

 

Nice! This lets me edit what I need. However, the text size then reverts back to a default size. How do I edit this so it doesnt change the text size? Or is there a way to specify what size I want?

0 Likes
Message 13 of 15

MechMachineMan
Advisor
Advisor

 

@chandra.shekar.g Ahh yes, that makes sense.

 

@Anonymous I think this tweak should work as long as all of the text within the textbox is formatted the same and there are no words or letters formatted separately.

 

Public Sub ExecuteOrder66()

    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oCurSheet As Sheet
    Set oCurSheet = oDoc.ActiveSheet
    
    Dim oRevTable As RevisionTable
    Set oRevTable = oCurSheet.RevisionTables.Item(1)
    
    oRevTable.Title = "revision stuff"
    
    Dim oTitleBlock As TitleBlockDefinition
    Set oTitleBlock = oCurSheet.TitleBlock.Definition
     
    
    Dim oSketch As DrawingSketch
    Set oSketch = oTitleBlock.Sketch
    
    Call oTitleBlock.Edit(oSketch)
    
    Dim cgCodeTB As TextBox
    Set cgCodeTB = oSketch.TextBoxes.Item(19)
    replacetext = cgCodeTB.Text
    cgCodeTB.FormattedText = cgCodeTB.FormattedText.Replace(replacetext, "New Stuff")
Call oTitleBlock.ExitEdit
MsgBox ("Revision History Title and Cage Code have been updated.") & ("Current cage code is ") & cgCodeTB.Text End Sub  

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 14 of 15

Anonymous
Not applicable

@MechMachineMan

 

i get a compile error - invalid qualifier with 

 

Dim cgCodeTB As TextBox
Set cgCodeTB = oSketch.TextBoxes.Item(19)
'cgCodeTB.Style.FontSize = (0.25 * 2.54)
'cgCodeTB.StyleOverride?
replacetext = cgCodeTB.Text
'cgCodeTB.Text = "stuff"
cgCodeTB.FormattedText = cgCodeTB.FormattedText.Replace(replacetext, "new")

 

Part of the problem is that this textbox uses the same style as a few others, and changing this font (by style) will also change the others. can i override style's font size with the formatted text function? I've seen stuff like 

 

oTextBox.FormattedText = "<StyleOverride 
FontSize = '.6096'>" & "some text" & 
"</StyleOverride>"

 

but I dont understand the carats., or the syntax

0 Likes
Message 15 of 15

MechMachineMan
Advisor
Advisor

You shouldn't need to understand the carrats, but utilizing formatted text is exactly the method this uses....

 

That error is simply because I used the vb.net replace method instead of the vba one...

 

Public Sub ExecuteOrder66()

    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oCurSheet As Sheet
    Set oCurSheet = oDoc.ActiveSheet
    
    Dim oRevTable As RevisionTable
    Set oRevTable = oCurSheet.RevisionTables.Item(1)
    
    oRevTable.Title = "revision stuff"
    
    Dim oTitleBlock As TitleBlockDefinition
    Set oTitleBlock = oCurSheet.TitleBlock.Definition
     
    
    Dim oSketch As DrawingSketch
    Set oSketch = oTitleBlock.Sketch
    
    Call oTitleBlock.Edit(oSketch)
    
    Dim cgCodeTB As TextBox
    Set cgCodeTB = oSketch.TextBoxes.Item(19)
    replacetext = cgCodeTB.Text
    cgCodeTB.FormattedText = Replace(cgCodeTB.FormattedText, replacetext, "New Stuff")

    Call oTitleBlock.ExitEdit 
  
    MsgBox ("Revision History Title and Cage Code have been updated.") & ("Current cage code is ") & cgCodeTB.Text

End Sub  

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type