I am using the following code to delete lines of code that I previously added:
Public Sub RemoveShowMeButtonCode()
Dim P As Integer
Dim lngCodeLineCt As Integer
Dim ButtonClickCodeLineCt As Integer
lngCodeLineCt = 0
GlobalTemplatePathFileName = “E:My DocumentsGoalsFormatDocuments.dot”
Set GlobTemp = Templates(GlobalTemplatePathFileName)
Set objCurProj = GlobTemp.VBProject
Set FindListFormatModule = objCurProj.VBComponents(“frmAnalyzeListFormat”)
With FindListFormatModule.CodeModule
lngCodeLineCt = FindListFormatModule.CodeModule.CountOfLines
ButtonClickCodeLineCt = lngCodeLineCt – 41
If lngCodeLineCt > 41 Then
FindListFormatModule.CodeModule.DeleteLines 1, Count:=ButtonClickCodeLineCt
End If
End With
End Sub
This code works perfectly if I run it manually (from the Visual Basic Editor in Word). HOWEVER, if I run it from the click event of the Cancel button on the form, it get this error:
“Compile Error: Only comments may appear after End Sub, End Function, or End Property”
This is the code that it is deleting. The green section is what is highlighted when the error box appears, not that it makes much sense:
Public Sub MyCommandButton1_17_Click()
P = 17
FindListExample
End Sub
Public Sub MyCommandButton1_16_Click()
P = 16
FindListExample
End Sub
.
.(See the progression from 17 to 1)
.
Public Sub MyCommandButton1_1_Click()
P = 17
FindListExample
End Sub
Are there better ways of doing this, like using Class Modules? Yes, but I haven’t got time to learn that like I need to at this point. Everything about my code works beautifully except for this and I don’t want (or have time) to reinvent what I have.
Just occured to me that I should also put in my click event code:
Private Sub cmbCancel_Click()
If BulletsNumbersInTextAnalyzed = False Then
RemoveShowMeButtonCode
Erase ListFormatArray
‘Remove all labels and comboboxes that shouldn’t be there.
On Error Resume Next
For M = 0 To frmAnalyzeListFormat.Controls.Count – 1
On Error Resume Next
frmAnalyzeListFormat.Controls.Remove (M)
Next M
End If
frmAnalyzeListFormat.Hide
End Sub
Also, the following is the code I’m using to add the code:
GlobalTemplatePathFileName = “E:My DocumentsGoalsFormatDocuments.dot” ‘ActiveDocument.AttachedTemplate.FullName
Set GlobTemp = Templates(GlobalTemplatePathFileName)
Set objCurProj = GlobTemp.VBProject
Set FindListFormatModule = objCurProj.VBComponents(“frmAnalyzeListFormat”)
With FindListFormatModule.CodeModule
.AddFromString _
(“Public Sub MyCommandButton1_” & MyCommandButton1Number & “_Click” & vbCr _
& “P = ” & MyCommandButton1Number & vbCr _
& “FindListExample” & vbCr _
& “End Sub”)
End With
Your help in getting me to overcome this particular error would be greatly appreciated. Thanks again!!
Troy