Here is an example of what I mean (this is Excel, should be modifiable)
Option Explicit
Const vbext_ct_StdModule As Long = 1
Const vbext_ct_ClassModule As Long = 2
Const vbext_ct_MSForm As Long = 3
Private Sub Workbook_Open() '<<<<<<<< change this to whatever fires on Word document open
Dim VBProject As Object
Dim VBMod As Object
Dim mStartLine As Long
Dim mLine As String
Dim mModuleDone As Boolean
Set VBProject = Application.VBE.ActiveVBProject
If Not VBAIsTrusted(VBProject) Then
MsgBox "You need trusted access", vbOKOnly, "Conditionals"
Else
If VBProject Is ThisWorkbook.VBProject Then '<<<< change ThisWorkbook to ThisDocument
With VBProject
For Each VBMod In .VBComponents
If VBMod.Type = vbext_ct_ClassModule Or VBMod.Type = vbext_ct_MSForm Or VBMod.Type = vbext_ct_StdModule Then
With VBMod.codemodule
mStartLine = 1
mModuleDone = False
Do Until mModuleDone
mModuleDone = mStartLine > .CountOfLines
If Not mModuleDone Then
mLine = .Lines(mStartLine, 1)
If mLine Like "*Const Version = *" Then
.DeleteLines mStartLine, 1
End If
mStartLine = mStartLine + 1
End If
Loop
'insert a conditional compilation line
.InsertLines .CountOfDeclarationLines + 1, "#Const Version = " & Val(Application.Version)
End With
End If
Next VBMod
End With
End If
End If
End Sub
'-----------------------------------------------------------------
Private Function VBAIsTrusted(ByRef Project As Object) As Boolean
'-----------------------------------------------------------------
Dim mpVBC As Object
Dim mpAlerts As Boolean
mpAlerts = Application.DisplayAlerts
Application.DisplayAlerts = False
On Error Resume Next
Set mpVBC = Project.VBComponents.Item(1)
On Error GoTo 0
Application.DisplayAlerts = mpAlerts
VBAIsTrusted = Not mpVBC Is Nothing
End Function