1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

帖子
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 163|回复: 2

不会用TwoColorGradient和SchemeColor,必须学会RGB。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2025-4-12 07:53 | 显示全部楼层 |阅读模式
RGB颜色对照表  https://tool.oschina.net/commons?type=3

image.png

image.png

image.png

image.png


image.png


image.png
SchemeColor 属性 (Excel Graph) | Microsoft Learn  https://learn.microsoft.com/zh-c ... i/excel.schemecolor

返回或设置指定 ChartColorFormat 对象的颜色作为当前配色方案中的索引。没有找到Schemecolor对应的值。
  1. With myChart.ChartArea.Fill
  2. .Visible = True
  3. .ForeColor.SchemeColor = 15
  4. .BackColor.SchemeColor = 17
  5. .TwoColorGradient msoGradientHorizontal, 1
  6. End With
复制代码














image.png

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-4-12 19:37 | 显示全部楼层
microsoft learn powerpoint VBA TwoColorGradient msoGradientHorizontal, 3_360搜索  https://www.so.com/s?q=microsoft ... fad75c37be7eb0513c7

  1. Sub SetTwoColorGradient()
  2.     Dim slide As Slide
  3.     Dim shape As Shape
  4.    
  5.     Set slide = ActivePresentation.Slides(1)
  6.     ' 创建矩形作为背景(或选择现有形状)
  7.     Set shape = slide.Shapes.AddShape(msoShapeRectangle, 0, 0, _
  8.         slide.Master.Width, slide.Master.Height)
  9.    
  10.     With shape.Fill
  11.         .ForeColor.RGB = RGB(255, 0, 0)   ' 渐变主色(如红色)
  12.         .BackColor.RGB = RGB(0, 0, 255)   ' 渐变次色(如蓝色)
  13.         .TwoColorGradient msoGradientHorizontal, 3
  14.         .Visible = msoTrue
  15.     End With
  16.    
  17.     shape.ZOrder msoSendToBack  ' 将形状置于底层
  18. End Sub
复制代码



2. 参数解析
msoGradientHorizontal:表示水平渐变方向,其他可选值包括:

plaintext
复制
msoGradientVertical
(垂直)
plaintext
复制
msoGradientDiagonalUp
(对角线向上)
plaintext
复制
msoGradientDiagonalDown
(对角线向下)
参数3:代表渐变变体类型,取值范围1-4:

1:主色占比25%/次色75%
2:主色50%/次色50%
3:主色75%/次色25%
4:主色中心辐射渐变
3. 高级应用技巧
动态尺寸适配:通过
plaintext
复制
slide.Master.Width

plaintext
复制
slide.Master.Height
获取幻灯片母版尺寸,确保背景全覆盖
颜色自定义:使用
plaintext
复制
RGB()
函数或
plaintext
复制
ObjectThemeColor
属性实现主题色绑定
多对象控制:结合
plaintext
复制
Shapes.Range
批量处理多个形状的渐变效果
4. 验证与调试建议
在VBA编辑器中按F8逐行调试
使用
plaintext
复制
Debug.Print shape.Fill.GradientVariant
验证渐变参数
通过
plaintext
复制
shape.Fill.Visible = msoFalse
临时隐藏效果进行对比测试
建议在实际操作前备份演示文稿,并通过Alt+F11打开VBA编辑器测试代码。如需扩展功能,可参考4中关于
plaintext
复制
Shapes
对象集合的操作方法。



TA的精华主题

TA的得分主题

 楼主| 发表于 2025-4-12 19:45 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 ning84 于 2025-4-12 20:18 编辑

FillFormat.TwoColorGradient 方法 (PowerPoint) | Microsoft Learn  https://learn.microsoft.com/zh-c ... at.twocolorgradient



语法
表达式。TwoColorGradient (样式、 变体)

表达 一个代表 FillFormat 对象的变量。

参数
名称        必需/可选        数据类型        说明
样式        必需        MsoGradientStyle        渐变样式。
Variant        必需        Long        渐变变量。 可以为 1 到 4,对应于“形状填充”选项卡上的“渐变”子选项卡上的四个变体。如果 Style 为 msoGradientFromTitle 或 msoGradientFromCenter,则此参数可以是 1 或 2。
示例
本示例向 myDocument 中添加一个具有双色渐变填充效果的矩形,并设置填充的前景色和背景色。

VB

复制
Set myDocument = ActivePresentation.Slides(1)

With myDocument.Shapes.AddShape(Type:=msoShapeRectangle, Left:=0, _
        Top:=0, Width:=40, Height:=80).Fill

    .ForeColor.RGB = RGB(Red:=128, Green:=0, Blue:=0)
    .BackColor.RGB = RGB(Red:=0, Green:=170, Blue:=170)
    .TwoColorGradient Style:=msoGradientHorizontal, Variant:=1

End With

  1. Function StarttimeToEndtimeForStreetsnapToTexBox(Shp As PowerPoint.Shape, DateArr, FontSize1, FontSize2)
  2.    Dim Str, EngDateStr, ChiDateStr
  3.    Dim N1, N2
  4.        EngDateStr = "Stree Snap From " & Format(DateArr(0), "h:mm") & " to " & Format(DateArr(1), "h:mm") & " on " & Format(DateArr(0), "mmmm d,yyyy")
  5.        ChiDateStr = Format(DateArr(0), "yyyy年m月d日h:mm") & "到" & Format(DateArr(1), "h:mm") & "的街拍"
  6.        With Shp
  7.            'If Shp.Type = 14 Then
  8.                .TextFrame2.TextRange.Text = ChiDateStr & vbCr & EngDateStr
  9.                N1 = Len(ChiDateStr)
  10.                .TextFrame.TextRange.Characters(0, N1).Font.Size = FontSize1
  11.                N2 = Len(ChiDateStr & vbCr & EngDateStr)
  12.                .TextFrame.TextRange.Characters(N1 + 1, N2).Font.Size = FontSize2
  13.            'End If
  14.        End With
  15. End Function


  16. Function ParseFilenameToDate(Str)
  17.     Dim Arr1, Arr2, Str1, Str2
  18.    
  19.         Arr1 = Split(Str, "_")
  20.         Arr2 = Split(Arr1(1), "-")
  21.         Str1 = Arr1(0) & Arr2(0)
  22.         Str2 = Arr1(0) & Arr2(1)
  23.     Dim DateArr(1) As Date
  24.         DateArr(0) = Format(Str1, "0000/00/00 00:00")
  25.         DateArr(1) = Format(Str2, "0000/00/00 00:00")
  26.         
  27.     ParseFilenameToDate = DateArr
  28.    
  29. End Function
  30. ''



  31. Sub L1()
  32.    Dim Pres As PowerPoint.Presentation
  33.        Set Pres = Application.ActivePresentation
  34.    Dim PathName
  35.        With Pres
  36.            Debug.Print .Name, .Path, .FullName
  37.        End With
  38.        PathName = Pres.FullName
  39.   Dim DateArr, StaartEndStr
  40.        DateArr = ParseFilenameToDate(Left(Pres.Name, Len(Pres.Name) - 5))
  41.    Dim Sld As Slide, Slds As Slides, SldRng As SlideRange
  42.       
  43.        Set Slds = Pres.Slides
  44.    Dim Shp As Shape, Shps As Shapes
  45.        Set SldRng = Application.ActiveWindow.Selection.SlideRange
  46.       
  47.             Set Shps = SldRng.Shapes
  48.             StartEndStr = Left(Pres.Name, Len(Pres.Name) - 5)
  49.             
  50.             StarttimeToEndtimeForStreetsnapToTexBox Shps("Txt1"), DateArr, 14, 8
  51.             StarttimeToEndtimeForStreetsnapToTexBox Shps("Txt2"), DateArr, 20, 15
  52.             For ii = 1 To Shps.Count
  53.                   Set Shp = Shps(ii)
  54.                   With Shp
  55.                        Debug.Print "    '" & .Name, .Type
  56.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(0) =" & .Left
  57.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(1) =" & .Top
  58.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(2) =" & .Width
  59.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(3) =" & .Height
  60.                   End With
  61.             Next ii
  62.       
  63. End Sub

  64. ''

  65. Sub Lll1()
  66.    Dim Pres As PowerPoint.Presentation
  67.        Set Pres = Application.ActivePresentation
  68.       
  69.    Dim PathName
  70.        With Pres
  71.            Debug.Print .Name, .Path, .FullName
  72.        End With
  73.        PathName = Pres.FullName
  74.   Dim DateArr, StartEndStr
  75.        DateArr = ParseFilenameToDate(Left(Pres.Name, Len(Pres.Name) - 5))
  76.    Dim Sld As Slide, Slds As Slides, SldRng As SlideRange
  77.    Dim Txt1Shp As Shape, Txt2Shp As Shape, Pic1Shp As Shape, Pic2Shp As Shape
  78.    Dim ShpRng As ShapeRange
  79.        Set Slds = Pres.Slides
  80.    Dim Shp As Shape, Shps As Shapes
  81.        For Each Sld In Slds
  82.             Set Shps = Sld.Shapes
  83.             StartEndStr = Left(Pres.Name, Len(Pres.Name) - 5)
  84.             Set Txt1Shp = Shps("Txt1")
  85.             With Txt1Shp
  86.                  StarttimeToEndtimeForStreetsnapToTexBox Txt1Shp, DateArr, 13, 10
  87.                  .Left = 420
  88.                  .Top = 50
  89.                  .Width = 220
  90.                  .Height = 100
  91.                  .Select
  92.                  With .TextFrame
  93.                      .TextRange.ParagraphFormat.Alignment = ppAlignCenter
  94.                      .AutoSize = ppAutoSizeNone
  95.                      .VerticalAnchor = msoAnchorMiddle
  96.                  End With
  97.                  
  98.                  
  99.             End With
  100.             Set Txt2Shp = Shps("Txt2")
  101.             With Txt2Shp
  102.                   .Left = 420 + 260
  103.                   .Top = 50
  104.                   .Width = 220
  105.                   .Height = 100
  106.                   StarttimeToEndtimeForStreetsnapToTexBox Txt2Shp, DateArr, 13, 10
  107.                   With .TextFrame
  108.                      .TextRange.ParagraphFormat.Alignment = ppAlignCenter
  109.                      .AutoSize = ppAutoSizeNone
  110.                      .VerticalAnchor = msoAnchorMiddle
  111.                   End With
  112.                   With .Fill
  113.                      .BackColor.RGB = RGB(118, 238, 198)
  114.                      '.ForeColor.SchemeColor = ppShadow '  .ForeColor.SchemeColor = 15
  115.                      .ForeColor.RGB = RGB(230, 230, 250)
  116.                      '.BackColor.SchemeColor = ppShadow
  117.                      .TwoColorGradient msoGradientHorizontal, 3
  118.                      '.TwoColorGradient msoGradientHorizontal, 2
  119.                      Debug.Print .GradientVariant
  120.                      
  121.                   End With
  122.                   .Select
  123.                   Stop
  124.                   Stop
  125.             End With
  126.             Set Pic1Shp = Shps(1)
  127.             With Pic1Shp
  128.                 .Left = 50 ' Txt1Shp.Width
  129.                 .Top = 50 'Txt2Shp.Height
  130.                 .Width = 260
  131.                 .Height = Pres.PageSetup.SlideHeight - 50 * 2
  132.                 .Select
  133.                
  134.                
  135.             End With
  136.             Set Pic2Shp = Shps(2)
  137.             With Pic2Shp
  138.                 .Left = 420
  139.                 .Top = 200
  140.                 .Width = 500
  141.                 .Height = 300
  142.                 .Select
  143.             End With
  144.             
  145.             
  146.             For ii = 1 To Shps.Count
  147.                   Set Shp = Shps(ii)
  148.                   With Shp
  149.                        Debug.Print "    '" & .Name, .Type
  150.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(0) =" & .Left
  151.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(1) =" & .Top
  152.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(2) =" & .Width
  153.                        Debug.Print Space(8) & "arr(" & ii - 1 & ")(3) =" & .Height
  154.                   End With
  155.             Next ii
  156.        Next Sld
  157. End Sub

复制代码
image.png
image.png

20250407_1309-1324.zip

1016.12 KB, 下载次数: 4

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-4-13 11:50 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 ning84 于 2025-4-13 19:23 编辑

学习,学习,再学习

  1. Public Sub Add_Example()
  2.     Dim Pres As PowerPoint.Presentation
  3.         
  4.     Dim pptSlide As Slide
  5.     Dim pptLayout As CustomLayout
  6.     Dim Sld As Slide, Slds As Slides
  7.         Set Slds = ActivePresentation.Slides
  8.         For ii = Slds.Count To 1 Step -1
  9.             Set Sld = Slds(ii)
  10.             Sld.Delete
  11.         Next ii
  12.    
  13.     Dim oStyle As Integer, oVar As Integer
  14.     For ii = 1 To 20
  15.         'Set Sld = Slds.AddSlide(ii, ppLayoutBlank)
  16.         Set Sld = Slds.Add(Slds.Count + 1, ppLayoutBlank)
  17.         oStyle = oStyle + 1
  18.         oVar = oVar + 1
  19.         Sld.FollowMasterBackground = msoFalse
  20.         With Sld.Background.Fill
  21.                         '.ForeColor.RGB = RGB(141 + ii, 238 + ii, 238 + ii)
  22.                         '.BackColor.RGB = RGB(139, 129, 139)
  23.                         .ForeColor.RGB = RGB(1 + ii * 10, 100 + ii * 3, 200 + ii)
  24.                         '.BackColor.RGB = RGB(255, 0, 0)
  25.                         '.BackColor.RGB = RGB(0, 0, 255)
  26.                         .BackColor.RGB = RGB(0, 0, 0)
  27.              If oStyle > 7 Then
  28.                  oStyle = 1
  29.              End If
  30.              If oVar > 4 Then
  31.                  oVar = 1
  32.              End If
  33.              '.TwoColorGradient oStyle, oVar
  34.              .TwoColorGradient msoGradientHorizontal, 1
  35.              .Visible = msoCTrue
  36.         End With
  37.         
  38.     Next ii
  39.    
  40.    

  41. End Sub
  42. ''
  43. Sub SetRandomSlideBackgroundColor()
  44.     Dim Sld As Slide
  45.     Dim Pres As PowerPoint.Presentation
  46.     Set Pres = Application.ActivePresentation
  47.     Dim R As Integer, G As Integer, B As Integer
  48.     Dim SldIndex As Integer
  49.     SldIndex = 2 '这里以第2张幻灯片为例,你可修改为所需幻灯片序号
  50.     Set Sld = Pres.Slides(SldIndex)
  51.     Sld.FollowMasterBackground = msoFalse
  52.     '在青色相关范围内生成随机RGB值
  53.     R = Int((127 - 0 + 1) * Rnd + 0) '青色相关范围内的R值范围(示例)
  54.     G = Int((255 - 100 + 1) * Rnd + 100) 'G值范围(示例)
  55.     B = Int((255 - 200 + 1) * Rnd + 200) 'B值范围(示例)
  56.     With Sld.Background.Fill
  57.        .Visible = msoTrue
  58.        .Solid
  59.        .ForeColor.RGB = RGB(R, G, B)
  60.     End With
  61. End Sub

  62. Sub dddd()
  63.     Dim Sld As Slide, Slds As Slides
  64.         Set Slds = Application.ActivePresentation.Slides
  65.         For ii = Slds.Count To 1 Step -1
  66.             Set Sld = Slds(ii)
  67.             Sld.Delete
  68.         Next ii
  69.    Dim R, G, B
  70.         For ii = 1 To 30
  71.             Set Sld = Slds.Add(ii, ppLayoutBlank)
  72.             Debug.Print Sld.Name
  73.             R = Int((127 - 0 + 1) * Rnd + 0) '青色相关范围内的R值范围(示例)
  74.             G = Int((255 - 100 + 1) * Rnd + 100) 'G值范围(示例)
  75.             B = Int((255 - 200 + 1) * Rnd + 200) 'B值范围(示例)
  76.             Sld.FollowMasterBackground = msoFalse
  77.             With Sld.Background.Fill
  78.                 .Visible = msoCTrue
  79.                 .ForeColor.RGB = RGB(R, G, B)
  80.                 .BackColor.RGB = RGB(R, G, B)
  81.                 .TwoColorGradient msoGradientHorizontal, 1
  82.             End With
  83.         Next ii
  84.         
  85. End Sub


  86. Sub ddddd()
  87.     Dim Sld As Slide, Slds As Slides
  88.     Set Slds = Application.ActivePresentation.Slides
  89.     '删除原有幻灯片
  90.     For ii = Slds.Count To 1 Step -1
  91.         Set Sld = Slds(ii)
  92.         Sld.Delete
  93.     Next ii
  94.    
  95.     For ii = 1 To 30
  96.         Set Sld = Slds.Add(ii, ppLayoutBlank)
  97.         Debug.Print Sld.Name
  98.         '生成ForeColor的随机RGB值
  99.         SldChangeTwoColorGradient Sld
  100.     Next ii
  101. End Sub

  102. Function SldChangeTwoColorGradient(Sld As Slide)
  103.     Dim R, G, B
  104.     Dim R2, G2, B2 '用于存储BackColor的RGB值
  105.         
  106.         R = Int((127 - 0 + 1) * Rnd + 0)
  107.         G = Int((255 - 100 + 1) * Rnd + 100)
  108.         B = Int((255 - 200 + 1) * Rnd + 200)
  109.         '生成BackColor的随机RGB值,这里简单示例为在ForeColor基础上微调
  110.         R2 = Int((R + 50) / 2)
  111.         G2 = Int((G + 50) / 2)
  112.         B2 = Int((B + 50) / 2)
  113.         
  114.         Sld.FollowMasterBackground = msoFalse
  115.         With Sld.Background.Fill
  116.            .Visible = msoCTrue
  117.            .ForeColor.RGB = RGB(R, G, B)
  118.            .BackColor.RGB = RGB(R2, G2, B2)
  119.            '.TwoColorGradient msoGradientHorizontal, Int((4 - 1 + 1) * Rnd + 1)
  120.            .TwoColorGradient Int((7 - 1 + 1) * Rnd + 1), Int((4 - 1 + 1) * Rnd + 1)
  121.         End With

  122. End Function
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

1234

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-4-25 05:16 , Processed in 0.025938 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表