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
Related
I have the code below which I found online to copy paste a tble from excel to powerpoint. After pasting the table into the slide, it fails on PPSlide.Shapes(1).Select with a Run-time error '-2147188160 (80048240)': Shape.Select : Invalide request. To select a shape, its view must be active.
I've been searching and trying different things but can't seem to figure it out.. I thought after the paste that the table would be active and the code would jsut continue but it doesn't unless I activate/select the table in the slide and then click Run. Any help is appreciated. Thanks.
Dim pp As Object
Dim PPPres As Object
Dim PPSlide As Object
Dim Rng As Range
DestinationPPT = "C:\Users\username\Desktop\Data_Display.pptx"
Set pp = CreateObject("PowerPoint.Application")
Set PPPres = pp.Presentations.Open(DestinationPPT)
pp.Visible = True
Set Rng = ActiveSheet.Range("CA1:CJ" & Count + 1)
Rng.Copy
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount, 12)
pp.ActivePresentation.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
PPSlide.Shapes(1).Select
pp.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, True
pp.ActiveWindow.Selection.ShapeRange.Top = 65
pp.ActiveWindow.Selection.ShapeRange.Left = 7.2
pp.ActiveWindow.Selection.ShapeRange.Width = 700
Set PPSlide = Nothing
Set PPPres = Nothing
Set pp = Nothing
Try the code below:
Set pp = CreateObject("PowerPoint.Application")
Set ppPres = pp.Presentations.Open(DestinationPPT)
pp.Visible = True
' first set the Slide and select it
SlideCount = ppPres.Slides.Count
Set ppSlide = ppPres.Slides.Add(SlideCount, 12)
ppSlide.Select
' have the Copy part right beofre youe Paste it to PowerPoint
Set Rng = ActiveSheet.Range("CA1:CJ" & Count + 1)
Rng.Copy
pp.ActivePresentation.Application.CommandBars.ExecuteMso ("PasteSourceFormatting")
Dim myShape As Object
' set up the Pasted shape to an object
Set myShape = ppSlide.Shapes(ppSlide.Shapes.Count)
With myShape
' set-up the shape properties
End With
So I am trying to paste a column into powerpoint slide notes but it only grabs one cell and pastes it into the first slide and will not go to the next slide and paste the next cell into the notes of the 2nd slide.
Sub Notes()
Dim PPTApp As PowerPoint.Application
Dim PPTPres As PowerPoint.Presentation
Dim PPTSlide As PowerPoint.Slide
Dim PPTShape As PowerPoint.Shape
Dim strNotes As String
' Amended Dim Sh As Shape to...
Dim Sh As PowerPoint.Shape
'launch powerpoint application
Set PPTApp = New PowerPoint.Application
PPTApp.Activate
'open powerpoint presentation for macmahon off the intranet
Set PPTPres = PPTApp.Presentations.Open("C:\Users)
Sheets("Raw Data").Select
Range("M2:M26").Select
Set PPTSlide = PPTPres.Slides(1)
On Error GoTo errHandler
Do While ActiveCell.Value <> ""
ActiveCell.Copy
With PPTSlide
If PPTSlide.NotesPage.Shapes.Count = 0 Then 'If no shapes to take Notes then add a shape first
PPTSlide.NotesPage.Shapes.AddShape msoShapeRectangle, 0, 0, 0, 0
Sh = PPTSlide.NotesPage.Shapes(1)
'Code change here - did not recognize Sh.TextFrame.TextRange.Text.Paste
'So, I set the object text to value of the active cell and seemed to do the trick
Sh.TextFrame.TextRange.Text = ActiveCell.Value
Else 'has shapes, so see if they take text
For Each Sh In PPTSlide.NotesPage.Shapes
If Sh.HasTextFrame Then
'Code change here - did not recognize Sh.TextFrame.TextRange.Text.Paste
'So, I set the object text to value of the active cell and seemed to do the trick
Sh.TextFrame.TextRange.Text = ActiveCell.Value
End If
Next Sh
End If
End With
Set PPTSlide = PPTPres.Slides.Add(PPTPres.Slides.Count + 1, ppLayoutText)
ActiveCell.Offset(1, 0).Select
Loop
Exit Sub
errHandler:
MsgBox Err.Number & vbTab & Err.Description, vbCritical, "Error"
End Sub
You are setting a fixed reference to slide 1 in this line:
Set PPTSlide = PPTPres.Slides(1)
Instead of that, wrap the code to copy and paste the cell content in a For...Next loop which loops through your desired slides. For example, to loop through all slides in the presentation:
For Each PPTSlide In PPTPres.Slides
With PPTSlide
' Do the things you need to do on this slide
End With
Next
Or manage a pre-defined range of slides:
Dim lSlideIndex As Long
For lSlideIndex = 2 to 5 ' Process slides 2 to 5
With PPTPres.Slides(lSlideIndex)
' Do the things you need to do on this slide
End With
Next
I'm trying to make a PP slideshow from an Excel spreadsheet. I have this VBA module:
Sub CreatePowerPointQuestions()
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim Question As String
Dim Options As String
Dim Answer As String
Dim limit As Integer
limit = 3
On Error Resume Next
Set newPowerPoint = GetObject(, "PowerPoint.Application")
On Error GoTo 0
If newPowerPoint Is Nothing Then
Set newPowerPoint = New PowerPoint.Application
End If
'Make a presentation in PowerPoint
If newPowerPoint.Presentations.Count = 0 Then
newPowerPoint.Presentations.Add
End If
'Show the PowerPoint
newPowerPoint.Visible = True
'Select worksheet and cells activate
Worksheets("Sheet1").Activate
'Loop through each question
For i = 1 To limit
'Add a new slide where we will paste the Question and Options:
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, 3
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
'Set the variables to the cells
Question = ActiveSheet.Cells(i, 1).Value
Options = ActiveSheet.Cells(i, 2).Value
Answer = ActiveSheet.Cells(i, 3).Value
activeSlide.Shapes(1).TextFrame.TextRange.Text = Question
activeSlide.Shapes(2).TextFrame.TextRange.Text = Options
activeSlide.Shapes(3).TextFrame.TextRange.Text = Answer
Next
Set activeSlide = Nothing
Set newPowerPoint = Nothing
End Sub
The problem is that it creates slides with a title and two entries side by side with a bullet. I want the slide to be three lines, without the bullets. Is there a way to do this? I don't know how to do the layouts
There is a list of layouts that you can use at https://msdn.microsoft.com/en-us/library/office/ff745137.aspx
However, only two of them match your criteria
ppLayoutObjectOverText, and
ppLayoutTextOverObject
Try this after your Set activeslide line:
activeSlide.layout = ppLayoutObjectOverText
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'm an amateur coder. I'm trying to put something together to transfer all charts in an excel file to different slides on a powerpoint. I've tested several modules online (some from here as well). I've found this one below to be the most comprehensive for me so far. I have 3 graphs on a worksheet and for some reason I can't figure out, the code only copies one graph (first created), makes new slide and sticks it on that second slide. No idea what's going on, any help would be appreciated:
Sub ChartsAndTitlesToPresentation()
' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim PresentationFileName As Variant
Dim SlideCount As Long
Dim iCht As Integer
Dim sTitle As String
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
For iCht = 1 To ActiveSheet.ChartObjects.Count
With ActiveSheet.ChartObjects(iCht).Chart
' get chart title
If .HasTitle Then
sTitle = .ChartTitle.Text
Else
sTitle = ""
End If
' remove title (or it will be redundant)
.HasTitle = False
' copy chart as a picture
.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
' restore title
If Len(sTitle) > 0 Then
.HasTitle = True
.ChartTitle.Text = sTitle
End If
End With
' Add a new slide and paste in the chart
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutTitleOnly)
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
With PPSlide
' paste and select the chart picture
.Shapes.Paste.Select
' align the chart
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
.Shapes.Placeholders(1).TextFrame.TextRange.Text = sTitle
End With
Next
' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Sub