vba 在VBA中更改文本框中部分文本的字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7979921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:23:05  来源:igfitidea点击:

Change the font of part of the text in a textbox in VBA

vbatextboxpowerpoint

提问by user1025536

Ok, this hasn't been asked yet.

好的,这还没有被问到。

So, I recently started exploring VBA in PowerPoint. I started to make something that look like a text editor in a slideshow. But, the problem is that the buttons that I made that supposed to make the selected text become bold, italic or underline, changes the whole text inside the textbox into bold, italic or underlined texts.

所以,我最近开始在 PowerPoint 中探索 VBA。我开始制作一些看起来像幻灯片中的文本编辑器的东西。但是,问题是我制作的按钮应该使所选文本变为粗体、斜体或下划线,将文本框内的整个文本更改为粗体、斜体或带下划线的文本。

The current method that I used to change the text font is this:

我用来更改文本字体的当前方法是这样的:

Private Sub CommandButton1_Click()
    If TextBox1.Text.Font.Bold = False Then
        TextBox1.Text.Font.Bold = True
    Else
        TextBox1.Text.Font.Bold = False
    End If
End Sub

Obviously, this code will make all the text in TextBox1 change into bold text when CommandButton1 is clicked. But, what should I do if I want only part of the text change into bold text (that is, the selected text)?

显然,当单击 CommandButton1 时,此代码将使 TextBox1 中的所有文本变为粗体文本。但是,如果我只想将部分文本更改为粗体文本(即所选文本),该怎么办?

回答by Steve Rindsberg

As far as I know, you can't control the formatting of a text box's text, other than as a whole.

据我所知,除了作为一个整体之外,您无法控制文本框文本的格式。

In VB, I think you could use a Rich Text Box control to do the job, but PPT/VBA doesn't supply one.

在 VB 中,我认为您可以使用富文本框控件来完成这项工作,但 PPT/VBA 不提供。

回答by Luke

I know that this works with dealing with VBA and powerpoint objects:

我知道这适用于处理 VBA 和 powerpoint 对象:

...Shape.TextFrame.TextRange.Characters(10, 15).Font.Color.RGB = RGB(, , )

...Shape.TextFrame.TextRange.Characters(10, 15).Font.Color.RGB = RGB(, , )

...Shape.TextFrame.TextRange.Characters(10, 15).Font.Italic = msoTrue

...Shape.TextFrame.TextRange.Characters(10, 15).Font.Italic = msoTrue

I haven't tried in with excel text boxes.

我还没有尝试过使用 excel 文本框。