dan
dan

Reputation: 3519

Convert VBA to VBS

I have a little VBA script with some functions that I would like to convert to a single VBS file.

Here is an example of what I got:

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As String) As String
    Dim Worked As Long
    Dim RetStr As String * 128
    Dim StrSize As Long
    Dim iNoOfCharInIni As Integer
    Dim sIniString, sProfileString As String

    iNoOfCharInIni = 0
    sIniString = ""
    If Sect = "" Or Keyname = "" Then
        MsgBox "Erreur lors de la lecture des paramètres dans " & IniFileName, vbExclamation, "INI"
        Access.Application.Quit
    Else
        sProfileString = ""
        RetStr = Space(128)
        StrSize = Len(RetStr)
        Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, IniFileName)
        If Worked Then
            iNoOfCharInIni = Worked
            sIniString = Left$(RetStr, Worked)
        End If
    End If
    ReadIniFileString = sIniString
End Function

And then, I need to use this function to put some values in strings. VBS doesn't seem to like any of my var declaration ((Dim) MyVar As MyType).

If I'm able to adapt that code to VBS, I should be able to do the rest of my functions too. How can I adapt/convert this to VBS? Thank you.

Upvotes: 1

Views: 1860

Answers (2)

peter
peter

Reputation: 42207

It's a pitty i didn't see this earlier, anyway, for future reference here is a pure vbscript solution to read a value from an ini file. If anyone needs explanation on the used regular expression just leave a comment.

'this is the contents of test.ini'
' [Brussels]
' Address = "Postbox 3245_58348 Brussels"

' [Copenhagen]
' Address = "Postbox 2455_5478347 Copenhagen"

' [Paris]
' Address = "Postbox 8546_5412557 Paris"

section = "Brussels"
key = "Address"

const ForReading = 1
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.OpenTextFile("test.ini", ForReading)

'returns "Postbox 3245_58348 Brussels"'
wscript.echo get_key_of_ini(file.readall, section, key)

function get_key_of_ini(readFile, section, key)
  set regEx = New RegExp
  with regEx
    .Pattern = "(\[" & section & "\]\r\n)([^\[]+)"
    .IgnoreCase = True 
    .Global = True
  end With

  set matches  = regEx.execute(readFile)
  for x = 0 to matches.count-1
    set match = matches(x)
    For i = 1 To match.subMatches.count-1
      subMatches = match.SubMatches(i)
      if trim(split(match.SubMatches(i),"=")(0)) = key then
        get_key_of_ini = trim(split(match.SubMatches(i),"=")(1))
      end if
    Next
  next
end function

Upvotes: 2

HelloW
HelloW

Reputation: 1617

Since you have an MDB that does what you want, run the VBS script to open this mdb and set the AutoExec Macro to run the functions that compact these Databases and then self close the MDB. This is a bit hacky but may prove to be the least troublesome.

Upvotes: 1

Related Questions