Hi,
Please see below for the final code for now.
Public Sub Main SketchTextAdd()
' Confirm Document Type is Drawing
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Drawing Documents.", vbOKOnly, "WRONG DOCUMENT TYPE")
Return
End If
' a reference to the drawing document.
' This assumes a drawing document is active.
oDrawDoc = ThisApplication.ActiveDocument
' Create a new sketch on the active sheet.
Dim oSketch As DrawingSketch
Dim oDrawSketch As DrawingSketch
' Open the sketch for edit so the text boxes can be created.
' This is only required for drawing sketches, not part.
Dim oTextBox As Inventor.TextBox
' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.
Dim SkName As String
SkName = "Sketch_Title"
Dim oDoc As DrawingDocument
'Identify border type
Dim oBorder = ActiveSheet.Border
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
Dim sText As String
' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.
'Trying new sketch catch addition to the program
Dim oSheet As Inventor.Sheet = ThisDrawing.Document.ActiveSheet
Dim oExistingText As String
Dim oNewText As String
'set the value of the oSketch variable
If oSheet.Sketches.Count = 0 Then
oSketch = oDrawDoc.ActiveSheet.Sketches.add
oSketch.Name = SkName
Else
oSketch = oSheet.Sketches.Item(1) 'since you said it will be the first sketch on the sheet
For Each oSketch In oSheet.Sketches
If oSketch.Name.Contains(SkName) Then 'I believe this is Case-Sensitive
Exit For 'it will retain the right sketch as the value of oSketch
End If
Next
End If
'Set the value Of the oTextBox variable
If oSketch IsNot Nothing Then
If oSketch.TextBoxes.Count = 0 Then
If oNewTextBox = vbNo Then Return 'or Exit Sub (we don't need to check for vbYes now)
oSketch.Edit
'Create a new sketch on the active sheet.
If oBorder = "A3" Then
Dim A3_A4_A3P_Style As TextStyle
A3_A4_A3P_Style = oDrawDoc.StylesManager.TextStyles.Item("A3_A4_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(22.5, 3.3), oTG.CreatePoint2d(34.25, 2.5), sText, A3_A4_P_Style)
Else If oBorder = "A4" Then
Dim A3_A4_A3P_Style As TextStyle
A3_A4_A3P_Style = oDrawDoc.StylesManager.TextStyles.Item("A3_A4_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.19, 3.3), oTG.CreatePoint2d(22.0, 2.5), sText, A3_A4_P_Style)
Else If oBorder = "A4_P" Then
Dim A4_P_Style As TextStyle
A4_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A4_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.95, 6.40), oTG.CreatePoint2d(19.95, 5.5), sText, A4_P_Style)
Else If oBorder = "A3_P" Then
Dim A3_A4_A3P_Style As TextStyle
A3_A4_A3P_Style = oDrawDoc.StylesManager.TextStyles.Item("A3_A4_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(10.19, 3.3), oTG.CreatePoint2d(22, 2.5), sText, A3_A4_P_Style)
Else If oBorder = "A2" Then
Dim Altrad_MB_A0_A1_A2_P_Style As TextStyle
A0_A1_A2_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A0_A1_A2_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(47.65, 9.45), oTG.CreatePoint2d(57.8, 8.2), sText, A0_A1_A2_P_Style)
Else If oBorder = "A1" Then
Dim A0_A1_A2_P_Style As TextStyle
A0_A1_A2_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A0_A1_A2_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(72.35, 9.45), oTG.CreatePoint2d(82.5, 8.2), sText, A0_A1_A2_P_Style)
Else If oBorder = "A0" Then
Dim A0_A1_A2_P_Style As TextStyle
A0_A1_A2_P_Style = oDrawDoc.StylesManager.TextStyles.Item("A0_A1_A2_P_Style")
sText = InputBox("Enter new Title Text", "Secondary Title ", "")
oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(107.15, 9.45), oTG.CreatePoint2d(117.3, 8.2), sText, A0_A1_A2_P_Style)
End If
Else
oTextBox = oSketch.TextBoxes.Item(1) 'since it is going to be the only TextBox in the sketch
If oTextBox.Text = "" Then
oSketch.Edit
sText = InputBox("Enter new Title Text.", "Secondary Title.")
oTextBox.Text = sText
Else
oExistingText = oTextBox.Text
oSketch.Edit
sText = InputBox("Enter new Title Text.", "Secondary Title.", oExistingText)
Try
oTextBox.Text = sText
Catch oEx As Exception
'You could ask the user if they want to just delete and recreate the TextBox, then loop or GoTo
MsgBox("Error occurred while trying to set the value of the entered text to the TextBox." & vbCrLf & _
"The Error Message Is as follows:" & vbCrLf & _
oEx.Message & vbCrLf & vbCrLf & _
"The StackTrace is as follows:" & vbCrLf & _
oEx.StackTrace, vbOKOnly, " ")
End Try
End If
End If
End If
'-----------------------------------------------------------------------------------
'MessageBox.Show("This is going to check the number of character in the second title")
Fitcheck :
'Set maximum Charactor value
Dim Count As String
'Dim CurrentLength As Integer
Dim MaxCount As Integer
'MessageBox.Show("Getting the MaxCount")
If oBorder = "A4_P" Then
MaxCount = 150
ElseIf oBorder = "A4" Then
MaxCount = 154
ElseIf oBorder = "A3" Then
MaxCount = 154
ElseIf oBorder = "A3_P" Then
MaxCount = 154
ElseIf oBorder = "A2" Then
MaxCount = 178
ElseIf oBorder = "A1" Then
MaxCount = 178
ElseIf oBorder = "A0" Then
MaxCount = 178
End If
Count = oTextBox.Text
'MessageBox.Show("Checking the text length against the MaxCount")
If Count.Length > MaxCount Then
MessageBox.Show("The title currently exceeds the " & MaxCount & " character limit." & vbLf & _sText & "Total count = " & Count.Length & " characters", "iLogic")
sText = InputBox("Enter new Title Text", "Secondary Title ", oTextBox.Text)
oTextBox.Text = sText
GoTo Fitcheck
ElseIf Count.Length <= MaxCount Then
Stop
End If
Dim ActSheet As Sheet
Dim SheetNumber As String
Dim aSNum As Integer
'FOr Each ActSheet In ThisApplication.ActiveDocument.Sheets
SheetNumber = Mid(oSheet.Name, InStr(1, oSheet.Name, ":") + 1)
If SheetNumber<10 Then
iProperties.Value("Custom", "Second Title Sheet:0" & SheetNumber) = sText
'aSNum = ("0" & SheetNumber)', oSheet.Name
Else
iProperties.Value("Custom", "Second Title Sheet:" & SheetNumber) = sText
'aSNum = (SheetNumber)', oSheet.Name)
End If
'Next
'MessageBox.Show("This is going to update the custom iProperty")
'iProperties.Value("Custom", "Second Title " & aSNum) = sText
oSketch.ExitEdit
End Sub A big thank you to all that helped.