Actualmente estoy haciendo un "sistema operativo" en PowerPoint y necesito saber cómo establecer variables globales para la configuración.Configuración de variables globales en VBA
hice un módulo llamado "Configuración" que contienen:
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
Ahora en la pantalla "Iniciar sesión", tengo un cuadro de texto denominado "nombre de usuario". Luego hice un botón solo para probar las variables. El botón hace esto:
Private Sub CommandButton1_Click()
UserName.Value = UserName
End Sub
El cuadro de texto no tiene ningún valor cuando hago clic en el botón. Soy súper nuevo en VBA y me gustaría saber cómo hacer esto. Además, si alguien sabe cómo ejecutar automáticamente códigos al iniciar el PowerPoint, sería fantástico.
EDITAR: Estoy intentando crear un módulo que solo contenga la configuración. ¿Puede alguien señalar cómo cambiar los valores de las diapositivas? Al igual que si hago clic en un botón en la diapositiva 1, quiero que cambie el valor de "Nombre de usuario" en el módulo "Configuración" a lo que yo quiera.
Solución: Bien, encontré la única solución. Tengo que escribir las configuraciones en un archivo de texto y recuperarlo para leerlo.
Mi configuración de módulo:
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
ahora para guardar la configuración, sólo tiene que utilizar un cuadro de texto y un botón. ahorro el valor de TextBox1 a nombre de usuario en el archivo:
Private Sub CommandButton1_Click()
UserName = TextBox1.Value
Settings.SaveSettings
End Sub
leer el valor de Nombre de usuario y ponerlo en TextBox1:
Private Sub CommandButton2_Click()
Settings.ReadSettings
TextBox2.Value = UserName
End Sub
código muy larga, pero funciona bien. ¡Gracias a todos!
{advertencia genérica sobre variables globales} –
'Nombre de usuario público, UserIcon, Background, BrowserHomePage As String' aquí solamente' BrowserHomePage' es una cadena, debe repetir 'como cadena' después de cada variable –
" Actualmente estoy haciendo una "OS" en PowerPoint ". ¿??? – cja