Results 1 to 3 of 3

Thread: VB6 - Modify VBA Macro Code From VB

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    VB6 - Modify VBA Macro Code From VB

    I wrote this for a member that needed to modify macro code that
    referenced a server that was to be retired to another server's
    name.

    What it will do is basically open an Excel workbook and test for a
    password on the vba project. Then it will loop through the
    classes & modules looking through every line of code for the old
    server name. Then it will replace the old server name with the
    new server name and save the workbook and close it.

    Access to Visual Basic Projects needs to be manually enabled for
    this to work. So there is no threat of malicious action by this code.



    VB Code:
    1. Option Explicit
    2. 'Add reference to MS Excel xx.0 Object Library
    3. 'Trust Access to VB Projects needs to be enabled in Excel first.
    4. 'Check it, in Tools > Macros > Security > Trusted Publishers tab - "Trust access to Visual Basic projects"
    5. Private moApp As Excel.Application
    6.  
    7. Private Sub Command1_Click()
    8.    
    9.     On Error GoTo No_Bugs
    10.    
    11.     Dim oWB As Excel.Workbook
    12.     Dim i As Integer
    13.     Dim iReplace As Integer
    14.     Dim sReplace As String
    15.     Dim sOriginalLine As String
    16.     Dim iLine As Integer
    17.    
    18.     moApp.Visible = True
    19.     'Do a loop here and iterate through your folder containing the workbooks.
    20.     'ToDo: Start workbook loop of all folders and sub folders passing the workbook name
    21.         Set oWB = moApp.Workbooks.Open("C:\Documents and Settings\VB-Guru\My Documents\Book2.xls")
    22.         If moApp.VBE.ActiveVBProject.VBComponents.Item(1).Properties("HasPassword").Value = False Then
    23.             If moApp.VBE.ActiveVBProject.VBComponents.Count > 0 Then 'Components are like sheet1,thisworkbook,etc.
    24.                 For i = 1 To moApp.VBE.ActiveVBProject.VBComponents.Count
    25.                     moApp.VBE.ActiveVBProject.VBComponents.Item(i).Activate
    26.                     Debug.Print "------------------------------------------------------------------"
    27.                     Debug.Print moApp.VBE.ActiveVBProject.VBComponents.Item(i).Name & ": Type - " & moApp.VBE.ActiveVBProject.VBComponents.Item(i).Type
    28.                     Debug.Print "Lines: " & moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.CountOfLines
    29.                     If moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.CountOfLines > 0 Then
    30.                         'Loop through all lines searching for "\\servername and replace with new server name"
    31.                         For iLine = 1 To moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.CountOfLines
    32.                             Debug.Print "Line " & iLine & "  : " & moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.Lines(iLine, 1)
    33.                             iReplace = InStr(1, moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.Lines(iLine, 1), "\\Server Name", vbTextCompare)
    34.                             If iReplace > 0 Then
    35.                                 sOriginalLine = moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.Lines(iLine, 1)
    36.                                 sReplace = Replace(sOriginalLine, "\\Server Name", "\\New Server", 1, 1, vbTextCompare)
    37.                                 moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.ReplaceLine iLine, sReplace
    38.                                 Debug.Print "Repl " & iLine & "  : " & moApp.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.Lines(iLine, 1)
    39.                             End If
    40.                         Next
    41.                     Else
    42.                         Debug.Print moApp.VBE.ActiveVBProject.VBComponents.Item(i).Name & " contains No Macro code!"
    43.                     End If
    44.                 Next
    45.             Else
    46.                 Debug.Print oWB.FullName & " contains No VBComponents!!"
    47.             End If
    48.         End If
    49.         oWB.Close True 'Save modifications to workbook
    50.     'Loop
    51.     Exit Sub
    52.    
    53. No_Bugs:
    54.     If Err.Number = 50289 Then
    55.         MsgBox oWB.Name & " contains a password on the VBProject!", vbOKOnly + vbExclamation
    56.         Debug.Print oWB.FullName & " contains a password on the VBProject!"
    57.     Else
    58.         MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    59.         Debug.Print "Other error" & vbNewLine & Err.Number & " - " & Err.Description
    60.     End If
    61. End Sub
    62.  
    63. Private Sub Form_Load()
    64.     Set moApp = New Excel.Application
    65.     moApp.Visible = False
    66. End Sub
    67.  
    68. 'MY SAMPLE OUTPUT!!!!
    69. '------------------------------------------------------------------
    70. 'ThisWorkbook: Type - 100
    71. 'Lines: 3
    72. 'Line 1  : Public Sub Test()
    73. 'Line 2  :     MsgBox "Module Test"
    74. 'Line 3  : End Sub
    75. '------------------------------------------------------------------
    76. 'Sheet1: Type - 100
    77. 'Lines: 0
    78. 'Sheet1 contains No Macro code!
    79. '------------------------------------------------------------------
    80. 'Sheet2: Type - 100
    81. 'Lines: 0
    82. 'Sheet2 contains No Macro code!
    83. '------------------------------------------------------------------
    84. 'Sheet3: Type - 100
    85. 'Lines: 0
    86. 'Sheet3 contains No Macro code!
    87. '------------------------------------------------------------------
    88. 'Module1: Type - 1
    89. 'Lines: 5
    90. 'Line 1  : Private Sub Workbook_Open()
    91. 'Line 2  :     'MsgBox "\\Server Name\Test"
    92. 'Repl 2:       'MsgBox "\\New Server\Test"
    93. 'Line 3  : End Sub
    94. 'Line 4  :
    95. 'Line 5  :
    VB/Excel Guru™
    Attached Images Attached Images  
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width