2016-10-02 67 views
0

我对vba非常陌生(仅在昨天刚刚开始),但希望使用代码来节省一些时间,使用PowerPoint填充表单。到目前为止,我已经想出了如何生成带有结果文本框的输入框,并且如果只输入一个单词并且匹配,则更改字体颜色。然而,这并不完全符合我的需求。如何更改输入框中特定单词的颜色vba powerpoint vba初学者

出于我的目的,用户输入约定是“Word1-Word2-Word3-Word4-Word5”。例如,“红 - 黄 - 橙 - 绿 - 蓝”。我希望红色变成红色,黄色变成黄色,橙色变成橙色,等等。虚线将保持黑色。输入可以是任意顺序,并不是所有部件都可以存在(即“黄 - 红 - 绿 - 蓝 - 橙”或“红 - 蓝 - 绿”),但需要保留颜色编码。

<here is where I should put my current code that doesn't exactly fit my needs> 

任何人都可以帮助我吗?

+1

这不是代码写入服务。你到目前为止尝试了什么?发布您的代码!当你运行它时发生了什么?你预期会发生什么?你有什么特别的问题? – Robert

+0

向我们显示您的代码! –

+0

在问你的下一个问题之前,请阅读http://stackoverflow.com/help/how-to-ask。 –

回答

0

正如指出的那样,我们不是一个代码写入服务。但是,一旦你提供了基本代码,你仍然可能会遇到一些PPT处理文本的怪癖。所以让我们短路一些。

我们假设您将采用用户提供的文本字符串并将其添加到幻灯片的文本框中。以下示例假定选择了文本框;您的代码将需要插入一个新的文本框并在变量中获取对它的引用(在这种情况下为oSh)。

Sub Example() 
' This looks for the word "red" in the currently selected 
' shape's text and if found, turns it red. 

    Dim oSh As Shape 
    Dim oRng As TextRange 

    ' Get a reference to the currently selected shape 
    ' For what you're doing, you'd need slightly different code 
    Set oSh = ActiveWindow.Selection.ShapeRange(1) 

    ' Get a reference to the text range that contains the word "red" 
    Set oRng = oSh.TextFrame.TextRange.Find("red") 

    ' Change the color of the text range containing the word "red" 
    ' RealWorld Notes: 
    ' Test to see if oRng Is Nothing ... which it'll be if there's 
    ' no word "red" in the string 
    ' You'll need to deal with capitalization: "red" vs "Red" etc.   
    oRng.Font.Color.RGB = RGB(255, 0, 0) 

End Sub