gpt4 book ai didi

vba - 在 VBA 中设置全局变量

转载 作者:行者123 更新时间:2023-12-04 14:47:43 34 4
gpt4 key购买 nike

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

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

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

现在在“登录”屏幕上,我有一个名为“用户名”的文本框。然后我做了一个按钮来测试变量。该按钮执行此操作:
Private Sub CommandButton1_Click()
UserName.Value = UserName
End Sub

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

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

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

我的设置模块:
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

现在要保存设置,我只使用一个文本框和一个按钮。
将 TextBox1 的值保存到文件中的 UserName 中:
Private Sub CommandButton1_Click()
UserName = TextBox1.Value
Settings.SaveSettings
End Sub

读取 UserName 的值并放入 TextBox1:
Private Sub CommandButton2_Click()
Settings.ReadSettings
TextBox2.Value = UserName
End Sub

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

最佳答案

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

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

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

Public UserName, UserIcon, Background, BrowserHomePage As String

将 BrowserHomePage 调暗为字符串,但将其他三个变量调暗为 Variants。改用这个
Public UserName As String, UserIcon As String, Background As String, BrowserHomePage As String

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

关于vba - 在 VBA 中设置全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241167/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com