Setting global variables in VBA

I am currently creating an "OS" in PowerPoint, and I need to know how to set global variables for the settings.

I created 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 login screen I have a text box called "UserName". Then I made a button to check the variables. The button does this:

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

The text box doesn't matter when I click the button. I am super new in VBA and would like to know how to do this. Also, if someone knows how to automatically execute codes when starting PowerPoint, that would be fantastic.

EDIT: I am trying to create a module containing only settings. Can anyone point out how to change the values โ€‹โ€‹from the slides? For example, if I click on the button in slide 1, I want it to change the โ€œUserNameโ€ value in the โ€œSettingsโ€ module to whatever I want.

Solution : Ok, I found one solution. I need to write the settings to a text file and get 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 text box and a button. Saving the value of TextBox1 to UserName in a file:

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

Reading the value of UserName and putting it in TextBox1:

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

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

+6
source share
2 answers

Do not put your settings in the module. The modules are designed for code, and you will do more work, trying to store data in them than you want. Save the settings in the registry or in a text file. If you want the settings to be inside the file, you can use CustomDocumentProperties or a hidden slide - I don't know enough about PPT to give you a good suggestion.

Make a procedure that reads settings from anywhere in the memory. Then follow the procedure to write them back to the repository. When the user clicks a specific button on the slide, you change the Username variable, and then execute the procedure that writes it to the repository.

When declaring variables on a single line, you must specify a type for each variable.

 Public UserName, UserIcon, Background, BrowserHomePage As String 

dims BrowserHomePage as a string, but the other three variables as options. Use this instead

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

Or even better, but they are all on their own.

+3
source

Your username variable is not really global, as it falls within the scope of the Settings routine. Move variable declarations outside the function / subroutine to make them global.

Try the following:

 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 
+3
source

Source: https://habr.com/ru/post/924416/


All Articles