Results 1 to 3 of 3

Thread: VB6 - Modify VBA Macro Code From VB

  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

  2. #2
    New Member
    Join Date
    Jan 2006
    Posts
    1

    Exclamation C#: List down Macros from a Word Document

    Hi there Rob,
    I need to to show all the macros that a word document contains, so that a user can run the selected macro through my application. Fortunately found your post near morning .

    2 questions please:

    1. I have got 2 macros as you can see here:
    Line 1: Sub TestMacro()
    Line 12: Sub Macro1()
    Now, should I extract the macro name with some custom logic or is there any standard way to iterate through the macro collection?

    2. Do you have any idea of the exception (between try-catch) ?

    VB Code:
    1. int nCount = m_appWord.VBE.ActiveVBProject.VBComponents.Count;
    2.             richTextBox1.AppendText( "Project Count: " + nCount.ToString() );
    3.            
    4.             for(int i = 1; i <= nCount; i++)
    5.             {
    6.                 m_appWord.VBE.ActiveVBProject.VBComponents.Item(i).Activate();
    7.                                
    8.                 richTextBox1.AppendText( "\n------------------------------------------------------------------\n" );
    9.                 richTextBox1.AppendText( m_appWord.VBE.ActiveVBProject.VBComponents.Item(i).Name +
    10.                     ": Type - " + m_appWord.VBE.ActiveVBProject.VBComponents.Item(i).Type );
    11.                
    12.                 try
    13.                 {
    14.                     richTextBox1.AppendText( " Line Count: " + m_appWord.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.CountOfLines);
    15.  
    16.                 }catch(Exception ex)
    17.                 {
    18.                     MessageBox.Show(this, ex.StackTrace, ex.Source + " " + ex.Message);
    19.  
    20. //System.Runtime.InteropServices.COMException (0x800A01C4): Exception from HRESULT: 0x800A01C4.
    21. //at VBIDE.CodePanesClass.Item(Object index)
    22. //at TestWord.Form1.btnListMacros_Click(Object sender, EventArgs e) in c:\documents and settings\administrator\desktop\mamjad dt\testword\form1.cs:line 663
    23. //at System.Windows.Forms.Control.OnClick(EventArgs e)
    24. //at System.Windows.Forms.Button.OnClick(EventArgs e)
    25. //at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    26. //at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    27. //at System.Windows.Forms.Control.WndProc(Message& m)
    28. //at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    29. //at System.Windows.Forms.Button.WndProc(Message& m)
    30. //at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
    31. //at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
    32. //at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    33.  
    34.                 }
    35.  
    36.                 int nLineCount = m_appWord.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.CountOfLines;
    37.                 for(int nLine = 1; nLine <= nLineCount; nLine++)
    38.                 {
    39.                     richTextBox1.AppendText( "\nLine " + nLine + ": " + m_appWord.VBE.ActiveVBProject.VBE.CodePanes.Item(i).CodeModule.get_Lines(nLine, 1) );
    40.                 }
    41.  
    42.                 richTextBox1.AppendText( "\n" );
    43.             }
    HTML Code:
    OUTPUT!!!
    Project Count: 2
    ------------------------------------------------------------------
    ThisDocument: Type - vbext_ct_Document Line Count: 23
    Line 1: Sub TestMacro()
    Line 2: '
    Line 3: ' TestMacro Macro
    Line 4: ' Macro recorded 1/20/2006 by Administrator
    Line 5: '
    Line 6:     Selection.TypeText Text:="Allah"
    Line 7:     Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    Line 8:     Selection.Font.Italic = wdToggle
    Line 9:     Selection.Font.Bold = wdToggle
    Line 10:     Selection.Font.Size = 36
    Line 11: End Sub
    Line 12: Sub Macro1()
    Line 13: '
    Line 14: ' Macro1 Macro
    Line 15: ' Macro recorded 1/20/2006 by Administrator
    Line 16: '
    Line 17:     Selection.MoveRight Unit:=wdCharacter, Count:=1
    Line 18:     Selection.TypeParagraph
    Line 19:     Selection.TypeText Text:="o akbar"
    Line 20:     Selection.MoveLeft Unit:=wdCharacter, Count:=5
    Line 21:     Selection.TypeParagraph
    Line 22:     Selection.MoveUp Unit:=wdLine, Count:=1
    Line 23: End Sub
    
    ------------------------------------------------------------------
    NewMacros: Type - vbext_ct_StdModule
    Thanks a lot,
    Muhammad Amjad

  3. #3

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

    Re: VB6 - Modify VBA Macro Code From VB

    Welcome to the Forums.

    I couldnt find anything on your C# Try Catch error but to list all procedures of a module I can do. Its the .ProcOfLine function that compares the lines and can return the type of procedure line it is (Property, Function, Sub, etc). No other way to count or retrieve them then to loop through all lines.
    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