Error setting GUID reference

A

Amit Shanker

Hi,

I have the following queries with reference to the code modules below :

1) If I first run SetMyRef and then run ExcelChartsToPPT, it works fine. But
if I *call* SetMyRef from the second module the reference to Powerpoint is
not set, and the code aborts. Why is this so ?
2) How should I change my code to late binding for ExcelChartsToPPT ?

Option Explicit

Sub SetMyRef()
Dim R As Variant

For Each R In ActiveWorkbook.VBProject.References
If R.GUID = "{91493440-5A91-11CF-8700-00AA0060263B}" Then
Exit Sub
End If
Next

On Error GoTo NotFound

ActiveWorkbook.VBProject.References.AddFromGuid
"{91493440-5A91-11CF-8700-00AA0060263B}", _
Major:=2, Minor:=7

Exit Sub

NotFound:
MsgBox "CAN'T RUN THIS CODE"

End Sub

Sub ExcelChartsToPPT()

On Error GoTo ErrHandler

Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim SlideCount As Long
Dim iCht As Integer
Dim i As Integer
Dim Ans As String


Ans = MsgBox("Copy all charts in workbook to Powerpoint ?", vbYesNo +
vbQuestion, "Confirm")
If Ans = vbYes Then GoTo Proceed Else Exit Sub

Proceed:
Set PPApp = CreateObject("Powerpoint.Application")
PPApp.Visible = True
PPApp.WindowState = ppWindowMinimized

' Create active presentation
Set PPPres = PPApp.Presentations.Add
PPPres.Slides.Add 1, ppLayoutTitle

For i = 1 To Worksheets.Count 'All worksheets with embedded charts
needing transfer to PP
For iCht = 1 To Worksheets(i).ChartObjects.Count
' copy chart as a picture
Worksheets(i).ChartObjects(iCht).Chart.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
'xlBitmap can also be used

' Add a new slide and paste in the chart
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)
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

End With

Next iCht
Next i

PPPres.Slides(1).Select
PPApp.WindowState = ppWindowMaximized

' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing

Exit Sub

ErrHandler: MsgBox "An error occurred; please re-try !", vbInformation

End Sub


Thanks,
Amit
 
C

Chip Pearson

Amit,

Using VBA code to modify code in the same workbook is
problematic, e.g., when is such code compiled. If you can bind
to the PowerPoint reference at design time, I would use late
binding and bypass all the issues with the PowerPoint reference
completely. Just change any variable declared "As
PowerPoint.Whatever" to "As Object". If you use enum constants
from the PPT library, you'll have to change those to their
literal values.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
A

Amit Shanker

Thanks Chip, I will try and go down the late binding route as per your
suggestion. But as an alternative, does it mean that I can set reference
with my original code if I am using it from another workbook ?
 
C

Chip Pearson

You should be able to do that. There are still issues of when the
revised project gets compiled (before or after the reference has
been added). Adding references on the fly is something I haven't
done much work with, so I'm no expert on the perils of doing so.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
A

Amit Shanker

Chip, another question if I may - is the GUID reference independent of
Office and Windows version ? I have not been able to find a definitive
answer.
 
M

Melanie Breden

Hi Amit,

Amit said:
I have the following queries with reference to the code modules below :

1) If I first run SetMyRef and then run ExcelChartsToPPT, it works fine. But
if I *call* SetMyRef from the second module the reference to Powerpoint is
not set, and the code aborts. Why is this so ?
2) How should I change my code to late binding for ExcelChartsToPPT ?

replace the constants from PowerPoint by its values:

Sub ExcelChartsToPPT()
Dim PPApp As Object
Dim PPPres As Object
Dim PPSlide As Object
Dim iCht As Integer
Dim i As Integer

If MsgBox("Copy all charts in workbook to Powerpoint ?", _
vbYesNo + vbQuestion, "Confirm") = vbNo Then Exit Sub

Set PPApp = CreateObject("Powerpoint.Application")
PPApp.Visible = True
PPApp.WindowState = 2 'ppWindowMinimized

' Create active presentation
Set PPPres = PPApp.Presentations.Add
PPPres.Slides.Add 1, 1 ' ppLayoutTitle

For i = 1 To Worksheets.Count
For iCht = 1 To Worksheets(i).ChartObjects.Count
' copy chart as a picture
Worksheets(i).ChartObjects(iCht).Chart.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
' Add a new slide and paste in the chart
Set PPSlide = PPPres.Slides.Add(PPPres.Slides.Count + 1, 12) 'ppLayoutBlank)
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
' paste and select the chart picture
PPSlide.Shapes.Paste.Select
' align the chart
With PPApp.ActiveWindow.Selection.ShapeRange
.Align msoAlignCenters, True
.Align msoAlignMiddles, True
End With
Next iCht
Next i

PPPres.Slides(1).Select
PPApp.WindowState = 3 'ppWindowMaximized
End Sub

--
Regards
Melanie Breden
- Microsoft MVP für Excel -

http://excel.codebooks.de (Das Excel-VBA Codebook)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top