简体   繁体   English

在VBA Powerpoint中替换变量

[英]Replacing a variable in vba powerpoint

I am trying to replace and save an integer in VBA Powerpoint. 我正在尝试替换并在VBA Powerpoint中保存一个整数。

I have a variable in 1 Subroutine named projectId. 我在1个名为projectId的子例程中有一个变量。 At the moment, the variable is set to 617. In another subroutine I am running a user input method that prompts the user for a new projectId and stores the new projectId to replace the "617". 目前,该变量设置为617。在另一个子例程中,我正在运行一个用户输入方法,该方法提示用户输入新的projectId并存储新的projectId以替换“ 617”。 The only issue is the projectId go to 0 when I close the PowerPoint and reopen it. 唯一的问题是当我关闭PowerPoint并重新打开它时projectId变为0。

Is there any way to physically replace the code projectId = 617 with projectId = 699 for this example. 对于此示例,是否有任何方法可以将项目projectId = 617实际替换为projectId = 699

Below is the code Im working with, notice that projId is a global veriable: 以下是我正在使用的代码,请注意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

If CommentConnect() is called after ChangeProjID() , the line projId = 617 will override any value the user entered in ChangeProjID() . 如果CommentConnect()被称为 ChangeProjID()projId = 617将覆盖任何值在输入的用户ChangeProjID()

I see a couple of options for fixing this. 我看到几个解决此问题的选项。

  1. Change the signature of ChangeProjID() to: ChangeProjID()的签名更改为:
    Function ChangeProjID() as Integer
    then change the line in CommentConnect to: 然后将CommentConnect to:的行更改CommentConnect to:
    projId = ChangeProjID()
  2. Change the line in CommentConnect() to: CommentConnect()的行更改为:
    If projId = 0 then
    projId = 617 'use this value as a default
    End If

For option 1, the complete, changed code would look like: 对于选项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

The complete, changed code for option 2 would be: 选项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

Note that whatever you do, projId will default to 0 when you first open the PPTX - that's the nature of variables, they have a default value when the application starts until set otherwise. 请注意,无论您做什么,首次打开PPTX时projId都将默认设置为0这就是变量的性质,在应用程序启动之前,它们具有默认值,除非另行设置。

If you want, you can create some sort of mechanism for storing the projId in non-volatile memory (a text file, the registry, a hidden (background color = foreground color) field on the first slide of the PPT, etc). 如果需要,可以创建某种机制将projId存储在非易失性存储器中(文本文件,注册表,PPT第一张幻灯片上的隐藏(背景色=前景色)字段等)。 Or you can have some sort of initialization for projId so that it always gets set to some known, non-zero value when your code is launched, but it will not retain its last set value all by itself. 或者,您可以对projId某种初始化,以便在启动代码时始终将其设置为某个已知的非零值,但它本身不会保留其最后一个设置值。

FreeMan's got the right idea ... store the value in non-volatile memory. FreeMan的想法正确……将值存储在非易失性存储器中。

Luckily, PowerPoint has a feature that's perfect for this: Tags. 幸运的是,PowerPoint具有一个非常适合此功能:标记。

With ActivePresentation
  .Tags.Add "ProjectID", Cstr(intProjID)
End With

The string version of intProjID is now a permanent "tag" attached to the presentation object. 现在,intProjID的字符串版本是附加到表示对象的永久“标记”。 To retrieve it: 要检索它:

MsgBox ActivePresentation.Tags("ProjectID")

Each presentation, slide and shape can have pretty much as many of these tags as you like. 每个演示文稿,幻灯片和形状都可以包含任意数量的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM