【发布时间】:2019-09-03 17:57:50
【问题描述】:
我希望允许用户通过文本框输入 RGB 颜色并传递该变量以更改所有形状的颜色。我写了一个循环,它会查看形状名称的最后 2 个字符,以确定是否应该将其更改为主要颜色或次要颜色。
这是来自最新版 Office 365 的 powerpoint。
我尝试了以下代码。我收到类型不匹配或无效参数错误:
Dim osld As Slide
Dim oshp As Shape
Dim strMainColor As String, strSecondColor As String
'Set main color to default if users didn't enter a RGB value
If MainColor.Value = "" Then strMainColor = "73, 109, 164" Else strMainColor = MainColor.Value
'Set Secondary color to default if users didn't enter a RGB value
If SecondColor.Value = "" Then strSecondColor = "207, 203, 201" Else strSecondColor = SecondColor.Value
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If Right(oshp.Name, 2) = "_1" Then
'Main Color to all slides
oshp.Fill.ForeColor.RGB = "RGB(" + strMainColor + ")"
oshp.Fill.BackColor.RGB = "RGB(" + strMainColor + ")"
ElseIf Right(oshp.Name, 2) = "_2" Then
'Secondary Colors
oshp.Fill.ForeColor.RGB = "RGB(" + strSecondColor + ")"
oshp.Fill.BackColor.RGB = "RGB(" + strSecondColor + ")"
End If
Next oshp
Next osld
Dim osld As Slide
Dim oshp As Shape
Dim strMainColor As String, strSecondColor As String
'Set main color to default if users didn't enter a RGB value
If MainColor.Value = "" Then strMainColor = "73, 109, 164" Else strMainColor = MainColor.Value
'Set Secondary color to default if users didn't enter a RGB value
If SecondColor.Value = "" Then strSecondColor = "207, 203, 201" Else strSecondColor = SecondColor.Value
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If Right(oshp.Name, 2) = "_1" Then
'Main Color to all slides
oshp.Fill.ForeColor.RGB = RGB(strMainColor)
oshp.Fill.BackColor.RGB = RGB(strMainColor)
ElseIf Right(oshp.Name, 2) = "_2" Then
'Secondary Colors
oshp.Fill.ForeColor.RGB = RGB(strSecondColor)
oshp.Fill.BackColor.RGB = RGB(strSecondColor)
End If
Next oshp
Next osld
【问题讨论】:
-
RGB() 函数不能像第一个循环中的字符串那样使用。此外,您不能将字符串传递给 RGB() 函数,因为它需要 3 个整数。最好的办法是制作 3 个用户可以更改的整数变量,然后将其传递给 RGB() 函数。
-
RGB 颜色是
Long(最好用十六进制文字表示,例如&H00FFDD),而不是String... -
感谢您的回复。我接受了 TheJeebo 的建议,为每个 RGB 创建了一个变量,并分别传递了每个变量。
标签: vba variables powerpoint rgb