views:

88

answers:

1

Hi, the title may not be appropriate because I don't really know how I should put this. I have a template where I have a "Module1" module and a "ThisDocument" code. Module1 contains the following code to pick up values from the registry. I pick up these values from ThisDocument code.

What I want is to move the code from ThisDocument over to it's own module. And being able to access the values from all other modules or subs.

Right now, ThisDocument has code that looks a little like this:

regPath = ReadIni(File, "Registry", "Path")
regString = ReadIni(File, "Registry", "String")
regStrFirstname = ReadIni(File, "Fields", "Firstname")
regStrLastname = ReadIni(File, "Fields", "Lastname")
regStrInitials = ReadIni(File, "Fields", "Initials")
regStrFullname = ReadIni(File, "Fields", "Fullname")

So, instead of having this code in ThisDocument, I would like to have this code in a separate Module and being able to access the values from ThisDocument directly. This way I can pick up the values from, say a form as well. Populate textboxes with the values etc.

How should I do this?

Module1 looks like this:

Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Long

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 Declare Function WritePrivateProfileSection Lib "kernel32" Alias _
"WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Long

Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
ByVal lpString As Any, ByVal lpFileName As String) As Long

'// INI CONTROLLING PROCEDURES
'reads an Ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v + 0)
End Function

'reads an Ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v + 0)
End Function
+3  A: 

Make the variables functions. You basically want global variables accessible everywhere. The way to do that is to capsulate the variables.

In Module1 (or a new module) add:

Function regPath()
        regPath = ReadIni(File, "Registry", "Path")
End Function

Hope it helps.

If you want to get even more sophisticated you can work with objects.

Jürgen Hollfelder
So does this mean that I need to declare a Function for each of the registry values? regPath, regFirstName, regLastName etc?
Kenny Bones
Yes, one function for each registry value.
Jürgen Hollfelder
Is it not possible to make a Function with a parameter? That way you could just pass the Registry and Path variables as parameters to that Funtion....
froeschli
Sure, you could do that.
Jürgen Hollfelder