having problems getting excel vba to save a powerpoint

sumhungl0

Board Regular
Joined
Jan 1, 2014
Messages
119
working with excel to make a powerpoint of a few ranges that i need to send out each week. i usually do this manually and would like to get this process automated. i have it to the point now that it will create the powerpoint and paste the ranges i need. the powerpoint is just the way i need it.
the problem is when i try to save the powerpoint, i get an error. i cant get anything to work to get this saved. anybody have any ideas?
Code:
Sub wpp()

Dim rng As Excel.Range
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim mySlide As PowerPoint.Slide
Dim myShapeRange As PowerPoint.Shape
Dim fileNameString As String
Dim D As Integer
Dim N As Date
Dim iCol As Long, iRow As Long, iRow2 As Long
Dim userResponce As Range
Dim wTitle As String
ThisWorkbook.Sheets("weekly").Select
'Copy Range from Excel
  Set rng = ThisWorkbook.ActiveSheet.Range("a3:c13")
'Create an Instance of PowerPoint
  On Error Resume Next
    
    'Is PowerPoint already opened?
      Set PowerPointApp = GetObject(class:="PowerPoint.Application")
    
    'Clear the error between errors
      Err.Clear
    'If PowerPoint is not already open then open PowerPoint
      If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
    
    'Handle if the PowerPoint Application is not found
      If Err.Number = 429 Then
        MsgBox "PowerPoint could not be found, aborting."
        Exit Sub
      End If
  On Error GoTo 0
  
'Make PowerPoint Visible and Active
  PowerPointApp.Visible = True
  PowerPointApp.Activate
    
'Create a New Presentation
  Set myPresentation = PowerPointApp.Presentations.Add
'Add a slide to the Presentation
  Set mySlide = myPresentation.Slides.Add(1, ppLayoutTitleOnly)
  Const themePath As String = "C:\Program Files\Microsoft Office\Document Themes 14\Apex.thmx"
  mySlide.ApplyTheme "C:\Program Files\Microsoft Office\Document Themes 14\Apex.thmx"
'Find next Monday
  'nmonday = DateAdd("ww", 1, pdat - (Weekday(pdat, vbMonday) + 1))
  D = Weekday(Now)
  N = Date + (9 - D)
  'N = Date + 54
  st = N 'Format(N, "d")
  sp = st + 6
  mo = Format(N, "mmmm")
  yr = Year(N)
  nextmonday = Format(N, "d-mmm-yy")
'MsgBox Format(st, "mmmm")
If Format(st, "mmmm") = Format(sp, "mmmm") Then
 wTitle = "Weekly Schedule" & vbCr & Format(st, "d") & " - " & Format(sp, "d") & " " & mo & " " & yr
Else
 wTitle = "Weekly Schedule" & vbCr & Format(st, "d mmm yy") & " - " & Format(sp + 6, "d mmm yy")
End If
mySlide.Shapes.Title.TextFrame.TextRange.Text = wTitle
'Copy Excel Range
  rng.Copy
'Paste to PowerPoint and position
  mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
  Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
  
    'Set position:
      myShapeRange.Left = 8
      myShapeRange.Top = 120
      myShapeRange.Height = 416
      'myShapeRange.Width = 120
      
For iCol = 3 To 90000
    If Cells(3, iCol) = N Then Exit For
  Next iCol
  ul = Cells(3, iCol).Address(rowabsolute:=False, columnabsolute:=False) ' set upper left of range
  lr = Cells(3, iCol).Offset(10, 6).Address(rowabsolute:=False, columnabsolute:=False) ' set lower right of range
Set rng = ThisWorkbook.ActiveSheet.Range(ul & ":" & lr)
  rng.Copy
  mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
  Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
  With myShapeRange
      .LockAspectRatio = 0  'msoFalse
      '.ScaleHeight(45, msoFalse, msoScaleFromTopLeft)
      '.ScaleHeight 45, msoFalse, msoScaleFromTopLeft
      .Left = 128.5
      .Top = 120
      .Height = 416
      .Width = 583
    End With
ThisWorkbook.Sheets("schedule").Select
  For iRow = 3 To 90000
    If Cells(iRow, 1) = N Then Exit For
  Next iRow
  For iRow2 = 3 To 90000
    If Cells(iRow2, 1) = N + 7 Then Exit For
  Next iRow2
  ul2 = Cells(iRow, 1).Offset(-1, 0).Address(rowabsolute:=False, columnabsolute:=False) ' set upper left of range
  lr2 = Cells(iRow2, 12).Offset(-3, 0).Address(rowabsolute:=False, columnabsolute:=False) ' set lower right of range
Set rng = ThisWorkbook.ActiveSheet.Range(ul2 & ":" & lr2)
  rng.Copy

'Add a slide to the Presentation
  Set mySlide = myPresentation.Slides.Add(2, ppLayoutCustom)
  mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
  Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)
  
    With myShapeRange
      .LockAspectRatio = 0  'msoFalse
      .Left = 0
      .Top = 0
      .Height = 539
      .Width = 722
    End With
'Clear The Clipboard
  Application.CutCopyMode = False
  'MsgBox "Edit PowerPoint?"
  '------------------------------------------- NONE OF THE SAVING BELOW WORKS, AS YOU CAN SEE IVE TRIED SEVERAL THINGS
  'FileN = "EMAILED WEEKLY " & Format(st, "d") & " - " & Format(sp, "d") & " " & mo & " " & yr
  fileN = "EMAILED WEEKLY " & wTitle
  'myPresentation.SaveAs Filename:="[URL="file://\\SharedDrive\PowerPoints\"]\\SharedDrive\PowerPoints\[/URL]" & FileN & ".pptx"
  fileNameString = "[URL="file://\\SharedDrive\PowerPoints\"]\\SharedDrive\PowerPoints\[/URL]" & fileN ' & ".pptx"
'With myPresentation ' ActivePresentation
'    .SaveAs Filename:="\\SharedDrive\PowerPoints\" & FileN & ".pptx", _
'        FileFormat:=ppSaveAsOpenXMLPicturePresentation 'ppSaveAsOpenXMLPresentation
'End With
myPresentation.SaveCopyAs fileNameString, ppSaveAsOpenXMLPresentationMacroEnabled ', ppSaveAsOpenXMLPresentation
'ppApp.Quit
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
sorry, forgot to post what the error was.

Run-time error'-2147467259 (80004005)':
Presentation (unknown member) : An error occurred while PowerPoint was saving the file.
 
Upvote 0
i cant figure this thing out. ive tried saving all types of formats for powerpoint, ppSaveAsDefault, ppSaveAsOpenXMLPresentation, and so on. ive tried with and without file extension. ive tried local C: drive and shared drive. anyone?
 
Upvote 0
@sumhungl0, did you ever figure this out? I am having a similar issue and was wondering.

i did, not sure what I did to get it working but here is the working last part that I am currently using.

Code:
fileN = "EMAILED WEEKLY " & wTitleDate
fileNameString = "\\SharedDrive\PowerPoints\" & fileN ' & ".pptx"
myPresentation.SaveAs fileNameString

I also have a reference to "Microsoft PowerPoint 14.0 Object Library".

I think the last line of code is what was causing the error I had. simplifying it just to myPresentation .SaveAs I think is what fixed it. good luck.
 
Upvote 0
Slightly connecting to this topic, maybe useful for others who read this thread: if you want to close a file after using saveAs method, it can also throw errors, because the file saving is still in progress when the closing of the file would happen.
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top