2
votes

I would like to comment the lines who contains Msg Box from another code VBA. I'm trying this with the Library VBA EXTENSIBILITY but i doesn't found the solution.

Any help is welcome.

This is my code:

Sub CommentCode()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim LineNum As Long
        Const QUOTE = ' 

        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("ThisWorkbook")
        Set CodeMod = VBComp.CodeModule

        With CodeMod
            LineNum = .CreateEventProc("Open", "Workbook")
            LineNum = LineNum + 1
            .InsertLines LineNum,  QUOTE 
        End With
    End Sub
2
please, show the code snippet which you already have - Kazimierz Jawor

2 Answers

1
votes

first, please change

Const QUOTE = '

to this:

Const QUOTE = "'"

basically your quote (or Rem) would be a string and needs to be enclosed in quotes.

as for the VB Extensibilty, you may need to delete the line once found, and insert a new line with the comment at the beginning.

See Chip Pearson: Programming in the VBA Editor

1
votes

I have found the solution to my problem with this code :

Sub CommentCodeModule(wb As Workbook)
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim WordToFind As String
        Dim SL As Long ' start line
        Dim EL As Long ' end line
        Dim SC As Long ' start column
        Dim EC As Long ' end column
        Dim Found As Boolean
        Dim LineNum As Long
        Set VBProj = wb.VBProject
        Set VBComp = VBProj.VBComponents("Module3")
        Set CodeMod = VBComp.CodeModule

        WordToFind = "test"

        With CodeMod
            SL = 1
            EL = .CountOfLines
            SC = 1
            EC = 255
            Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                EndLine:=EL, EndColumn:=EC, _
                wholeword:=True, MatchCase:=False, patternsearch:=False)
            Do Until Found = False
                Debug.Print "Found at: Line: " & CStr(SL) & " Column: " & CStr(SC)
                EL = .CountOfLines
                SC = EC + 1
                EC = 255

                Found = .Find(target:=FindWhat, StartLine:=SL, StartColumn:=SC, _
                    EndLine:=EL, EndColumn:=EC, _
                    wholeword:=True, MatchCase:=False, patternsearch:=False)
            Loop
             LineNum = SL
             .DeleteLines LineNum

            .InsertLines LineNum, "  ' test"
        End With
    End Sub

Thanks to everyone that helped me.