【问题标题】:Text Alignment in VBA PowerPoint 2013VBA PowerPoint 2013 中的文本对齐
【发布时间】:2017-01-25 09:04:55
【问题描述】:

我有这段代码 sn-p 可以正常工作,除了当我尝试将文本与中心对齐时的最后一行。 msoAlignRight 只是为了测试目的,看看它是否向右移动..但没有任何反应。 - 编辑:我已经将它从 Qlikview 合并到 PPT 宏中,不过应该没关系。

注意:我希望 leText 0 在中间居中。现在它在左边。

Sub ppt

'Set ppt template
filePath_template = "...\Template.pptx"

'Remove filters
ActiveDocument.ClearAll()

'Retrieve all accounts
set field1Values = ActiveDocument.Fields("name").GetPossibleValues 


 ActiveDocument.ActivateSheetByID "ABC01"
for i = 0 to 15
ActiveDocument.Fields("name").Clear
ActiveDocument.GetApplication.WaitForIdle 100
'Set filter on just 1 account
ActiveDocument.Fields("name").Select field1Values.Item(i).Text

ActiveDocument.GetApplication.Sleep 5000

ActiveDocument.GetApplication.WaitForIdle 100
'Create a ppt object
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
'Open the ppt template 
Set objPresentation = objPPT.Presentations.Open(filePath_template)

Set PPSlide = objPresentation.Slides(1)

'leText 2
ActiveDocument.GetSheetObject("TEXT001").CopyTextToClipboard
ActiveDocument.GetApplication.WaitForIdle 100
Set leText2 = PPSlide.Shapes.Paste
leText2.Top = 280
leText2.Left = 310
leText2.Width = 300
leText2.TextFrame.TextRange.Font.Size = 8

ActiveDocument.GetApplication.Sleep 1000

for k = 0 to 10
ActiveDocument.GetApplication.WaitForIdle 100
ActiveDocument.ActiveSheet.CopyBitmapToClipboard
ActiveDocument.GetApplication.WaitForIdle 100
next

ActiveDocument.GetApplication.WaitForIdle 100

'leText 0
ActiveDocument.GetSheetObject("TEXT002").CopyTextToClipboard
ActiveDocument.GetApplication.WaitForIdle 100
Set leText0 = PPSlide.Shapes.Paste
leText0.Top = 1
leText0.Left = 150
leText0.Width = 700
leText0.TextFrame.TextRange.Font.Size = 12
leText0.TextFrame.TextRange.Font.Color = vbWhite

'Save ppt
filePath = "...\SaveFolder\" & field1Values.Item(i).Text & ".pptx"
objPresentation.SaveAs filePath
Next
objPPT.Quit

End Sub

【问题讨论】:

  • 试试我下面的代码,让我知道它是否有效

标签: vba powerpoint powerpoint-2013


【解决方案1】:

将“右对齐”行更改为:

leText.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight

对您的代码的另一个可能的改进是使用With',例如:

With leText
    .Top = 12
    .Left = 250
    .Width = 500
    .TextFrame.TextRange.Font.Size = 14
    .TextFrame.TextRange.Font.Color = vbWhite
    .TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight
End With

【讨论】:

  • 嗨,Shai,它不起作用。我也试过了:(
  • 您是否收到错误消息?您确实注意到您正在将文本更改为 white ,因此很难看到字体。它是一个什么样的物体?文本框 ?表?
  • 它是更大代码的一部分,所以背景很暗。但我把这个对齐放在最后,它什么也没做。如果我把它放在前面,例如 Font.Size,它就停在那里。只是停止处理代码。它只是从 Qlikview 复制文本,所以它是一个常规文本框
  • @Probs 你能上传其余的相关代码吗?是不是太长了?
  • 不,它真的很长而且无关紧要。
【解决方案2】:

您将 leText 声明为什么变量类型?它应该是 Shape,因为您正在处理单个对象,但 paste 方法将返回一个 ShapeRange 类型的对象,因此您可以使用此行获得单个 Shape:

Set leText = PPSlide.Shapes.Paste(1)

另外,如果此代码在 Excel 中运行并且您正在使用早期绑定,我假设您已设置对 PowerPoint 库的引用,以便知道 ppAlignRight 值,如果使用后期绑定,您需要定义自己动手。

最后,对于 MSO 2007 及更高版本,我建议使用较新的 TextFrame2(和 TextRange2)对象,因为它们在更新的图形引擎中具有更多可用属性。

【讨论】:

    【解决方案3】:

    由于 CopyTextToClipboard 方法是一个 QV API,我不确定是复制了形状还是形状中的文本(或 TextRange)。试试这个:一旦宏创建了形状 leText0,在 PowerPoint 中选择它,将对齐设置为左,然后在立即窗口中输入以下命令: ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.ParagraphFormat.Alignment=ppAlignCenter

    注意 ppAlignCenter = 2

    会发生什么?

    如果 API 仅复制文本,那么我预计您需要先在 PowerPoint 中创建形状,然后将文本从剪贴板复制到形状的 TextRange 中。要对此进行测试,请替换以下行:

    'leText 2
    ActiveDocument.GetSheetObject("TEXT001").CopyTextToClipboard
    ActiveDocument.GetApplication.WaitForIdle 100
    Set leText2 = PPSlide.Shapes.Paste
    leText2.Top = 280
    leText2.Left = 310
    leText2.Width = 300
    leText2.TextFrame.TextRange.Font.Size = 8
    

    ...用这些:

    'leText 2
    ActiveDocument.GetSheetObject("TEXT001").CopyTextToClipboard
    ActiveDocument.GetApplication.WaitForIdle 100
    With PPSlide.Shapes.AddShape(msoShapeRectangle, 310, 280, 300, 0)
      With .TextFrame
        .WordWrap = msoFalse
        .AutoSize = ppAutoSizeShapeToFitText
        With .TextRange
          .Paste
          .ParagraphFormat.Alignment = ppAlignCenter
          .Font.Size = 8
        End With
      End With
    End With
    

    【讨论】:

    • 嗨,ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.ParagraphFormat.Alignment=ppAlignCenter 有效,但是..当我将您的代码替换为我的代码时,Qlikview 似乎无法识别 With 函数。如何在没有 Qlikview 的情况下运行 ppAlignCenter?它只适用于所有 Powerpoint?
    • 不确定我现在是否理解这个 QV 的东西!您不是在 Microsoft VBE 中编辑 VBA 吗?
    • 哦,实际上在 Qlikview 中没有。但是,我也尝试为所有打开的 powerpoint 文件运行那段特定的代码,但没有成功。它只能对已打开的选定 powerpoint 执行此操作 - 只有一个。
    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多