3

I have this code snippet which works fine, except for the last line when I try to align the text to the center. msoAlignRight was just for testing purposes to see if it moves to the right..but nothing happens. - edit: I have incorporated this from Qlikview to PPT macro, shouldn't matter though.

NOTE: I WOULD LIKE leText 0 to be centered text in the middle. Now it's to the left.

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
Probs
  • 343
  • 2
  • 6
  • 20

3 Answers3

1

Since the CopyTextToClipboard method is a QV API I'm not sure if the shape is being copied or the text within the shape (or TextRange). Try this: once the macro has created the shape leText0, select it in PowerPoint, set the justification to left and enter this command in the Immediate window: ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.ParagraphFormat.Alignment=ppAlignCenter

Note that ppAlignCenter = 2

What happens?

If the API is copying just the text then I would have expected you would need to create the shape in PowerPoint first and then copy the text from the clipboard into the TextRange of the shape. To test this, replace these lines:

'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

...with these:

'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
Jamie Garroch - MVP
  • 2,839
  • 2
  • 16
  • 24
  • Hi, ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.ParagraphFormat.Alignment=ppAlignCenter works, however..it seems like Qlikview doesn't recognize the With function when I replace your code into mine. How can I run the ppAlignCenter without Qlikview? That it just runs for all Powerpoints? – Probs Sep 19 '16 at 13:05
  • Not sure I understand this QV thing now! Are you not editing VBA within the Microsoft VBE? – Jamie Garroch - MVP Sep 20 '16 at 13:18
  • Oh no actually in Qlikview. However, I also tried to run that particular piece of code for all open powerpoint files but didn't succeed. It can only do it for the selected powerpoint that is open already - just one. – Probs Sep 20 '16 at 13:23
0

Change the "Align right" line to :

leText.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignRight

Another possible improvement to your piece of code, will be using With' like:

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 Rado
  • 33,032
  • 6
  • 29
  • 51
0

What variable type have you declared leText as? It should be Shappe as you're processing a single object but the paste method will return an object of type ShapeRange so you could get the single Shape using this line:

Set leText = PPSlide.Shapes.Paste(1)

Also, if this code is running in Excel and you're using early binding, I assume you've set a reference to the PowerPoint library so that the ppAlignRight value is known, if using late binding you'll need to define it yourself.

Finally, for MSO 2007 and above I recommend using the newer TextFrame2 (and TextRange2) objects as they have more properties available from the updated graphics engine.

Jamie Garroch - MVP
  • 2,839
  • 2
  • 16
  • 24