title: Setting Global Variables in VBA
body: <p>I'm currently making an "OS" in PowerPoint and I need to know how to set global variables for the settings.</p>
  
  <p>I made a module called "Settings" containing:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Private Sub CommandButton1_Click()
  UserName.Value = UserName
  End Sub
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>Solution</strong>: Okay, I found the one solution. I have to write the settings to a text file and retrieve it for reading.</p>
  
  <p>My settings module:</p>
  
  <pre><code>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 &amp; 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 &amp; SettingsFileName
  WriteFile = FreeFile()
  
  'Write all settings to file
  Open SettingsFile For Output As #WriteFile
  Print #WriteFile, "UserName = " &amp; UserName
  Print #WriteFile, "Password = " &amp; Password
  Print #WriteFile, "UserIcon = " &amp; UserIcon
  Print #WriteFile, "DesktopBackground = " &amp; DesktopBackground
  Print #WriteFile, "LogInBackground = " &amp; LogInBackground
  Print #WriteFile, "BrowserHomePage = " &amp; BrowserHomePage
  Print #WriteFile, "InitialSetupCompleted = " &amp; InitialSetupCompleted
  Close #WriteFile
  
  End Sub
  </code></pre>
  
  <p>Now to save the settings, I just use a textbox and a button.
  Saving the value of TextBox1 to UserName in the file:</p>
  
  <pre><code>Private Sub CommandButton1_Click()
  UserName = TextBox1.Value
  Settings.SaveSettings
  End Sub
  </code></pre>
  
  <p>Reading the value of UserName and put it into TextBox1:</p>
  
  <pre><code>Private Sub CommandButton2_Click()
  Settings.ReadSettings
  TextBox2.Value = UserName
  End Sub
  </code></pre>
  
  <p>Very long code, but it works well. Thanks everyone!</p>