VBA Powerpoint 文本框对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11541475/
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-08 13:34:52  来源:igfitidea点击:

VBA Powerpoint Textbox alignment

vbatextboxpowerpointpowerpoint-vba

提问by user1534778

I am using Powerpoint 2007 and i want to program a macro which makes a textbox in a slide.

我正在使用 Powerpoint 2007,我想编写一个宏,在幻灯片中制作一个文本框。

However the text in the textbox is aligned to center by default. I want to align it left, but I don't know how to do. How can I change alignement of this textbox?

但是,文本框中的文本默认居中对齐。我想把它左对齐,但我不知道该怎么做。如何更改此文本框的对齐方式?

Here is my code.

这是我的代码。

Set objPPT = CreateObject("PowerPoint.Application")
Set SQ = objPPT.Presentation

......

SQ.Slides(i + 6).Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 100, 600, 100).Select
objPPT.ActiveWindow.Selection.TextRange.Text = titre

回答by Steve Rindsberg

First, selecting anything in code or relying on the current selection is generally not good practice if only because it can slow your code down by orders of magnitude.

首先,选择代码中的任何内容或依赖当前选择通常不是好的做法,因为它会使您的代码速度降低几个数量级。

Instead, something like this:

相反,是这样的:

Dim oSh as Object ' if you're using late binding, as Shape if early binding

Set oSh =  SQ.Slides(i + 6).Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 100, 600, 100)
' notice that I've removed the .Select from the end of the line above

With oSh.TextFrame
  .TextRange.Text = "something"
  .TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignLeft
End With

回答by tobias

The answer to your problem I believe is in Shape.TextFrame.TextRangeobject properties

我相信您的问题的答案在于Shape.TextFrame.TextRange对象属性

oPPShape.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignLeft

Just a remark to Your and Steve's post. If you're really using this code and objects for late binding, you also need to remember to define the constants from PowerPoint library like msoTextOrientationHorizontal. You'll quickly find when you remove the PPT reference from your project which constants are left out. Like with Excel, distributing your macro to users with different versions is best done with late binding where Office product references are version independent.

只是对你和史蒂夫的帖子的评论。如果您真的使用此代码和对象进行后期绑定,您还需要记住从 PowerPoint 库中定义常量,如msoTextOrientationHorizontal. 当您从项目中删除 PPT 参考时,您会很快发现哪些常量被遗漏了。与 Excel 一样,将宏分发给不同版本的用户最好使用后期绑定,其中 Office 产品引用与版本无关。

'Bind to an existing or created instance of Microsoft Powerpoint
Dim objPPTApp As Object
On Error Resume Next
Set objPPTApp = GetObject(, "Powerpoint.Application")
If Err.Number <> 0 Then: Err.Clear: Set objPPTApp = CreateObject("Powerpoint.Application")

More of late binding here.

更多后期绑定在这里。