开发者

Word vba - How can I store string values in a module?

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 val开发者_开发技巧ues 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


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.


Firstly, I'd recommend you get rid of the API calls and just use the built-in registry functions in VBA of GetSetting, SaveSetting, GetAllSettings and DeleteSetting. You can learn more about them here: Maintain Custom Settings Using the Windows Registry. One of the immediate benefits of doing this, besides the fact that it's easy, is that if you end up working with Word 2010 x64, you don't have to maintain two sets of API calls, one for x64 and one for x86.

Now on to your question. It depends if your code is going to run in a global template or by itself. If it's in a global template, you'd use a sub routine in any module (like Module1) called AutoExec - you can put anything you want in that subroutine, like initilization of global variables. If it's not a global template (i.e. you're just going to open it sometimes and don't want it to always open when you start Word), then you would use AutoOpen instead. Any code in AutoOpen will run automatically (assuming you've handled security settings) when your macro-enabled document is opened.

So you could have something like this in Module1:

''# Put this at the top of the module
Public regPath As String
Public regString As String
Public regStrFirstname  As String
Public regStrLastname As String 
Public regStrInitials As String
Public regStrFullname As String

Sub AutoOpen()
    ''# Use your current API calls to the registry or the VBA calls to the registry (above)
    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")
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜