替换 vba powerpoint 中的变量
Replacing a variable in vba powerpoint
我正在尝试替换并保存 VBA Powerpoint 中的一个整数。
我在 1 个名为 projectId 的子程序中有一个变量。目前,变量设置为 617。在另一个子例程中,我是 运行 提示用户输入新 projectId 并存储新 projectId 以替换“617”的用户输入法。唯一的问题是当我关闭 PowerPoint 并重新打开它时,projectId 变为 0。
对于此示例,是否有任何方法可以将代码 projectId = 617
物理替换为 projectId = 699
。
下面是我正在使用的代码,请注意 projId
是一个全局变量:
Dim projId As Integer
Sub ChangeProjID()
Dim strProjId As String
Dim intProjId As Integer
strProjId = InputBox("Enter the Project ID given by example:", "Project ID Input")
On Error GoTo NotValidID
intProjId = CInt(strProjId)
projId = intProjId
MsgBox " Your new project ID will be set to " & intProjId & " until changed again"
Exit Sub
NotValidID:
MsgBox " You must enter a valid integer"
End Sub
Public Sub CommentConnect(control As IRibbonControl)
Dim URL As String
projId = 617
URL = "example.com/prID="
ActivePresentation.FollowHyperlink (URL & projId)
End Sub
如果 CommentConnect()
在 ChangeProjID()
之后调用,行 projId = 617
将覆盖用户在 ChangeProjID()
中输入的任何值.
我看到了解决此问题的几个选项。
- 将
ChangeProjID()
的签名更改为:
Function ChangeProjID() as Integer
然后更改 CommentConnect to:
[=19= 中的行]
- 将
CommentConnect()
中的行更改为:
If projId = 0 then
projId = 617 'use this value as a default
End If
对于选项 1,完整的更改代码如下所示:
Function ChangeProjID() As Integer
Dim strProjId As String
Dim intProjId As Integer
strProjId = InputBox("Enter the Project ID given by example:", "Project ID Input")
On Error GoTo NotValidID
intProjId = CInt(strProjId)
MsgBox " Your new project ID will be set to " & intProjId & " until changed again"
ChangeProjID = intProjId
Exit Sub
NotValidID:
MsgBox " You must enter a valid integer"
End Sub
Public Sub CommentConnect(control As IRibbonControl)
Dim URL As String
projId = ChangeProjID
URL = "example.com/prID="
ActivePresentation.FollowHyperlink (URL & projId)
End Sub
选项 2 的完整更改代码为:
Public Sub CommentConnect(control As IRibbonControl)
Dim URL As String
if projId = 0 then
projId = 617 'use project 617 as the default
End If
URL = "example.com/prID="
ActivePresentation.FollowHyperlink (URL & projId)
End Sub
请注意,无论您做什么,当您首次打开 PPTX 时,projId
将默认为 0
- 这是变量的本质,它们在应用程序启动时具有默认值,除非另有设置。
如果需要,您可以创建某种机制来将 projId
存储在非易失性内存中(文本文件、注册表、隐藏(背景色 = 前景色)字段PPT幻灯片等)。或者您可以对 projId
进行某种初始化,以便在您的代码启动时它总是被设置为某个已知的非零值,但它将 而不是 保留其最后一个设定值。
FreeMan 的想法是正确的...将值存储在非易失性存储器中。
幸运的是,PowerPoint 有一个完美的功能:标签。
With ActivePresentation
.Tags.Add "ProjectID", Cstr(intProjID)
End With
intProjID 的字符串版本现在是永久性的 "tag" 附加到演示文稿对象。要检索它:
MsgBox ActivePresentation.Tags("ProjectID")
每个演示文稿、幻灯片和形状都可以包含任意数量的标签。
我正在尝试替换并保存 VBA Powerpoint 中的一个整数。
我在 1 个名为 projectId 的子程序中有一个变量。目前,变量设置为 617。在另一个子例程中,我是 运行 提示用户输入新 projectId 并存储新 projectId 以替换“617”的用户输入法。唯一的问题是当我关闭 PowerPoint 并重新打开它时,projectId 变为 0。
对于此示例,是否有任何方法可以将代码 projectId = 617
物理替换为 projectId = 699
。
下面是我正在使用的代码,请注意 projId
是一个全局变量:
Dim projId As Integer
Sub ChangeProjID()
Dim strProjId As String
Dim intProjId As Integer
strProjId = InputBox("Enter the Project ID given by example:", "Project ID Input")
On Error GoTo NotValidID
intProjId = CInt(strProjId)
projId = intProjId
MsgBox " Your new project ID will be set to " & intProjId & " until changed again"
Exit Sub
NotValidID:
MsgBox " You must enter a valid integer"
End Sub
Public Sub CommentConnect(control As IRibbonControl)
Dim URL As String
projId = 617
URL = "example.com/prID="
ActivePresentation.FollowHyperlink (URL & projId)
End Sub
如果 CommentConnect()
在 ChangeProjID()
之后调用,行 projId = 617
将覆盖用户在 ChangeProjID()
中输入的任何值.
我看到了解决此问题的几个选项。
- 将
ChangeProjID()
的签名更改为:Function ChangeProjID() as Integer
然后更改CommentConnect to:
[=19= 中的行] - 将
CommentConnect()
中的行更改为:If projId = 0 then
projId = 617 'use this value as a default
End If
对于选项 1,完整的更改代码如下所示:
Function ChangeProjID() As Integer
Dim strProjId As String
Dim intProjId As Integer
strProjId = InputBox("Enter the Project ID given by example:", "Project ID Input")
On Error GoTo NotValidID
intProjId = CInt(strProjId)
MsgBox " Your new project ID will be set to " & intProjId & " until changed again"
ChangeProjID = intProjId
Exit Sub
NotValidID:
MsgBox " You must enter a valid integer"
End Sub
Public Sub CommentConnect(control As IRibbonControl)
Dim URL As String
projId = ChangeProjID
URL = "example.com/prID="
ActivePresentation.FollowHyperlink (URL & projId)
End Sub
选项 2 的完整更改代码为:
Public Sub CommentConnect(control As IRibbonControl)
Dim URL As String
if projId = 0 then
projId = 617 'use project 617 as the default
End If
URL = "example.com/prID="
ActivePresentation.FollowHyperlink (URL & projId)
End Sub
请注意,无论您做什么,当您首次打开 PPTX 时,projId
将默认为 0
- 这是变量的本质,它们在应用程序启动时具有默认值,除非另有设置。
如果需要,您可以创建某种机制来将 projId
存储在非易失性内存中(文本文件、注册表、隐藏(背景色 = 前景色)字段PPT幻灯片等)。或者您可以对 projId
进行某种初始化,以便在您的代码启动时它总是被设置为某个已知的非零值,但它将 而不是 保留其最后一个设定值。
FreeMan 的想法是正确的...将值存储在非易失性存储器中。
幸运的是,PowerPoint 有一个完美的功能:标签。
With ActivePresentation
.Tags.Add "ProjectID", Cstr(intProjID)
End With
intProjID 的字符串版本现在是永久性的 "tag" 附加到演示文稿对象。要检索它:
MsgBox ActivePresentation.Tags("ProjectID")
每个演示文稿、幻灯片和形状都可以包含任意数量的标签。