2012-09-03 258 views
6

我目前在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 

很長的代碼,但它工作得很好。感謝大家!

+0

{關於全局變量的通用警告} –

+0

'公共用戶名,UserIcon,背景,BrowserHomePage作爲字符串'這裏只有'BrowserHomePage'是一個字符串,你必須在每個變量之後重複'as string' –

+13

「我目前正在製作一個「PowerPoint中的操作系統」。 !! ??? – cja

回答

3

不要把你的設定值的模塊中。模塊是用於代碼的,你會做更多的工作來嘗試存儲數據,而不是你想要的。將設置存儲在註冊表中或文本文件中。如果您希望設置位於文件內部,您可以使用CustomDocumentProperties或隱藏幻燈片 - 我對PPT不夠了解,因此無法給您一個好的建議。

製作一個從您存儲它們的位置讀取設置的過程。然後制定程序將它們寫回存儲。當用戶單擊幻燈片上的某個按鈕時,您將更改用戶名變量,然後執行將其寫入存儲的過程。

當聲明在同一行的變量,你必須包括每個變量的類型。

Public UserName, UserIcon, Background, BrowserHomePage As String 

dims BrowserHomePage作爲一個字符串,但其他三個變量作爲Variant。使用這個代替

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

或者更好的,但他們都對自己行。

3

您的「用戶名」變量實際上並不是全局的,因爲它在您的設置子例程的範圍內。將你的變量聲明移到一個函數/子程序之外,以使它們成爲全局變量。

試試這個:

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