在 VBA 中设置全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12241167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:56:01  来源:igfitidea点击:

Setting Global Variables in VBA

vbavariablespowerpointglobalpowerpoint-vba

提问by dennis96411

I'm currently making an "OS" in PowerPoint and I need to know how to set global variables for the settings.

我目前正在 PowerPoint 中制作一个“操作系统”,我需要知道如何为这些设置设置全局变量。

I made a module called "Settings" containing:

我制作了一个名为“设置”的模块,其中包含:

Public Sub Settings()

Option Explicit
Public UserName, UserIcon, Background, BrowserHomePage As String
Public SetupComplete As Boolean

SetupComplete = False
UserName = "Administrator"
UserIcon = Nothing
Background = Nothing
BrowserHomePage = Nothing

'Set the variables
UserName.Text = UserName

End Sub

Now on the "log in" screen, I have a text box named "UserName". I then made a button just to test out the variables. The button does this:

现在在“登录”屏幕上,我有一个名为“用户名”的文本框。然后我做了一个按钮来测试变量。该按钮执行以下操作:

Private Sub CommandButton1_Click()
UserName.Value = UserName
End Sub

The text box has no value when I click the button. I'm super new at VBA, and would like to know how to do this. Also, if anyone knows how to automatically execute codes when starting the PowerPoint, that would be fantastic.

单击按钮时,文本框没有任何值。我是 VBA 的新手,想知道如何做到这一点。此外,如果有人知道如何在启动 PowerPoint 时自动执行代码,那就太棒了。

EDIT: I'm trying to make a module containing only the settings. Can someone point out how to change the values from slides? Like if I click a button in slide 1, I want it to change the "UserName" value in module "Settings" to whatever I want.

编辑:我正在尝试制作一个仅包含设置的模块。有人可以指出如何更改幻灯片中的值吗?就像我单击幻灯片 1 中的按钮一样,我希望它将模块“设置”中的“用户名”值更改为我想要的任何值。

Solution: Okay, I found the one solution. I have to write the settings to a text file and retrieve it for reading.

解决方案:好的,我找到了一个解决方案。我必须将设置写入文本文件并检索它以供阅读。

My settings module:

我的设置模块:

Public UserName As String, Password As String, UserIcon As String, DesktopBackground As String, LogInBackground As String, BrowserHomePage As String
Public InitialSetupCompleted As Boolean

Public Sub ReadSettings()

'Delcaring variables
TempDir = Environ("Temp")
SettingsFileName = "\OpenOSSettings.txt"
SettingsFile = TempDir & SettingsFileName
ReadFile = FreeFile()

'Read all settings from file
Open SettingsFile For Input As #ReadFile
Do While Not EOF(ReadFile)
    Line Input #ReadFile, Read
        If Read Like "UserName = *" Then
        UserName = Replace(Read, "UserName = ", "")
        End If
        If Read Like "Password = *" Then
        Password = Replace(Read, "Password = ", "")
        End If
        If Read Like "UserIcon = *" Then
        UserIcon = Replace(Read, "UserIcon = ", "")
        End If
        If Read Like "DesktopBackground = *" Then
        DesktopBackground = Replace(Read, "DesktopBackground = ", "")
        End If
        If Read Like "LogInBackground = *" Then
        LogInBackground = Replace(Read, "LogInBackground = ", "")
        End If
        If Read Like "BrowserHomePage = *" Then
        BrowserHomePage = Replace(Read, "BrowserHomePage = ", "")
        End If
        If Read Like "InitialSetupCompleted = *" Then
        InitialSetupCompleted = Replace(Read, "InitialSetupCompleted = ", "")
        End If
Loop
Close #ReadFile

'Applying settings to all elements
Slide5.UserName.Caption = UserName

End Sub


Public Sub SaveSettings()

'Declaring variables
TempDir = Environ("Temp")
SettingsFileName = "\OpenOSSettings.txt"
SettingsFile = TempDir & SettingsFileName
WriteFile = FreeFile()

'Write all settings to file
Open SettingsFile For Output As #WriteFile
Print #WriteFile, "UserName = " & UserName
Print #WriteFile, "Password = " & Password
Print #WriteFile, "UserIcon = " & UserIcon
Print #WriteFile, "DesktopBackground = " & DesktopBackground
Print #WriteFile, "LogInBackground = " & LogInBackground
Print #WriteFile, "BrowserHomePage = " & BrowserHomePage
Print #WriteFile, "InitialSetupCompleted = " & InitialSetupCompleted
Close #WriteFile

End Sub

Now to save the settings, I just use a textbox and a button. Saving the value of TextBox1 to UserName in the file:

现在要保存设置,我只使用一个文本框和一个按钮。将 TextBox1 的值保存到文件中的 UserName 中:

Private Sub CommandButton1_Click()
UserName = TextBox1.Value
Settings.SaveSettings
End Sub

Reading the value of UserName and put it into TextBox1:

读取 UserName 的值并将其放入 TextBox1:

Private Sub CommandButton2_Click()
Settings.ReadSettings
TextBox2.Value = UserName
End Sub

Very long code, but it works well. Thanks everyone!

代码很长,但效果很好。谢谢大家!

采纳答案by Dick Kusleika

Don't put your setting values in a module. Modules are for code and you will do more work trying to store data in them than you want. Store settings in the registry or in a text file. If you want the settings to be inside the file, you may be able to use CustomDocumentProperties or a hidden slide - I don't know enough about PPT to give you a good suggestion.

不要将您的设置值放在模块中。模块用于代码,您将做更多的工作来尝试将数据存储在其中而不是您想要的。将设置存储在注册表或文本文件中。如果您希望设置在文件内,您可以使用 CustomDocumentProperties 或隐藏幻灯片 - 我对 PPT 了解不够,无法给您一个好的建议。

Make a procedure that reads the settings from wherever you store them. Then make a procedure to write them back out to storage. When the user clicks a certain button on the slide, you change the Username variable, then execute the procedure that writes it to storage.

制定一个程序,从您存储它们的任何位置读取设置。然后制定一个程序将它们写回存储。当用户单击幻灯片上的某个按钮时,您会更改 Username 变量,然后执行将其写入存储的过程。

When declaring variables on the same line, you have to include the type for each variable.

在同一行声明变量时,必须包含每个变量的类型。

Public UserName, UserIcon, Background, BrowserHomePage As String

dims BrowserHomePage as a String, but the other three variables as Variants. Use this instead

将 BrowserHomePage 变暗为字符串,但其他三个变量为变体。改用这个

Public UserName As String, UserIcon As String, Background As String, BrowserHomePage As String

Or better yet, but them all on their own line.

或者更好,但他们都在自己的线上。

回答by Parker

Your "username" variable is not actually global because it's within the scope of your Settings subroutine. Move your variable declarations outside of a function/subroutine to make them global.

您的“用户名”变量实际上并不是全局变量,因为它在您的 Settings 子例程的范围内。将变量声明移到函数/子例程之外以使其成为全局变量。

Try this:

尝试这个:

Option Explicit
Public UserName, UserIcon, Background, BrowserHomePage As String
Public SetupComplete As Boolean

Public Sub Settings()

SetupComplete = False
UserName = "Administrator"
UserIcon = Nothing
Background = Nothing
BrowserHomePage = Nothing

'Set the variables
UserName.Text = UserName

End Sub