gmaxey
01-22-2014, 09:14 AM
I know how to check a VB project for a named procedure and add it programmatically if required.
What I struggling with is how to check for and add the "Option Explicit" statement, an API and an Enum procedure at the module level if they are not already there.
Since I don't know how, I'm having to drive a nail with sledge hammer.:crying: Instead of only adding the code if required, I'm currently just deleting all of the code in the code pane and adding it like this:
Sub AddModuleLevelCode(ByVal oDoc As Word.Document)
Dim stdModule As VBComponent
Dim i As Long
Dim strCode As String
With oDoc.VBProject
Set stdModule = .VBComponents("ThisDocument")
'Clear existing code.
For i = stdModule.CodeModule.CountOfLines To 1 Step -1
stdModule.CodeModule.DeleteLines i, 1
Next i
strCode = "Option Explicit" & vbLf _
& "Public p_oCCMonitored As ContentControl" & vbLf _
& "Private Declare Function GetKeyState Lib ""user32"" (ByVal nVirtKey As Long) As Integer" & vbLf _
& "Private Enum GotFocusHow" & vbLf _
& " TabKey = 1" & vbLf _
& " ShiftTabKeys = 4" & vbLf _
& " LeftMouseBtn = 3" & vbLf _
& " AltKey = 2" & vbLf _
& "End Enum" & vbLf
stdModule.CodeModule.AddFromString strCode
Set stdModule = Nothing
End With
End Sub
Then I'm then using a similar procedure to add additional named procedures.
What I am trying to do is:
1. Check for existence of "Option Explicit" on line 1. If it isn't there then add it.
2. Check for existence of the API. If it is not there then add it at the module level
3. Check for the existence of the type Enum. If it is not then then add it at the module level.
Then I can check for each of the other named procedures and only add them if required.
Thanks.
What I struggling with is how to check for and add the "Option Explicit" statement, an API and an Enum procedure at the module level if they are not already there.
Since I don't know how, I'm having to drive a nail with sledge hammer.:crying: Instead of only adding the code if required, I'm currently just deleting all of the code in the code pane and adding it like this:
Sub AddModuleLevelCode(ByVal oDoc As Word.Document)
Dim stdModule As VBComponent
Dim i As Long
Dim strCode As String
With oDoc.VBProject
Set stdModule = .VBComponents("ThisDocument")
'Clear existing code.
For i = stdModule.CodeModule.CountOfLines To 1 Step -1
stdModule.CodeModule.DeleteLines i, 1
Next i
strCode = "Option Explicit" & vbLf _
& "Public p_oCCMonitored As ContentControl" & vbLf _
& "Private Declare Function GetKeyState Lib ""user32"" (ByVal nVirtKey As Long) As Integer" & vbLf _
& "Private Enum GotFocusHow" & vbLf _
& " TabKey = 1" & vbLf _
& " ShiftTabKeys = 4" & vbLf _
& " LeftMouseBtn = 3" & vbLf _
& " AltKey = 2" & vbLf _
& "End Enum" & vbLf
stdModule.CodeModule.AddFromString strCode
Set stdModule = Nothing
End With
End Sub
Then I'm then using a similar procedure to add additional named procedures.
What I am trying to do is:
1. Check for existence of "Option Explicit" on line 1. If it isn't there then add it.
2. Check for existence of the API. If it is not there then add it at the module level
3. Check for the existence of the type Enum. If it is not then then add it at the module level.
Then I can check for each of the other named procedures and only add them if required.
Thanks.