vba 改变形状内的字体ppt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26103088/
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
change font within a shape ppt
提问by user2533406
I'm automatically generating a powerpoint slide through VBA, User Forms, and Excel. You run the VBA script in excel, fill out the data, the data goes into cells in excel, then the VBA script pulls the data and puts it into textbox shapes in the slide.
我正在通过 VBA、用户表单和 Excel 自动生成幻灯片。您在 excel 中运行 VBA 脚本,填写数据,数据进入 excel 中的单元格,然后 VBA 脚本提取数据并将其放入幻灯片中的文本框形状中。
My problem is I want to use different font sizes at different times, so for example 28 pt font for one part and 14 pt for the rest. The problem is that any property changes I make to the textbox applies to all of the text within the shape.
我的问题是我想在不同的时间使用不同的字体大小,例如,一部分使用 28 pt 字体,其余部分使用 14 pt。问题是我对文本框所做的任何属性更改都适用于形状内的所有文本。
My current workaround is sloppy and is to just generate another textbox over the original and insert spacing in the original so it looks like the larger text is "in" the textbox while it's actually just sitting over a few empty lines set aside.
我目前的解决方法很草率,只是在原始文本上生成另一个文本框并在原始文本中插入间距,因此看起来较大的文本在文本框中“在”文本框中,而实际上它只是坐在旁边的几行空行上。
采纳答案by David Zemens
You can format specific substrings within a string, but it's very cumbersome, for example assuming shp
is an object variable representing your textbox:
您可以格式化字符串中的特定子字符串,但它非常麻烦,例如假设shp
是一个表示您的文本框的对象变量:
Sub foo()
Dim shp As Shape
Set shp = ActivePresentation.Slides(1).Shapes("TextBox 3")
shp.TextFrame.TextRange.Text = "Hello, world!"
shp.TextFrame.TextRange.Characters.Font.Size = 14 'applies uniform font to entire shape
shp.TextFrame.TextRange.Characters(1, 5).Characters.Font.Size = 28
End Sub
Example output:
示例输出:
The difficulty of course is working with mixed formats, and I do not think there is any easysolution. It will be up to you to determine what formats you need to "capture", and what subsequently implement the appropriate conditional logic to transfer those formats to the PowerPoint shapes.
难点当然是使用混合格式,我认为没有任何简单的解决方案。由您决定需要“捕获”哪些格式,以及随后实施适当的条件逻辑以将这些格式传输到 PowerPoint 形状的内容。
One possible alternative -- and this is the route that I would go if I were you -- would be to copy the cellfrom Excel, and use this method to paste in to PowerPoint. I believe this will create a tableconsisting of a single cell, in the PowerPoint slide. You will have to make adjustments for size/position, but this should be an order of magnitude easier than trying to capture every possible variation of font formatting:
一种可能的选择——如果我是你,这就是我会走的路线——是从 Excel复制单元格,然后使用这种方法粘贴到 PowerPoint 中。我相信这将在 PowerPoint 幻灯片中创建一个由单个单元格组成的表格。您必须对大小/位置进行调整,但这应该比尝试捕获字体格式的所有可能变化要容易一个数量级:
Sub foo2()
Dim shp As Shape
Dim xl As Object
'Get Excel and copy a specific cell
Set xl = GetObject(, "Excel.Application")
xl.Workbooks("Book35").Sheets("Sheet2").Range("B4").Copy
'Paste that cell in to PowerPoint as a table, preserving formats:
ActivePresentation.Slides(1).Select
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End Sub
Example output, as copied from the Excel cell:
从 Excel 单元格复制的示例输出:
回答by sam
No need to change the font in excel to reflect in Word. You can do it directly. Just paste the below mentinoed line in Word VBA : - Activedocument.Shapes("sam").TextFrame.TextRange.Words(1).Font.Size = 28
无需更改 excel 中的字体以反映在 Word 中。你可以直接做。只需在 Word VBA 中粘贴以下 mentinoed 行: - Activedocument.Shapes("sam").TextFrame.TextRange.Words(1).Font.Size = 28