MS Word VBA:我需要一个调色板对话框 [英] MS Word VBA: I need a color palette dialog box

查看:294
本文介绍了MS Word VBA:我需要一个调色板对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VBA for MS Word 2010中,如何使Word弹出调色板对话框,以便用户可以选择颜色?

In VBA for MS Word 2010, how can I get Word to bring up a color palette dialog box so the user can pick a color?

关于如何在Excel中执行操作,有很多示例,但是我还没有为Word用户找到任何帮助.这是Excel的代码:

There are tons of examples on how to do it in Excel, but I haven't found any help for Word users. Here's the code for Excel:

Application.Dialogs(xlDialogPatterns).Show

问题是,没有wdDialogPatterns等效项,我也找不到名称暗示调色板对话框的名称.我发现了wdFormatBordersAndShading,但并不完全相同:我希望用户选择一种颜色,以便以后重复使用.

Problem is, there's no wdDialogPatterns equivalent, nor can I find anything with a name that suggests a color palette dialog. I've found wdFormatBordersAndShading, but it's not quite the same: I want the user to select a color for later and repeated use.

谢谢!

推荐答案

在x64 Word上,您必须按以下方式修改Ádám的代码:

On x64 Word you must modify Ádám's code below as follows:

Option Explicit
Option Base 0

Private Type CHOOSECOLOR
  lStructSize As LongLong
  hwndOwner As LongPtr
  hInstance As LongPtr
  rgbResult As LongLong
  lpCustColors As LongPtr
  flags As LongLong
  lCustData As LongLong
  lpfnHook As LongLong
  lpTemplateName As String
End Type

Private Declare PtrSafe Function MyChooseColor _
    Lib "comdlg32.dll" Alias "ChooseColorW" _
    (ByRef pChoosecolor As CHOOSECOLOR) As Boolean

Public Function GetColor(ByRef col As LongLong) As _
    Boolean

  Static CS As CHOOSECOLOR
  Static CustColor(15) As LongLong

  CS.lStructSize = Len(CS)
  CS.hwndOwner = 0
  CS.flags = &H1 Or &H2
  CS.lpCustColors = VarPtr(CustColor(0))
  CS.rgbResult = col
  CS.hInstance = 0
  GetColor = MyChooseColor(CS)
  If GetColor = False Then Exit Function

  GetColor = True
  col = CS.rgbResult
End Function

例如,将该函数与Font对象的TextColor属性一起使用:

Use the function for example with the TextColor property of a Font object:

Sub FontColorTest()
  Dim col As LongLong
  col = rgb(200, 100, 50)
  GetColor col
  Dim p As Word.Paragraph
  Set p = ActiveDocument.Paragraphs(1)
  p.Range.Font.TextColor.rgb = CLng(col)
End Sub

请注意,GetColor函数要求的参数类型为LongLong,而TextColor.rgb属性的类型为Long.

Please note, that the GetColor function requires a parameter of type LongLong whereas the TextColor.rgb property is of type Long.

这篇关于MS Word VBA:我需要一个调色板对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆