I have an access database which produces a multisheet excel workbook with many tables and charts ( > 20). I need to copy each of these into a powerpoint presentation.
I have created a code for one of the tables (see foot of post). But if I have to repeat this for each of the 20, that is a lot of code. I would ideally like to make a function/sub that can be called multiple times from a single line. But my attempt to create a function just to do a small part of what is required (add a slide) is not succeeding. Can anyone advise a solution.
My Function:
The use of my function in the main code:
This returns error 91 "Object Variable or with block variable not set"
The code below is what I am trying to avoid recreating 20+ times in the main routine:
I have created a code for one of the tables (see foot of post). But if I have to repeat this for each of the 20, that is a lot of code. I would ideally like to make a function/sub that can be called multiple times from a single line. But my attempt to create a function just to do a small part of what is required (add a slide) is not succeeding. Can anyone advise a solution.
My Function:
Code:
Public Function CreatePowerpointSlide(myPresentation As Object, strSlideName As String) As Object
Dim intSlideCount, x As Integer
Dim mySlide As Object
'Set myPresentation = PowerPointApp.Presentations.Open(FileName:="R:\Project Management Central Function\Databases\Budgets\wip\test.pptx")
intSlideCount = myPresentation.Slides.Count
Set mySlide = myPresentation.Slides.Add((intSlideCount + 1), ppLayoutBlank) '11 = ppLayoutTitleOnly
'make sure no two slides with same name exist
For x = intSlideCount To 1 Step -1
If myPresentation.Slides(x).Name = strSlideName Then
myPresentation.Slides(x).Delete
End If
Next x
mySlide.Name = "Monthly Summary"
CreatePowerpointSlide = mySlide
End Function
The use of my function in the main code:
Code:
Set mySlide = CreatePowerpointSlide(myPresentation, "Monthly Summary")
This returns error 91 "Object Variable or with block variable not set"
The code below is what I am trying to avoid recreating 20+ times in the main routine:
Code:
Set rngToCopy = wsht5.Range("A3:O35")
dblRngHeight = rngToCopy.Height
dblRngWidth = rngToCopy.Width
If PowerPointApp Is Nothing Then
Set PowerPointApp = CreateObject(Class:="PowerPoint.Application")
End If
Set myPresentation = PowerPointApp.Presentations.Open(FileName:="R:\Project Management Central Function\Databases\Budgets\wip\test.pptx")
intSlideCount = myPresentation.Slides.Count
Set mySlide = myPresentation.Slides.Add((intSlideCount + 1), ppLayoutBlank) '11 = ppLayoutTitleOnly
'make sure no two slides with same name exist
For x = intSlideCount To 1 Step -1
If myPresentation.Slides(x).Name = "Monthly Summary" Then
myPresentation.Slides(x).Delete
End If
Next x
mySlide.Name = "Monthly Summary"
'Set tbxTitle = mySlide.Shapes.AddShape(Type:=msoshaperectangle, Left:=10, Top:=10, Width:=800, Height:=25)
'tbxTitle.Name = "Slide title"
Set tbxTitle = mySlide.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=30, Top:=10, Width:=800, Height:=25).TextFrame.TextRange
tbxTitle = "Monthly Summary"
'tbxTitle.ShapeRange.Name = "Title"
'cant get naming of a text box to work at all
tbxTitle.Font.Name = "Arial"
tbxTitle.Font.Size = 20
tbxTitle.Font.Color.RGB = RGB(0, 0, 0)
tbxTitle.ParagraphFormat.Alignment = ppAlignCenter
Set tbxLastUpdate = mySlide.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=30, Top:=500, Width:=200, Height:=15).TextFrame.TextRange
tbxLastUpdate = "Last Updated: " & FormatDateEastern([Forms]![frmSwitchboard]![MaxOfLastUpdated])
tbxLastUpdate.Font.Name = "Arial"
tbxLastUpdate.Font.Size = 12
tbxLastUpdate.Font.Color.RGB = RGB(0, 0, 0)
tbxLastUpdate.ParagraphFormat.Alignment = ppAlignLeft
'rngToCopy.Copy
rngToCopy.CopyPicture xlScreen, xlBitmap
Set myShapeRange = mySlide.Shapes.PasteSpecial(DataType:=1) 'bitmap
dblShapeHeight = myShapeRange.Height 'this is for info only
dblShapeWidth = myShapeRange.Width 'this is for info only
dblAspectRatio = dblRngWidth / dblRngHeight
With myShapeRange
.Left = 100
.Top = 50
.Width = 700
.Height = .Width / dblAspectRatio
End With
PowerPointApp.Visible = True
PowerPointApp.Activate
myPresentation.Save
strValidatedPath = strPath
strValidatedFilename = "ManagementReport-" & strType & "-" & FormatDateEastern([Forms]![frmSwitchboard]![MaxOfLastUpdated]) & ".pptx"
If FolderExists(strValidatedPath) Then
If FileExists(strValidatedPath & "\" & strValidatedFilename) Then
MsgBox "File already exists and will be overwritten"
End If
myPresentation.SaveAs strValidatedPath & "\" & strValidatedFilename 'This will overwite what is there without warning as it stands
MsgBox "Management Powerpoint report created, filename: " & strValidatedFilename & ". Stored in folder: " & strValidatedPath
Else
MsgBox "Folder provided does not exist"
End If