VBA使用VBA代码替换另一个模块中的代码

时间:2017-06-07 08:04:01

标签: vba powerpoint

我在一个模块中有一行代码:

City = "Paris"

在一个单独的模块中,我需要根据用户从下拉列表中选择的内容来更改城市名称。我的代码将改变整行,如下所示:

Sub ChangeUserCity()
Call Dictionary.CityLocation
Dim UserChosenCity As String
Dim SL As Long, EL As Long, SC As Long, EC As Long
Dim S As String
Dim Found As Boolean

ComboBoxList = Array(CStr(CityName)) 'This is the name of the combodropdown box with the list of city names.

For Each Ky In ComboBoxList
'On Error Resume Next

UserChosenCity = dict4.Item(Ky)(0) 'This refers to the dictionary that has the list of city names. It grabs the string (the name of the city).

With ActivePresentation.VBProject.VBComponents("Dictionary").CodeModule
SL = 1
SC = 1
EL = -1
EC = -1
Found = .Find("City = " & """" & "Paris" & """", SL, SC, EL, EC, True, False, False)
If Found = True Then
S = .Lines(SL, 1)
S = Replace(S, "City = " & """" & "Paris" & """", "City= " & """" & UserChosenCity & """")
.ReplaceLine SL, S
End If
End With
Next Ky
End Sub

此代码的工作方式问题是城市名称并不总是“巴黎”。它可以是任何字符串(即任何城市名称)。所以我真正需要的代码就是用UserChosenCity替换引号之间的城市名称。有关如何实现这一点的任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

在幻灯片中添加一个组合框和一个文本框。

使用幻灯片1上的ComboBox1和TextBox1,此代码将值从组合框移动到文本框:

Private Sub ComboBox1_Change()
    Dim oComboBox As ComboBox
    Dim oTextBox As TextBox

    Set oComboBox = ActivePresentation.Slides("Slide1").Shapes("ComboBox1").OLEFormat.Object
    Set oTextBox = ActivePresentation.Slides("Slide1").Shapes("TextBox1").OLEFormat.Object

    oTextBox.Value = oComboBox.Value

    'or

    Slide1.TextBox1.Value = Slide1.ComboBox1.Value

End Sub

注意: Powerpoint不是我的强项所以可能有一种“正确”的方式来存储PPT中的值。

现在,您可以在保存,关闭和重新打开演示文稿后从文本框中检索值(说 - 当我重新打开它时,组合框也保留了该值)并在代码的其他位置使用该值