How can I change the source of links in PPT programmatically?

S

shysue25

I have a PowerPoint presentation that has charts that are linked to an
Excel file.

I want to be able to change the source of all the links in the
presentation programmatically, using VB.

Can anyone tell me how to go about doing that?
 
S

Shyam Pillai

If you have the existing source code you are working with, I would suggest
you post that. If not you can search google groups for past posting on this
topic. I've posted code snippet for the same here in the past.
 
S

shysue25

Below is my code:

Public Sub ChangeExcelSource(ByVal sSourceOrig1, ByVal sSource1 As
String, ByVal sFileName As String)

Dim objPPT As Object 'PowerPoint.Application
Dim objPresentation As Object 'PowerPoint.Presentation
Dim I As Integer
Dim K As Integer
Dim linkname As String ' Create a variable to store the
worksheet reference string.
Dim linkpos As Integer ' Create a variable to store the
position of the worksheet
' reference in the .SourceFullName
property of the OLEFormat
' object.
'Creates an instance of PowerPoint
On Error Resume Next

Set objPPT = GetObject(, "PowerPoint.Application")

If objPPT Is Nothing Then
'Create a PowerPoint instance
Set objPPT = CreateObject("PowerPoint.Application")

If objPPT Is Nothing Then
MsgBox "PowerPoint is not Installed on your System!", vbCritical
', POWERVIEW_TITLE
Exit Sub
End If
objPPT.Visible = True
End If

'Opens the specified PowerPoint file
With objPPT
Set objPresentation = objPPT.Presentations.Open(sFileName)
End With

'*****************************
On Error GoTo 0
'*****************************

' Set a For loop to go from slide 1 to the last slide in the
' presentation.
For I = 1 To objPresentation.Slides.Count

' Select the slide based on the index value.
With objPresentation.Slides(I)
objPresentation.Application.ActiveWindow.ViewType =
ppViewSlideSorter
objPresentation.Slides.Range(Array(I)).Select
objPresentation.Application.ActiveWindow.ViewType = ppViewSlide

' Loop through all the objects on slide.
For K = 1 To objPresentation.Slides(I).Shapes.Count

' Use only shapes on the slide.
With .Shapes(K)

objPresentation.Application.ActiveWindow.Selection.SlideRange.Shapes(K).Select
' If the shape's type is an OLE object then...
If objPresentation.Slides(I).Shapes(K).Type = 7 Then
'msoLinkedOLEObject Then

' Change the path to new source and set the update
' type to Automatic. First find where the worksheet
' reference is, and then store it in linkname. Next
' assign the new path and concatenate the chart name
' to the end of the path.
With .LinkFormat

' Find where in the source path string the
' character "!" occurs, and assign the position
' to the variable linkpos.
linkpos = InStr(1,
objPresentation.Slides(I).Shapes(K).LinkFormat.SourceFullName, "!", _
vbTextCompare)

' Assign linkname to worksheet reference at the
' end of the source file path.
linkname = Right(.SourceFullName, _
Len(.SourceFullName) - linkpos)

If .SourceFullName = sSourceOrig1 & linkname Then
.SourceFullName = sSource1 & linkname
.AutoUpdate = ppUpdateOptionAutomatic
End If

End With

End If
objPresentation.Application.ActiveWindow.Selection.Unselect
End With

Next K

End With

Next I

' Update all links in the presentation, so that the changes are
' visible and the source file locations are correct on the screen.
objPresentation.UpdateLinks

objPresentation.Save
objPresentation.Close

'Exit PPT if no more presentations
If objPPT.Presentations.Count = 0 Then objPPT.Quit

'Free Memory
Set objPresentation = Nothing
Set objPPT = Nothing

End Sub





The line that does not work is:

linkpos = InStr(1,
objPresentation.Slides(I).Shapes(K).LinkFormat.SourceFullName, "!", _
vbTextCompare)
 
S

shysue25

The objects that I need to change the source for are type 7
(msoEmbeddedOLEObject). Do you know of a way to update the source for
these?
 
S

shysue25

I think what you said is exactly the case ... type 7 objects that have
data linked from Excel.
 
D

Darrell S

shysue25 said:
I have a PowerPoint presentation that has charts that are linked to an
Excel file.

I want to be able to change the source of all the links in the
presentation programmatically, using VB.

Can anyone tell me how to go about doing that?

One way around that is when you create the hyperlinks use "relative path" so
it will look first in the same folder. Insure the linked files are included
in that folder. This way the program hyperlinks will work no matter where
the PPT program is placed.
 

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