Access Lines of a Macro

GrexP

Thanks, Grex
Local time
Today, 14:42
Joined
Aug 1, 2007
Messages
51
I'm trying to read a macro in VBA Code. With forms and reports I can iterate all of the controls and objects on the form or report. I can read and set properties of the controls. I use the AllForms() and AllReports() collections to do this. There is the AllMacros() collection but it only returns the name of the macros and the count. How can I access the individual lines of a macro in code?

Greg
 
I believe in Access2007 you can create/change/... macro's.
In Acc2003 you can't.
 
I would convert all your Macros into VB.

Simon
 
Got it!

It's a bit of a hack, but it works. I borrowed on some code I found in the code repository

http://www.access-programmers.co.uk/forums/showthread.php?t=99179


Code:
Private Sub GetMacrosText()
    'Put these two lines in the declarations section of the module
    'Public iMarcoCount As Integer
    'Public MacroText() As String

    On Error GoTo Err_ExportMacor2
    
    Dim db As Database
    Dim c As Container
    Dim d As Document
    Dim k As Integer
    Dim sTemp As String
    
    Set db = CurrentDb()
    
    iMarcoCount = CurrentProject.AllMacros.Count
    ReDim MacroText(iMarcoCount) 'public string array
    'loop through the marcos one at a time
    'the ExportMacro function returns the text of the macro commands
    'store it in a Public Array
    Set c = db.Containers("Scripts")
    For Each d In c.Documents
        sTemp = ExportMacro(d.Name)
        MacroText(k) = sTemp
        k = k + 1
    Next
    
    
    Set db = Nothing
    Set c = Nothing
    
    Exit Sub
Err_ExportMacor2:
    MsgBox Err.Number & " - " & Err.Description, vbInformation
    Exit Sub
End Sub

Private Function ExportMacro(sMacroName As String) As String
    On Error GoTo Err_ExportMacor
    Dim iFreeFile As Integer
    Dim sTemp As String
    Dim sExportLocation As String
    
    sExportLocation = "u:\"
    'save the macro as a text file
    Application.SaveAsText acMacro, sMacroName, sExportLocation & "Macro_" & sMacroName & ".txt"
    
    'open the file and read in the contents
    iFreeFile = FreeFile
    Open sExportLocation & "Macro_" & sMacroName & ".txt" For Input As iFreeFile
        sTemp = Input(LOF(iFreeFile), #iFreeFile)
    Close iFreeFile
    'delete the file
    Kill sExportLocation & "Macro_" & sMacroName & ".txt"
    'pass back the text
    ExportMacro = sTemp
    
    Exit Function
Err_ExportMacor:
    MsgBox Err.Number & " - " & Err.Description, vbInformation
    Exit Function

End Function
 

Users who are viewing this thread

Back
Top Bottom