Weirdly this works but it's copying the external rule into 'After Save Document', not before, even tho youro code clearly seems to state 'before'

Similar to the other chaps code, If I try and add this to an existing external txt file that is run with a button, it throws a bunch of errors. Could this be because of the Sub Main grouping? The rest of the code in this text file seems to run OK without grouping.
Text file below (your code is added to the end of other functions)
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE updates the drawing style, deletes drawing templates and unsed drawing resources, then re-inserts clean template
'access styles manager
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oStyles As Inventor.DrawingStylesManager
oStyles = oDrawDoc.StylesManager
'update all styles by iterating through them
Dim iStyles As Integer
For iStyles = 1 To oStyles.Styles.Count
If oStyles.Styles.Item(iStyles).UpToDate = False Then
oStyles.Styles.Item(iStyles).UpdateFromGlobal
End If
Next
iStyles = 0
'delete sheet formats
ThisDrawing.ResourceFileName = "E:\_Vault_Workspace\Templates\KDS-Windsor ANSI B (Imperial).dwg"
ThisDrawing.KeepExtraResources = True
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveEditDocument
Dim oCurrentNumber As Sheet
oCurrentNumber = oDoc.ActiveSheet
' Iterate through the sheets, and delete the title blocks and symbols
For Each oSheet In oDoc.Sheets
oSheet.Activate
Try
oSheet.TitleBlock.Delete
Catch
'catch error if no TitleBlock found
End Try
Try
oSheet.Border.Delete
Catch
'catch error if no border found
End Try
Dim doc As DrawingDocument = ThisDoc.Document
'Delete un-used Sheets
For Each sheet As Sheet In doc.Sheets
If (Sheet.DrawingViews.Count = 0) Then
Sheet.Delete()
End If
Next
'Delete Sheet Formats
For Each sheetFormat As SheetFormat In doc.SheetFormats
SheetFormat.Delete()
Next
'Delete unused TitleBlocks
For Each titleBlockDefinition As TitleBlockDefinition In doc.TitleBlockDefinitions
If (Not TitleBlockDefinition.IsReferenced) Then
TitleBlockDefinition.Delete()
End If
Next
'Delete unused Borders
For Each borderDefinition As BorderDefinition In doc.BorderDefinitions
If ((Not BorderDefinition.IsReferenced) And (Not BorderDefinition.IsDefault)) Then
BorderDefinition.Delete()
End If
Next
'Delete un-used Sketch Symbols
For Each sketchedSymbolDefinition As SketchedSymbolDefinition In doc.SketchedSymbolDefinitions
If (Not SketchedSymbolDefinition.IsReferenced) Then
SketchedSymbolDefinition.Delete()
End If
Next
'set new TitleBlock
ActiveSheet.TitleBlock = "KDS-Windsor Title 'B'"
'set new border
ActiveSheet.Border = "KDS-Windsor Border 'B'"
Next
'set back to original sheet
oCurrentNumber.Activate
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE asks if you want to update the Drawing units, and pushes it to DRAWING iProp "UNITS" which is used in the title block.
'Promt the user to answer yes or no
myquestion = MessageBox.Show("Define Drawing UNITS?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
'If they answered YES
If myquestion = vbYes Then
'Promt the user for the value of a variable
myprop = InputRadioBox("Choose Drawing Units", "mm", "ft", true, Title :="Drawing Units")
'myprop = InputBox("Please Enter the UNITS Here ( mm or ft )", " ", "mm")
If myprop = Yes
iProperties.Value("Custom", "UNITS") = "ft"
Else
iProperties.Value("Custom", "UNITS") = "mm"
End If
'assign the value of that variable to a custom property
'Thank the user for filling out the custom property
'MessageBox.Show("Great Job!!", "GOLD STAR")
'If they answered NO
Else
'MessageBox.Show("Well.. Thats ok. Maybe next time", "Don't forget it!")
'You must close out the IF statement
End If
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE pulls "Title" and "Summary" from the part/assy and pushes it into the same DRAWING iProps.
'Push Part/Assy iProperties to Drawing iProperties
mDoc = ThisDrawing.ModelDocument
titleProp = mDoc.PropertySets.Item("Summary Information").Item("Title")
iProperties.Value("Summary", "Title") = titleProp.Value
subjProp = mDoc.PropertySets.Item("Summary Information").Item("Subject")
iProperties.Value("Summary", "Subject") = subjProp.Value
descProp = mDoc.PropertySets.Item("Design Tracking Properties").Item("Description")
iProperties.Value("Project", "Description") = descProp.Value
'
'Forces update after rule runs
iLogicVb.UpdateWhenDone = True
'--------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE asks if you want dimension precision set to zero, and either does so or moves on.
Dim oDrawingDoc as DrawingDocument
DWG = MessageBox.Show("To Zero?", "Set Dimension Precision?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If DWG = vbYes Then
Dim Doc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim s As Sheet = Doc.ActiveSheet
Dim dims As Inventor.DrawingDimensions = s.DrawingDimensions
For Each d As Inventor.DrawingDimension In dims
Try
If d.Precision = 1 Then d.Precision = 0
Catch
End Try
Next
Else
'do nothing
End If
'--------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------
'Add Title to Drawing Event Trigger
Sub Main
Dim oDoc As Inventor.Document = ThisDoc.Document
'required for external rule names in these Event Triggers properties
Dim sExternalRulePrefix As String = "file://"
Dim sExtRuleName As String = sExternalRulePrefix & "Title to Drawing"
'required to prepare document (it must have DocumentInterest for iLogic add-in)
DocumentEventTriggersPreparations(oDoc, True)
'event internal name is different than what you see in Event Triggers dialog
Dim sEventInternalName As String = "BeforeDocSave"
Dim oSet As Inventor.PropertySet = Nothing
Dim sSetDefaultName As String = "_iLogicEventsRules"
'this internal name must never be different, or it will not work
Dim sSetInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
If Not oDoc.PropertySets.PropertySetExists(sSetInternalName, oSet) Then
oSet = oDoc.PropertySets.Add(sSetDefaultName, sSetInternalName)
End If
Dim oProp As Inventor.Property = Nothing
'PropID range is unique/specific to each specific event
For iPropID As Integer = 800 To 899 'range is specific to each event
Try
oProp = oSet.ItemByPropId(iPropID)
If oProp.Value = sExtRuleName Then
'MsgBox("This rule has already been added to this event trigger. Exiting.", vbExclamation, "")
Logger.Info("This rule has already been added to this event trigger.")
Exit For 'exit this loop/iteration
End If
Continue For 'there is already an existing rule at this iPropID, so skip to next
Catch
'no property was found with that PropID, and this rule has not been found, so now create one for this rule
oProp = oSet.Add(sExtRuleName, sEventType & iPropID, iPropID)
Exit For 'exit the loop, because we have already added the rule to the event
End Try
Next
DocumentEventTriggersPreparations(oDoc, False)
End Sub
Sub DocumentEventTriggersPreparations(oDoc As Inventor.Document, bBefore As Boolean)
Dim iLogicClientId As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
Dim sTempRuleName As String = "DeleteMe"
If bBefore Then
If oDoc.DocumentInterests.HasInterest(iLogicClientId) = False Then
iLogicVb.Automation.AddRule(oDoc, sTempRuleName, "")
End If
Else
If iLogicVb.Automation.GetRule(oDoc, sTempRuleName) IsNot Nothing Then
iLogicVb.Automation.DeleteRule(oDoc, sTempRuleName)
End If
End If
End Sub