Edit Macro from VBA

speakers_86

Registered User.
Local time
Today, 18:02
Joined
May 17, 2007
Messages
1,919
Is this possible? I looked it up, but I have never seen it. In a combo box after update, I want to edit AutoExec from Call This() to Call That().
 
Macros do not as you have discovered a VBA solution only hands on editing via the interface, the reason is problably that user should not mess around
and the fact that macros cannot be created in an MDE/ACCDE.

But one way can be to export the macro as text using

Code:
Application.SaveAsText acMacro, Macroname, Stingpath

Modify the textfile and import it back into your db using

Code:
Application.LoadFromText acMacro, Macroname, StringPath

It is and looks like convoluted way to get what you want and perhaps it should, anyway attached is a samplebase you can look at.
It is tested on a macro with ONLY 1 action call

Here is the code for Export/Import/Modify the macro:
Code:
Option Compare Database
Option Explicit

Function ExportMacro(strMacro As String, Optional Destination As Variant) As String
Dim Target As String

If IsMissing(Destination) Then
    Target = CurrentProject.Path & "\"
    ElseIf Right(Destination, 1) = "\" Then
        Target = Destination
        Else
        Target = Destination & "\"
End If
Application.SaveAsText acMacro, strMacro, Target & strMacro & "tmp.txt"
ExportMacro = Target & strMacro & "tmp.txt"
End Function

Function ImportMacro(strMacro As String, Source As String)
    Application.LoadFromText acMacro, strMacro, Source
End Function

Function ModifyMacro(ByVal strSource As String, NewMacro As String) As String
Dim sText As String
Dim TmpFile As String
Dim ff As Long
Dim rIndex As Long

ff = FreeFile
TmpFile = Replace(strSource, "tmp", "") ' remove "tmp" from filename

Open strSource For Input As #1
Open TmpFile For Output As #2

    Do Until EOF(1)
    rIndex = rIndex + 1
    Line Input #1, sText
        If rIndex = 5 Then    'Modify the 5. line in textfile
            Print #2, "    Argument =""" & NewMacro & """" ' note the 4 leading spaces and the doubling up of quotes
        Else
            Print #2, sText
        End If
    Loop
Close #1
Close #2

End Function

Run it at your own risk :rolleyes:

JR
 

Attachments

Thank you, kind sir.
 

Users who are viewing this thread

Back
Top Bottom