Obecnie robię "OS" w programie PowerPoint i muszę wiedzieć, jak ustawić zmienne globalne dla ustawień.Ustawianie zmiennych globalnych w VBA
Zrobiłem moduł o nazwie „Ustawienia” zawierające:
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
Teraz na ekranie „Zaloguj”, mam pole tekstowe o nazwie „nazwa_użytkownika”. Następnie zrobiłem przycisk, żeby przetestować zmienne. Przycisk wykonuje następujące czynności:
Private Sub CommandButton1_Click()
UserName.Value = UserName
End Sub
Pole tekstowe nie ma wartości po kliknięciu przycisku. Jestem super nowy w VBA i chciałbym wiedzieć, jak to zrobić. Ponadto, jeśli ktoś wie, jak automatycznie wykonywać kody podczas uruchamiania programu PowerPoint, byłoby to fantastyczne.
EDYCJA: Próbuję utworzyć moduł zawierający tylko ustawienia. Czy ktoś może wskazać, jak zmienić wartości ze slajdów? Tak jak w przypadku kliknięcia przycisku na slajdzie 1, chcę zmienić wartość "UserName" w module "Ustawienia" na cokolwiek zechcę.
Rozwiązanie: OK, znalazłem jedno rozwiązanie. Muszę zapisać ustawienia do pliku tekstowego i pobrać go do odczytu.
Mój moduł ustawienia:
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
teraz, aby zapisać ustawienia, po prostu użyć tekstowe i przycisk. Saving wartość TextBox1 do nazwa_użytkownika w pliku:
Private Sub CommandButton1_Click()
UserName = TextBox1.Value
Settings.SaveSettings
End Sub
Czytając wartość nazwa_użytkownika i umieścić go w TextBox1:
Private Sub CommandButton2_Click()
Settings.ReadSettings
TextBox2.Value = UserName
End Sub
Bardzo długi kod, ale działa dobrze. Dziękuję wszystkim!
{Ogólne ostrzeżenie o zmiennych globalnych} –
'Publiczna nazwa użytkownika, UserIcon, Tło, BrowserHomePage jako ciąg' tutaj tylko' BrowserHomePage' jest łańcuchem, musisz powtórzyć 'jak ciąg' po każdej zmiennej –
" Obecnie robię "OS" w PowerPoint ". !! ??? – cja