我目前在PowerPoint中製作一個「OS」,我需要知道如何設置全局變量。在VBA中設置全局變量
我做了一個叫「設置」模塊包含:
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
當我點擊按鈕時,文本框沒有任何值。我是VBA的超級新手,想知道如何做到這一點。此外,如果有人知道如何在啓動PowerPoint時自動執行代碼,那就太棒了。
編輯:我想做一個模塊只包含設置。有人可以指出如何更改幻燈片的值嗎?就像我單擊幻燈片1中的一個按鈕一樣,我希望它將模塊「設置」中的「UserName」值更改爲我想要的任何內容。
解決方案:好吧,我找到了一個解決方案。我必須將設置寫入文本文件並將其檢索以供閱讀。
我的設置模塊:
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中的文件中的值,以用戶名:
Private Sub CommandButton1_Click()
UserName = TextBox1.Value
Settings.SaveSettings
End Sub
讀取用戶名的值,並把它放到TextBox1的:
Private Sub CommandButton2_Click()
Settings.ReadSettings
TextBox2.Value = UserName
End Sub
很長的代碼,但它工作得很好。感謝大家!
{關於全局變量的通用警告} –
'公共用戶名,UserIcon,背景,BrowserHomePage作爲字符串'這裏只有'BrowserHomePage'是一個字符串,你必須在每個變量之後重複'as string' –
「我目前正在製作一個「PowerPoint中的操作系統」。 !! ??? – cja