6

現在、PowerPointで「OS」を作成しています。設定にグローバル変数を設定する方法を知る必要があります。

以下を含む「設定」というモジュールを作成しました。

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

「ログイン」画面に、「UserName」という名前のテキストボックスが表示されます。次に、変数をテストするためだけにボタンを作成しました。ボタンはこれを行います:

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

ボタンをクリックしても、テキストボックスに値がありません。私はVBAを初めて使用するので、これを行う方法を知りたいです。また、PowerPointの起動時にコードを自動的に実行する方法を誰かが知っているなら、それは素晴らしいことです。

編集:設定のみを含むモジュールを作成しようとしています。スライドから値を変更する方法を誰かが指摘できますか?スライド1のボタンをクリックした場合と同様に、モジュール「設定」の「ユーザー名」の値を任意の値に変更します。

解決策:わかりました。1つの解決策を見つけました。設定をテキストファイルに書き込んで、読み取るために取得する必要があります。

私の設定モジュール:

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の値をファイルのUserNameに保存します。

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

UserNameの値を読み取り、TextBox1に配置します。

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

非常に長いコードですが、うまく機能します。みんな、ありがとう!

4

2 に答える 2

4

設定値をモジュールに入れないでください。モジュールはコード用であり、データをモジュールに保存しようとすると、必要以上の作業が必要になります。レジストリまたはテキストファイルに設定を保存します。設定をファイル内に配置したい場合は、CustomDocumentPropertiesまたは非表示のスライドを使用できる場合があります。PPTについて十分な知識がないため、適切な提案を行うことができません。

保存した場所から設定を読み取る手順を作成します。次に、それらをストレージに書き戻す手順を作成します。ユーザーがスライドの特定のボタンをクリックしたら、Username変数を変更してから、それをストレージに書き込むプロシージャを実行します。

同じ行で変数を宣言するときは、各変数の型を含める必要があります。

Public UserName, UserIcon, Background, BrowserHomePage As String

BrowserHomePageを文字列として薄暗くしますが、他の3つの変数はバリアントとして薄暗くします。代わりにこれを使用してください

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

またはさらに良いですが、それらはすべて独自のライン上にあります。

于 2012-09-03T14:24:19.643 に答える
3

「username」変数は、Settingsサブルーチンのスコープ内にあるため、実際にはグローバルではありません。変数宣言を関数/サブルーチンの外に移動して、グローバルにします。

これを試して:

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
于 2012-09-03T03:33:51.810 に答える