PPT VSTO根据存储的颜色变量设置字体颜色

人气:997 发布:2022-09-22 标签: vsto

问题描述

大家好,

为自定义功能区选项卡构建一些按钮,我们存储了一些预设颜色。尝试编写代码以根据存储的变量设置字体颜色,例如使用标准的紫色和橙色来测试这个,我有一些全局变量:

Public ReadOnly Colours_Purple As Color = Color.FromArgb(112,48,160) Public ReadOnly Colours_Orange As Color = Color.FromArgb(255,192,0)

然后我有一个

Dim ColourChoice作为Drawing.Color       Select Case ChooseColour          案例"紫色"               ColourChoice = Colours_Purple          

        案例"橙色"               ColourChoice = Colours_Orange

Etc

然后我尝试使用它:

Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.Font.Color = ColourChoice

但是得到错误 '颜色'是只读的。

我可以找到的所有替代方法,例如在下面的链接中,需要在代码行中指定RBG代码,尽管这是想要仅改变RGB代码一次的可能性不高。

https://stackoverflow.com/questions/5247135/how-can-i-change-the-font- color-of-a-textrange-in-powerpoint-from-c

如何使用颜色变量设置所选文本的颜色?

解决方案

您好cptjonkinbllll,

请尝试,

< pre class ="prettyprint"> Dim objSelection As PPT.Selection objSelection = Globa ls.ThisAddIn.Application.ActiveWindow.Selection 尺寸objFont作为对象。如果objSelection.Type = PPT.PpSelectionType.ppSelectionShapes然后 objFont = objSelection.ShapeRange.Item(1).TextFrame.TextRange .Font objFont.Color.RGB = Colours_Purple.ToArgb ,否则 MSGBOX(QUOT;您的选择项不包含任何形状")结束如果

最好的问候,

Terry

Hi All,

Building some buttons for a custom ribbon tab and we have some pre-set colours stored. Trying to write code to set font colour based on stored variables e.g. to test this out using the standard purple and orange, I have some global variables:

Public ReadOnly Colours_Purple As Color = Color.FromArgb(112, 48, 160) Public ReadOnly Colours_Orange As Color = Color.FromArgb(255, 192, 0)

I then have a sub that has

Dim ColourChoice As Drawing.Color Select Case ChooseColour Case "Purple" ColourChoice = Colours_Purple

Case "Orange" ColourChoice = Colours_Orange

Etc

And I'm then trying to use it like this:

Globals.ThisAddIn.Application.ActiveWindow.Selection.TextRange.Font.Color = ColourChoice

But get error that 'color' is read only.

All alternatives I can find such as at the link below require specifying the RBG code in the line of code though this is not efficient for the possibility of wanting the change the RGB codes only once.

https://stackoverflow.com/questions/5247135/how-can-i-change-the-font-color-of-a-textrange-in-powerpoint-from-c

How do I set colour of selected text using a color variable?

解决方案

Hello cptjonkinbllll,

Please try,

Dim objSelection As PPT.Selection
        objSelection = Globals.ThisAddIn.Application.ActiveWindow.Selection
        Dim objFont As Object
        If objSelection.Type = PPT.PpSelectionType.ppSelectionShapes Then
            objFont = objSelection.ShapeRange.Item(1).TextFrame.TextRange.Font
            objFont.Color.RGB = Colours_Purple.ToArgb
        Else
            MsgBox("Your select item does not contain any shape")
        End If

Best Regards,

Terry

426