Results 1 to 1 of 1
Thread: INI file to macro (Word 2000+)
2008-05-26, 18:47 #1
- Join Date
- Feb 2001
- Yilgarn region of Toronto, Ontario
- Thanked 0 Times in 0 Posts
INI file to macro (Word 2000+)
This is one of two procedures I'm adopting to ease the burden of migrating parameters from a GUI user interface to batch processing.
The function "Sequence Of Operations" is ungainly, with an optional parameter for each of about three dozen controls on a mult1-page form.
As each control (on the GUI) is updated - clicked or exited - I write the value to the section "[MacroSection"]" in the INI file, so that the next session can pick up where we left off last time.
Thus this INI file section has as many key/value lines as the function "Sequence Of Operations" requires.
It struck me that processing the INI file line-by-line, replacing the "=" with a ":=" was the easiest way to generate VBA code to call the function in batch mode.
Once the user has a working set of parameters, a click on the command button "macromaker" spits out a three-line SUB that can be pasted into a template.
<pre>Public Const strcMacroSection As String = "MacroSection"
Function strProcessLines(ByVal strText As String) As String
Dim strResult As String
Do While Len(strText) > 0 ' process the file of data a line at a time
Dim strLine As String
strLine = strsplitat(strText, vbCrLf) ' get next line
If Len(strLine) > 0 Then ' line is not empty
If Left$(strLine, 1) = "[" Then ' we have reached the start of a new section
Dim lngEquals As Long
lngEquals = InStr(1, strLine, "=") ' locate the leftmost equals sign
If lngEquals > 0 Then ' if found
''' refabricate the line suitable for named parameters
strLine = Left$(strLine, lngEquals - 1) & ":=" & """" & Right$(strLine, Len(strLine) - lngEquals) & """"
''' return the line as is; Best Of Luck!
strResult = strResult & strLine & "," ' append the parameter key and value to the previous result
strProcessLines = Left$(strResult, Len(strResult) - 1) ' drop the trailing comma
' ' Locate the INI file
' Dim strFile As String
' strFile = "Cocuments and SettingsChrisLApplication DataIdentIdent.ini"
' ' Obtain the data from the INI file
' Dim strData As String
' strData = UW.strGetFileData(strFile)
' ' Drop all text up to and including [MacroSection]
' Dim lngLeft As Long
' lngLeft = InStr(1, strData, strcMacroSection)
' If lngLeft > 0 Then
' strData = Right$(strData, Len(strData) - lngLeft - Len(strcMacroSection) - 2)
' Dim strResult As String
' strResult = strProcessLines(strData) ' modify each line of data
' strResult = vbTab & "msgbox Sequence Of Operations(" & strResult & ")" ' embraces with the call to the macro
' strResult = "Sub Test" & vbCrLf & strResult
' strResult = strResult & vbCrLf & "end sub"
' Call UW.PutFileData(strfixpath(ThisDocument.Path) & "Macro.bas", strResult) ' write to a fixed file.
' MsgBox "No macro section was found in " & strFile
' End If