Excel VBA将单元格内容复制到InkEdit文本框中,并保留格式,包括颜色/粗体等 [英] Excel VBA copy Cell contents to a InkEdit text box and keep for formatting including colour/bold etc

查看:803
本文介绍了Excel VBA将单元格内容复制到InkEdit文本框中,并保留格式,包括颜色/粗体等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作表,其中某些单元格的文本为彩色,并以粗体/下划线/斜体显示.

I have a sheet where some cells have multicolored text and is in bold/underline/italic.

我需要能够提取单元格内容并在保持相同格式的表单上显示信息.

I need to be able to pull the cell contents and display the information on a form keeping the same formatting.

我遇到了支持RichText的InkEdit控件,但无法从单元格复制到此框.

I have come across InkEdit control which supports RichText but I am unable to copy from cell to this box.

请帮助

推荐答案

InkEdit控件支持粘贴富文本格式,因此您真正需要做的就是复制Range,然后将其粘贴到控件中.由于该控件公开了.hWnd,因此您所需要做的就是使用SendMessage API函数发送WM_PASTE消息:

The InkEdit control supports pasting of rich text, so all you really need to do is copy the Range, then paste it into the control. Since the control exposes it's .hWnd, all you need to do is use the SendMessage API function to send a WM_PASTE message:

'UserForm1
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias _
            "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, _
            ByVal wParam As Long, lParam As Any) As Long

Private Const WM_PASTE = &H302

Private Sub UserForm_Initialize()
    RangeToInkEdit ActiveSheet.Cells(1, 1), InkEdit1
    Application.CutCopyMode = False
End Sub

Sub RangeToInkEdit(source As Range, target As InkEdit)
    source.Copy
    SendMessage InkEdit1.hwnd, WM_PASTE, 0&, 0&
End Sub

Private Sub CommandButton1_Click()
    Unload Me
End Sub

请注意,此有一个与@JohnColeman的方法类似的小问题-在拾取颜色方面做得并不出色.这似乎是Excel在发送到剪贴板的RTF编码中存在的问题,而不是InkEdit控件本身存在的问题(您可以通过复制并粘贴到WordPad(基本上是RTF编辑器)中来确认这一点).某些颜色可以工作,而另一些则不行-所有这些颜色的颜色深度都会减少到基本上是RTF支持的最接近的颜色.

Note that this also has a slight issue similar to @JohnColeman's method - it doesn't do that great of a job at picking up the colors. This appears to be a problem that Excel has in the encoding of RTF that it sends to the clipboard rather than an issue with the InkEdit control itself (you can confirm that by copying and pasting into WordPad, which is basically an RTF editor). Some colors work, others don't - all of them will be reduced in color depth to basically the closest color that RTF supports.

这篇关于Excel VBA将单元格内容复制到InkEdit文本框中,并保留格式,包括颜色/粗体等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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