Results 1 to 10 of 10

Thread: Modifying code via code

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Modifying code via code

    Is it possible to modify code (obviously code that's not being run at the time) in Excel? There's a small bug in an application we recently sent out, and we want to release a workbook which, when run, will go in and modify the code.

    The reasons we can't just re-release the actual workbook are:
    -You can't email a 9 MB file to very many places
    -Settings are stored in the workbook that shouldn't be lost
    -There are technicly more than one workbooks which will be modified


    To be specific, what I want is:
    How do I input current code? (Locating the subroutine)
    How do I modify code? (Replacing the subroutine)
    Is it a different method whether I remove or add code?
    Don't pay attention to this signature, it's contradictory.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Search in this vba forum for one of my posts that showed how to
    programmatically gain access to the VBA Editor for such purposes.
    There are some settings that need to be made manually, like
    under Tools > Macros > Security > Trusted Sources tab...

    I know it had to be with-in a year or so.

    Although I never got it to work, not enough free time.

    HTH
    Attached Images Attached Images  
    Last edited by RobDog888; Dec 27th, 2004 at 07:57 PM.
    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

  3. #3

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    I searched "programmatically gain access to the VBA Editor" but only found one post... this one.
    Don't pay attention to this signature, it's contradictory.

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

    Here
    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

  5. #5

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    There is some subtle irony here...
    Considering that post was made by the guy I work for.
    Don't pay attention to this signature, it's contradictory.

  6. #6

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Seeing as how I can't modify protected modules, I can up with another solution. Instead of modifying the modules, I just included the fixed modules with the "patch" workbook, had it copy-paste all the sheets from the target workbook into itself, and then save itself over the old one. It takes a minute to run, but I don't need to send out a 9 MB file
    Don't pay attention to this signature, it's contradictory.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Cool, good idea.
    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

  8. #8

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    It works great. Final patch sizes (one for each sheet in need): 381 kb and 467 kb (there's a picture in both of them)

    There's just one bug that I can't explain.

    VB Code:
    1. Workbooks("tbm_sheds.xls").Close False

    will crash excel (oh I do love when excel dies instantly), but

    VB Code:
    1. Workbooks("tbm_sheds.xls").Close True

    doesn't.

    Now I don't need to save the workbook because I'm about to overwrite it. Any ideas?

    There's nothing quite like debugging excel VBA... I've had lines that worked after they were commented.. and then uncommented.
    Don't pay attention to this signature, it's contradictory.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    What about turning off the prompt before the close?
    VB Code:
    1. Application.DisplayAlerts = False
    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

  10. #10

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Alerts, events, calculation, and asking for links are all off. However, I did try it with alerts on, and with a calculate statement just before the save. Here, I'll post the code:

    VB Code:
    1. Private Sub Apply()
    2. Dim Shp As Shape
    3. Dim A As Long
    4. Dim B As Long
    5. On Error GoTo ErrorHandle:
    6.  
    7.     Application.AskToUpdateLinks = False
    8.     Application.DisplayAlerts = False
    9.     ShtPatch1.Unprotect "******"
    10.     Application.Calculation = xlCalculationManual
    11.     Application.EnableEvents = False
    12. 'Code to make the interface look good...
    13. '(changing the color of three-four cells)
    14. 'Code to stop the patch from being applied twice...
    15. '(an if statement that won't apply)
    16. 'Move the workbook to the target location before opening
    17. 'other workbooks to avoid link errors in 2003
    18.     ThisWorkbook.SaveAs "C:/timbrplans/modules/" & ThisWorkbook.Name
    19. 'Open target workbook and its dependency (links, again)
    20.     GotoWorkbook "tbm_intro.xls"
    21.     GotoWorkbook "tbm_homes.xls" 'homes needs intro
    22.    
    23. 'Move some buttons...
    24.     With Workbooks("tbm_homes.xls").Sheets("Presentation")
    25.         .Select
    26.         .Unprotect Password:="open85e3"
    27.         For Each Shp In .Shapes
    28.             If Shp.Type = 8 Then
    29.                 Shp.Select
    30.                 Selection.ShapeRange.IncrementLeft -65
    31.             End If
    32.         Next Shp
    33.         .Protect Password:="open85e3"
    34.     End With
    35. 'Copy all sheets into the patch, which already contains modules and forms
    36.     For A = 1 To Workbooks("tbm_homes.xls").Sheets.Count
    37.         B = Workbooks("tbm_homes.xls").Sheets(A).Visible
    38.         Application.StatusBar = "Please wait, replacing worksheet #" & ThisWorkbook.Worksheets.Count & " of " & Workbooks("tbm_homes.xls").Worksheets.Count
    39.         Workbooks("tbm_homes.xls").Sheets(A).Visible = xlSheetVisible
    40.         Workbooks("tbm_homes.xls").Sheets(A).Copy ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    41.         Workbooks("tbm_homes.xls").Sheets(A).Visible = B
    42.         ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count - 1).Visible = B
    43.     Next A
    44.     ThisWorkbook.Activate
    45.    
    46.     Application.StatusBar = "Saving the homes module"
    47.     Workbooks("tbm_homes.xls").Close True  'ERROR HERE IF FALSE
    48.    
    49. 'More interface code...
    50.    
    51. 'Save patch over workbook
    52.     ThisWorkbook.SaveAs "C:/timbrplans/modules/tbm_homes.xls"
    53.  
    54. 'And finish up
    55.     Workbooks("tbm_intro.xls").Close False
    56.     Application.StatusBar = False
    57.     Application.EnableEvents = True
    58.     Application.DisplayAlerts = True
    59.     Application.Calculation = xlCalculationAutomatic
    60.     ShtPatch1.Protect "******"
    61.     MsgBox "The patch was applied succesfully.", vbInformation, "timbrplans"
    62. Exit Sub
    63.  
    64. ErrorHandle:
    65.     MsgBox "There was an error applying the patch.", vbExclamation, "timbrplans"
    66.     Application.StatusBar = False
    67.     Application.EnableEvents = True
    68.     Application.DisplayAlerts = True
    69.     Application.Calculation = xlCalculationAutomatic
    70.     ShtPatch1.Protect "******"
    71. End Sub
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78. Private Sub GotoWorkbook(ByVal Filename As String)
    79. Dim Filepath As String
    80. On Error GoTo Errhandler:
    81.  
    82.     Workbooks(Filename).Activate
    83. Exit Sub
    84.  
    85. Errhandler:
    86.     'If the file wasn't open yet
    87.     Application.ScreenUpdating = False
    88.     Application.StatusBar = "Please wait, loading " & Filename
    89.     Filepath = "C:\timbrplans\modules\" & Filename
    90.     If (Dir(Filepath)) = Filename Then
    91.         Workbooks.Open Filename:=Filepath, Password:="******"
    92.     Else
    93.         Filepath = "C:\timbrplans\" & Filename
    94.         If (Dir(Filepath)) = Filename Then
    95.             Workbooks.Open Filename:=Filepath, Password:="******"
    96.         End If
    97.     End If
    98. End Sub
    Last edited by alkatran; Aug 23rd, 2004 at 01:31 PM.
    Don't pay attention to this signature, it's contradictory.

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