首页 文章

VBA Powerpoint文本框对齐

提问于
浏览
4

我正在使用Powerpoint 2007,我想编写一个宏,它在幻灯片中创建一个文本框 .

但是,默认情况下,文本框中的文本与中心对齐 . 我想把它对齐,但我不知道该怎么做 . 如何更改此文本框的对齐?

这是我的代码 .

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

2 回答

  • 3

    首先,选择代码中的任何内容或依赖当前选择通常不是好的做法,只因为它可以减慢你的代码数量级 .

    相反,这样的事情:

    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
    
  • 5

    我相信你的问题的答案是 Shape.TextFrame.TextRange 对象属性

    oPPShape.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignLeft
    

    只是对你和Steve 's post. If you'的评论真正使用这个代码和对象进行后期绑定,你还需要记住从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.

相关问题