Powerpoint VBA-将RGB颜色作为变量传递

Powerpoint VBA-将RGB颜色作为变量传递,vba,variables,powerpoint,rgb,Vba,Variables,Powerpoint,Rgb,我希望允许用户通过文本框输入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

我希望允许用户通过文本框输入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

这是我做的,我通常使用Excel,所以可能有更好的方法来做这件事。此外,如果用户没有以正确的格式“#、#、#”输入数字,我建议进行一些错误捕获。但这实际上会获取默认颜色的字符串或用户输入的颜色,将其拆分为3部分,然后将其传递给RGB()函数

Dim osld As Slide
Dim oshp As Shape
Dim strMainColor As String, strSecondColor As String
'these are new
Dim MainInt As Variant, SecondInt As Variant

'Set main color to default if users didn't enter a RGB value
If MainColor.Value = "" Then
    strMainColor = "73, 109, 164"
    MainInt = Split(strMainColor, ",")
Else
    strMainColor = MainColor.Value
    MainInt = Split(strMainColor, ",")
End If

'Set Secondary color to default if users didn't enter a RGB value
If SecondColor.Value = "" Then
    strSecondColor = "207, 203, 201"
    SecondInt = Split(strSecondColor, ",")
Else
    strSecondColor = SecondColor.Value
    SecondInt = Split(strSecondColor, ",")
End If

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(MainInt(0), MainInt(1), MainInt(2))
   oshp.Fill.BackColor.RGB = RGB(MainInt(0), MainInt(1), MainInt(2))
   ElseIf Right(oshp.Name, 2) = "_2" Then
    'Secondary Colors
    oshp.Fill.ForeColor.RGB = RGB(SecondInt(0), SecondInt(1), SecondInt(2))
    oshp.Fill.BackColor.RGB = RGB(SecondInt(0), SecondInt(1), SecondInt(2))
End If
Next oshp
Next osld

正如其他人所建议的,RGB定义不能由字符串提供

如何创建一个自定义类型“颜色”,并使用它将颜色传递到任何需要的地方

如果要使用此选项,请不要忘记将自定义类型定义块(类型颜色)放在行
Sub Test()之前

那就用这个吧

标准模块中的代码:

选项显式
私有常量CC_FULLOPEN=&H2
专用DWCUSTCLR(0到15)长度相同
#如果是VBA7,则
专用型彩色结构
l结构尺寸与长度相同
hwndOwner作为LongPtr
hInstance As LongPtr
rgbResult尽可能长
将颜色设置为LongPtr
旗子一样长
lCustData作为LongPtr
lpfnHook As LongPtr
lpTemplateName作为字符串
端型
#否则
专用型彩色结构
l结构尺寸与长度相同
hwndOwner尽可能长
只要
rgbResult尽可能长
我们的颜色一样长
旗子一样长
lCustData尽可能长
长钩
lpTemplateName作为字符串
端型
#如果结束
#如果是VBA7,则
私有声明PtrSafe函数ChooseColor Lib“comdlg32.dll”别名“ChooseColorA”(pChoosecolor作为COLORSTRUC)的长度为
#否则
私有声明函数ChooseColor Lib“comdlg32.dll”别名“ChooseColorA”(pChoosecolor作为COLORSTRUC)的长度为
#如果结束
私有子集合customcolors()'在此处定义选择器的自定义颜色。
dwCustClrs(0)=vbBlack
dwCustClrs(1)=vbWhite
dwCustClrs(2)=vbRed
dwCustClrs(4)=vbGreen
dwCustClrs(5)=vbBlue
dwCustClrs(6)=RGB(0,0,0)
dwCustClrs(7)=黑色
dwCustClrs(8)=黑色
dwCustClrs(9)=黑色
dwCustClrs(10)=黑色
dwCustClrs(11)=黑色
dwCustClrs(12)=黑色
dwCustClrs(13)=黑色
dwCustClrs(14)=黑色
dwCustClrs(15)=黑色
端接头
公共函数ColorPickerDialog(可选默认颜色为Long=vbWhite)为Long
暗x为长,CS为彩色结构
如果所有自定义颜色都应为黑色,则设置自定义颜色的注释
CS.lStructSize=LenB(CS)'非Len,请参见https://codekabinett.com/rdumps.php?Lang=2&targetDoc=windows-api-declaration-vba-64位结尾
CS.flags=CC_完全打开
CS.lpCustColors=VarPtr(dwcustcls(0))
x=选择颜色(CS)
如果x=0,则
ColorPickerDialog=DefaultColor
退出功能
其他的
ColorPickerDialog=CS.rgbResult
如果结束
端函数
设置形状:

将osld设置为幻灯片
将oshp调暗为形状
暗淡主色与长色相同,次色与长色相同
“选择主色调
MainColor=ColorPickerDialog(RGB(73,109,164))'如果没有选择颜色,则使用默认颜色RGB(73,109,164)
“选择第二种颜色
SecondColor=ColorPickerDialog(RGB(207203201))
对于ActivePresentation.Slides中的每个osld
对于osld形状中的每个oshp
与oshp合作
如果正确(.Name,2)=“_1”,则
'所有幻灯片的主颜色
.Fill.ForeColor.RGB=MainColor
.Fill.BackColor.RGB=MainColor
如果正确(.Name,2)=“_2”,则
"二次色",
.Fill.ForeColor.RGB=SecondColor
.Fill.BackColor.RGB=SecondColor
如果结束
以
下一个oshp
下一个奥斯陆

RGB()函数不能像第一个循环中的字符串那样使用。此外,不能将字符串传递到RGB()函数中,因为它需要3个整数。最好的方法是生成3个用户可以更改的整数变量,然后将其传递到RGB()函数中。RGB颜色是一个
长的
(最好用十六进制文字表示,例如
&H00FFDD
),而不是
字符串
…感谢您的响应。我接受了Jeebo的建议,为每个RGB创建了一个变量,并分别传递了每个变量。我尝试了你的回答,因为理想情况下,如果有效的话,那将是最好的解决方案,但似乎什么都没有发生。我粘贴了上面的代码,并更新了我的点击按钮,现在可以使用您的脚本了,但现在什么都没有发生。是否应该使用脚本提示我选择主颜色和第二颜色?是,对话框应显示两次。是否执行了代码(通过断点或Msgbox检查)?代码编译(Vba->调试->编译Vba项目)?办公室版和Bitness?嗨,电脑管理员,谢谢你的回复,我感谢你的耐心。我试着用msgbox来储存ColorPickerDialog,结果它返回了一个数字。我假设该数字是函数中设置的默认颜色。它不会提示我选择颜色。就office版本而言,我使用的是PowerPoint 365。比特数?如果是x64,请在此处尝试示例。(在末尾)。尝试编辑代码(从Len到LenB)。
Option Explicit

Type Color
    R As Integer
    G As Integer
    B As Integer
End Type

Sub Test()

    Dim osld As Slide
    Dim oshp As Shape
    Dim MainColor As Color
    Dim SecondColor As Color

    'Set main color to default if users didn't enter a RGB value
    With MainColor
        If .R = 0 And .G = 0 And .B = 0 Then
            .R = 73
            .G = 109
            .B = 164
        End If
    End With

    'Set Secondary color to default if users didn't enter a RGB value
    With SecondColor
        If .R = 0 And .G = 0 And .B = 0 Then
            .R = 207
            .G = 203
            .B = 201
        End If
    End With


    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(MainColor.R, MainColor.G, MainColor.B)
                oshp.Fill.BackColor.RGB = RGB(MainColor.R, MainColor.G, MainColor.B)
            ElseIf Right(oshp.Name, 2) = "_2" Then
                'Secondary Colors
                oshp.Fill.ForeColor.RGB = RGB(SecondColor.R, SecondColor.G, SecondColor.B)
                oshp.Fill.BackColor.RGB = RGB(SecondColor.R, SecondColor.G, SecondColor.B)
            End If
        Next oshp
    Next osld

End Sub