Currently taking data from an Excel table and inputting it into Powerpoint slides. How could I resize the text below from the table? Powerpoint currently autosizes it to font size 18.
Set ppSlide = ppPres.Slides.Add(1, ppLayoutBlank)
ppSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 150).TextFrame.TextRange.Characters = "TEST " & Cells(Row, col + 1)
Thank you for any and all help!
You can use '~TextRange.Charaters.' But it is usually used to change some part of a text.
~ TextRange.Characters(1, 2).Font.Bold = True ' sets the font of first 2 letters to bold style
Instead, '~.TextRange' or '~.TextRange.Text' would be just good enough.
And the size of a text can be set with '~ TextRange.Font.Size = xx'
After applying Mathieu Guindon's advice, your code would look like this:
Sub test()
Dim Sht As Worksheet
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppShape As PowerPoint.Shape
Set Sht = ThisWorkbook.Worksheets("Sheet1")
Set ppPres = ActivePresentation
Set ppSlide = ppPres.Slides.Add(1, ppLayoutBlank)
Set ppShape = ppSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 150)
With ppShape
.Name = "MyShape 1"
With .TextFrame.TextRange
.Text = "Test" & Sht.Cells(xRow, xColumn + 1) ' "Excel Cell Value"
.Font.Size = 15
.Font.Name = "Arial"
.Font.Bold = True
.Font.Color.RGB = RGB(0, 125, 255)
'change first 2 letters to red color
.Characters(1, 2).Font.Color.RGB = rgbRed
End With
End With
End Sub
Please give a name to Powerpoint shape object, so that you can control the object later by using its name like '~.Shapes("Given name").Textframe.TextRange.~ = ~ '
Related
I wrote the below macro in VBA to copy data from Excel and then paste it into the last slide of a table in my powerpoint deck. The challenge that I'm running into is that I'm getting an error on when I am trying to change the text color from the default white to black.
shpTable.TextFrame.TextRange.Font.Color = RGB(0, 0, 0)
Any ideas what I'm doing wrong on this line? All else works well.
Option Explicit
Dim myFile, Fileselected As String, Path As String, objPPT As Object
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim shpTable As PowerPoint.Shape
Dim activeSlide As PowerPoint.Slide
Sub Button1_Click()
Application.ScreenUpdating = False
Set myFile = Application.FileDialog(msoFileDialogOpen)
With myFile
.Title = "Choose Template PPT File."
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
Fileselected = .SelectedItems(1)
End With
Path = Fileselected
Dim i As Integer
Set ppApp = New PowerPoint.Application
i = 1
ppApp.Presentations.Open Filename:=Path ' 'PowerPointFile = "C:\Test.pptx"
Set ppPres = ppApp.Presentations.Item(i)
' for debug
Debug.Print ppPres.Name
'ppPres.Slides(1).Copy
'ppPres.ActivePresentation.Slides.Add Index:=ppPres.Slides.Count + 1 'changed paste to add
'Set NewSlide = PowPntApp.ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle)
Set activeSlide = ppApp.ActivePresentation.Slides.Add(Index:=(ppPres.Slides.Count + 1), Layout:=ppLayoutTitle)
Set activeSlide = ppPres.Slides(ppPres.Slides.Count)
activeSlide.Select
Set shpTable = activeSlide.Shapes.AddTable(3, 3)
shpTable.Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = ThisWorkbook.Sheets("Macro").Range("F5")
shpTable.Table.Cell(1, 2).Shape.TextFrame.TextRange.Text = ThisWorkbook.Sheets("Macro").Range("F6")
shpTable.Select
shpTable.Fill.ForeColor.RGB = RGB(211, 211, 211)
shpTable.TextFrame.TextRange.Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
Set ppPres = Nothing
Set ppApp = Nothing
End Sub
Notice how you are updating the RGB property of shpTable.Fill.ForeColor? You'll need to target the .RGB property on the shpTable.TextFrame.TextRange.Font.Color property like so:
shpTable.Fill.ForeColor.RGB = RGB(211, 211, 211)
shpTable.TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 0)
That should fix your problem!
I'm trying to copy formatted text content from Excel to Powerpoint in VBA--preferably without copy-and-paste, as it just crashes every time I run it (even with multiple DoEvents to slow it down... there are hundreds of cells of heavily formatted text).
That's why I've been trying to get it to work by addressing the cells directly like in the code below.
For i = 1 To WS.Range("A65536").End(xlUp).Row
If WS.Cells(i, 1) > 0 Then
Set newSlide = ActivePresentation.Slides(1).Duplicate
newSlide.MoveTo (ActivePresentation.Slides.Count)
With newSlide.Shapes(1).TextFrame.TextRange
.Text = WS.Cells(i, 1).Value ' Inserts the (non-formatted) text from Excel. Have also tried WS.Cells(i, 1).Text
.Font.Name = WS.Cells(i, 1).Font.Name ' This works fine
.Font.Size = WS.Cells(i, 1).Font.Size ' This works fine too
' Neither of the below work because there is a mixture of font styled and colours within individual cells
.Font.FontStyle = WS.Cells(i, 1).Font.FontStyle ' Font Style (Regular, Bold, Italic, Bold Italic)
.Font.Color = WS.Cells(i, 1).Font.Color ' Font Color
End With
End If
Next
It works (very quickly) transferring the cell content, font name, and font size... but NOT for FontStyle (bold, italics, etc.) or FontColor because there is more than one style/color in individual cells.
Is there any way around this? I haven't the foggiest idea what the potential solution (if any) could be, so don't even know where to start looking. Even a push in the right direction would help enormously.
here is a proof-of-concept
copying cells from excel into powerPoint
specifics: cells have multiple text formatting per cell
achieved by copying into msWord document and then from msWord into powerPoint
Sub copyMultipleColorTextPerCell()
' this program copies excel cells that contain multiply formatted text in each cell
' the text is copiend into an msWord document, because the formatting is retained
' and then copied into powerpoint
' -------------------------- create powerpoint presentation
Const ppLayoutBlank = 12
Dim ppApp As PowerPoint.Application
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
If ppApp Is Nothing Then
Set ppApp = New PowerPoint.Application
End If
ppApp.Visible = True
Dim ppPres As Presentation
Set ppPres = ppApp.Presentations.Add
Dim ppSlid As PowerPoint.Slide
Set ppSlid = ppPres.Slides.Add(1, 1)
ppSlid.Layout = ppLayoutBlank
Dim ppShp As PowerPoint.Shape
Set ppShp = ppPres.Slides(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 500, 200)
Dim ppTxRng As PowerPoint.TextRange
Set ppTxRng = ppShp.TextFrame.TextRange
' ---------------------------------------------------------------
Dim wdApp As Word.Application ' not necessary
Set wdApp = New Word.Application
Dim xlRng As Excel.Range
Set xlRng = Sheets("Sheet1").Range("c6:c7") ' this is the range that gets copied into powerPoint, via msWord
xlRng.Cells(1) = "this is multicolor text" ' some multicolour test text, so you don't have to type any
xlRng.Cells(1).Characters(1, 13).Font.Color = vbGreen
xlRng.Cells(1).Characters(14, 20).Font.Color = vbRed
xlRng.Cells(2) = "this is also multicolor"
xlRng.Cells(2).Characters(1, 12).Font.Color = vbBlue
xlRng.Cells(2).Characters(13, 20).Font.Color = vbMagenta
Dim wdDoc As Word.Document
Set wdDoc = New Word.Document
Dim wdRng As Word.Range
Set wdRng = wdDoc.Range
xlRng.Copy ' copy whole excel range
wdRng.PasteExcelTable False, False, False ' paste to msWord doc, because formatting is kept
Dim wdTb As Table
Set wdTb = wdDoc.Tables(1)
' copy the two cells from msWord table
wdDoc.Range(start:=wdTb.Cell(1, 1).Range.start, End:=wdTb.Cell(2, 1).Range.End).Copy
ppTxRng.Paste ' paste into powerPoint text table
ppTxRng.PasteSpecial ppPasteRTF
Stop ' admire result ...... LOL
wdDoc.Close False
ppPres.Close
ppApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
Set ppSlid = Nothing
Set ppPres = Nothing
Set ppApp = Nothing
End Sub
I'm running vba to copy and paste ranges from Excel to PPT and I just changed the paste type to Bitmap and now I keep getting this error:
- selection.shaperange: invalid request . nothing appropriate is being
selected
on this line of code:
- PPApp.ActiveWindow.Selection.ShapeRange.Left = 0
I have Excel 2010 and need to use late binding as you can see from the code below.I have searched far and wide but can't seem to find a solution. Any help would be greatly appreciated.
'variables for referencing PPT
Dim PPApp As Object
Dim PPPres As Object
Dim PPSlide As Object
' And another variable to hold a shape
Dim PPTShp as Shape
Dim PresentationFileName As Variant
Dim SlideCount As Long
Dim lngSlideHeight As Long
Dim lngSlideWidth As Long
' reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' reference active presentation
' 1 = PPViewSlide
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = 1
' Determine height and width of slide
lngSlideHeight = PPPres.PageSetup.SlideHeight
lngSlideWidth = PPPres.PageSetup.SlideWidth
'select the object / table
Range("H13:K24").Select
'copy the object / table
Selection.CopyPicture appearance:=xlScreen, Format:=xlBitmap
'Selection.Copy
'add a new slide and paste in the table
'12 = ppLayoutBlank
If (SheetMap(h) = "Charts") Then
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, 12)
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
End If
'paste and select the table
'1 = PPpasteBitmap
' It's best NEVER to rely on selection if you can avoid it, so
' instead of this:
PPSlide.Shapes.PasteSpecial DataType:=1
' do this picks up the shape rather than the shaperange:
Set PPTShape = PPSlide.Shapes.PasteSpecial(1)(1)
'Conditional formatting of slide (based on step)
If (SheetMap(h) = "Charts") Then
' You don't need to go to the slide to work with its shapes.
' Use the shape reference created above instead:
' PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
PPTShp.Left = 0
PPTShp.Top = 50
PPTShp.LockAspectRatio = msoFalse
PPTShp.ScaleHeight 0.9322, msoTrue, msoScaleFromTop
PPTShp.ScaleWidth 0.6954, msoTrue, msoScaleFromLeft
PPApp.ActiveWindow.Selection.ShapeRange.IncrementTop 16#
End If
'add titles
'1 = PPAlignLeft
If (SheetMap(h) = "Charts") Then
Set osh = PPSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=8, Top:=15, Width:=lngSlideWidth, Height:=10)
osh.TextFrame.TextRange.Text = TitleMap(h, 1)
osh.TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 255)
osh.TextFrame.TextRange.Font.Size = 12
osh.TextFrame.TextRange.Font.Bold = True
osh.TextFrame.TextRange.Font.Italic = False
osh.TextFrame.TextRange.ParagraphFormat.Alignment = 1
osh.TextFrame.TextRange.ParagraphFormat.Bullet = False
End If
End If
I am trying to copy an Excel chart to a specific placeholder in Powerpoint. I have named the placeholder using the following code
Sub NameShape()
Dim Name$
On Error GoTo AbortNameShape
If ActiveWindow.Selection.ShapeRange.Count = 0 Then
MsgBox "No Shapes Selected"
Exit Sub
End If
Name$ = ActiveWindow.Selection.ShapeRange(1).Name
Name$ = InputBox$("Give this shape a name", "Shape Name", Name$)
If Name$ <> "" Then
ActiveWindow.Selection.ShapeRange(1).Name = Name$
End If
Exit Sub
AbortNameShape:
MsgBox Err.Description
End Sub
In Excel I have come as far as this:
Sub CreateNewReport()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptShape As PowerPoint.Shape
Dim w!, h!, t!, l!
Dim Chart As Chart
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = msoTrue
Set pptPres = pptApp.Presentations.Open("C:\Users\...\Report.pptm")
Set Chart = Worksheets("Analysts").ChartObjects("Chart 2")
Set PPSlide = pptPres.Slides(4)
'PPSlide.Shapes("Analyst.Forecasts").Copy
Set pptShape = pptPres.Slides(4).Shapes(4)
With pptShape
w = .Width
h = .Height
l = .Left
t = .Top
End With
pptShape.Parent.Paste
With Selection
.Width = w
.Height = h
.Left = l
.Top = t
End With
ppt.Shape.Delete
End Sub
Does anyone know how to take it from here? I can't quite figure out how to define the chart I want to copy and how to paste & replace the shep in Powerpoint. Ideally I would like to replace it with the metafile of the chart, but a picture is ok as well.
Thank you very much for your help!
i use this code to make ppt from Excel and paste in placeHolder;
Nr = 2
'Verifique os graficos nos arquivos
For Each Grf In E.ActiveSheet.ChartObjects
Grf.Copy
Sld.Shapes.Placeholders(Nr).Select msoCTrue
P.ActivePresentation.Windows(1).View.PasteSpecial (ppPasteMetafilePicture)
Nr = Nr + 1
Next Grf
End If
I'm trying to create an excel macro that copies charts displayed on an excel sheet, and pastes them (paste special) into a PowerPoint. The problem I'm having is how do I paste each chart on a different slide? I do not know the syntax at all..
This is what I have so far (it works but it only pastes to the first sheet):
Sub graphics3()
Sheets("Chart1").Select
ActiveSheet.ChartObjects("Chart1").Activate
ActiveChart.ChartArea.Copy
Sheets("Graphs").Select
range("A1").Select
ActiveSheet.Paste
With ActiveChart.Parent
.Height = 425 ' resize
.Width = 645 ' resize
.Top = 1 ' reposition
.Left = 1 ' reposition
End With
Dim PPT As Object
Set PPT = CreateObject("PowerPoint.Application")
PPT.Visible = True
PPT.Presentations.Open Filename:="locationwherepptxis"
Set PPApp = GetObject("Powerpoint.Application")
Set PPPres = PPApp.activepresentation
Set PPSlide = PPPres.slides _
(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
' Copy chart as a picture
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, _
Format:=xlPicture
' Paste chart
PPSlide.Shapes.Paste.Select
' Align pasted chart
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Given I dont have your file locations to work with I have attached a routine below that
Created a new instance of PowerPoint (late binding, hence the need to define constants for ppViewSlide etc)
Loops through each chart in a sheet called Chart1 (as per your example)
Adds a new slide
Pastes each chart, then repeats
Did you need to format each chart picture before exporting for size, or can you change your default chart size?
Const ppLayoutBlank = 2
Const ppViewSlide = 1
Sub ExportChartstoPowerPoint()
Dim PPApp As Object
Dim chr
Set PPApp = CreateObject("PowerPoint.Application")
PPApp.Presentations.Add
PPApp.ActiveWindow.ViewType = ppViewSlide
For Each chr In Sheets("Chart1").ChartObjects
PPApp.ActivePresentation.Slides.Add PPApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank
PPApp.ActiveWindow.View.GotoSlide PPApp.ActivePresentation.Slides.Count
chr.Select
ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
PPApp.ActiveWindow.View.Paste
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
Next chr
PPApp.Visible = True
End Sub
Code with function for plotting 6 charts from Excel to PPT
Option Base 1
Public ppApp As PowerPoint.Application
Sub CopyChart()
Dim wb As Workbook, ws As Worksheet
Dim oPPTPres As PowerPoint.Presentation
Dim myPPT As String
myPPT = "C:\LearnPPT\MyPresentation2.pptx"
Set ppApp = CreateObject("PowerPoint.Application")
'Set oPPTPres = ppApp.Presentations("MyPresentation2.pptx")
Set oPPTPres = ppApp.Presentations.Open(Filename:=myPPT)
ppApp.Visible = True
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
i = 1
For Each shp In ws.Shapes
strShapename = "C" & i
ws.Shapes(shp.Name).Name = strShapename
'shpArray.Add (shp)
i = i + 1
Next shp
Call Plot6Chart(oPPTPres, 2, ws.Shapes(1), ws.Shapes(2), ws.Shapes(3), ws.Shapes(4), ws.Shapes(5), ws.Shapes(6))
End Sub
Function Plot6Chart(pPres As Presentation, SlideNo As Long, ParamArray cCharts())
Dim oSh As Shape
Dim pSlide As Slide
Dim lLeft As Long, lTop As Long
Application.CutCopyMode = False
Set pSlide = pPres.Slides(SlideNo)
For i = 0 To UBound(cCharts)
cCharts(i).Copy
ppApp.ActiveWindow.View.GotoSlide SlideNo
pSlide.Shapes.Paste
Application.CutCopyMode = False
If i = 0 Then ' 1st Chart
lTop = 0
lLeft = 0
ElseIf i = 1 Then ' 2ndChart
lLeft = lLeft + 240
ElseIf i = 2 Then ' 3rd Chart
lLeft = lLeft + 240
ElseIf i = 3 Then ' 4th Chart
lTop = lTop + 270
lLeft = 0
ElseIf i = 4 Then ' 5th Chart
lLeft = lLeft + 240
ElseIf i = 5 Then ' 6th Chart
lLeft = lLeft + 240
End If
pSlide.Shapes(cCharts(i).Name).Left = lLeft
pSlide.Shapes(cCharts(i).Name).Top = lTop
Next i
Set oSh = Nothing
Set pSlide = Nothing
Set oPPTPres = Nothing
Set ppApp = Nothing
Set pPres = Nothing
End Function