PDA

View Full Version : [SOLVED:] Check for and add "Option Explicit" and module level code programatically



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.

westconn1
01-23-2014, 03:12 AM
try like

With ThisDocument.VBProject.VBComponents("module2")
If Not .CodeModule.Find("Option Explicit", 1, 1, 1, 1) Then
.CodeModule.InsertLines 1, "option explicit"
End If
End With



note: this will find option explicit anywhere in the code module, including this line of code, changing the end positions did not seem to prevent that for some reason

gmaxey
01-23-2014, 09:32 AM
westconn1,

Thanks for the reply. I'm still tinkering with the overall code. I think I can make this work. No land mines found yet! I'll let you know and mark appropriately later today.

gmaxey
01-23-2014, 07:24 PM
westconn1,

While I am wondering if I have perhaps filleted this fish to death, I think I have achieved what I had hoped to achieve. Anyone interested in the problem behind the question, see: http://gregmaxey.mvps.org/word_tip_pages/mutually_exclusive_content_control_option_buttons.html

The objective was to add required code to a document "ThisDocument" module without wrecking what a user might already have there. I realize that most users will have nothing there to begin with, hence the suspected overkill.

fumei
01-24-2014, 12:08 AM
Sort of makes you miss the old ActiveX Option buttons where you could make them a Group and thus allow only one selection...

Just kidding.