PowerPoint 中的断开链接 运行 但不起作用
Breaklinks in PowerPoint running but not working
我正在制作 PowerPoint,其中包括 link 中数据的图表 Excel。我想通过 VBA 中的宏删除此 link。
我使用了以下宏,它执行时没有错误,但是当我查看 PowerPoint 时,我仍然可以按照 links 回到我的 Excel 文件。有人能看出问题吗?
Sub BreakAllLinks()
Dim oSld As PowerPoint.Slide
Dim oSh As PowerPoint.Shape
Dim Yes As Integer
Dim PowerPointApp As PowerPoint.Application
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'// Check if more then single powerpoint open
If PowerPointApp.Presentations.Count > 1 Then
MsgBox "Please close all other PowerPoints"
Exit Sub
End If
Yes = MsgBox("Are you sure you want to break all links in your active PowerPoint presentation?", vbYesNo + vbQuestion, "Break ALL links")
If Yes = vbYes Then
For Each oSld In PowerPointApp.ActivePresentation.Slides
For Each oSh In oSld.Shapes
If oSh.Type = msoLinkedOLEObject Then 'SOLUTION EDIT: msoChart
oSh.LinkFormat.BreakLink
End If
Next ' Shape
Next ' Slide
End If
End Sub
提前致谢。
Let me start by saying I'm not an expert in OLE linked charts. Someone else may explain this much better than myself. So disclaimer done ... oSh.Type = msoLinkedOLEObject doesn't correctly identify all linked charts. A linked chart maybe oSh.Type = msoChart for example. I've found that a better way of identifying a chart that is linked is by testing for the existence of the 属性 Shape.LinkedFormat.AutoUpdate ... the 属性 AutoUpdate only exists for linked charts ... and does not exist for non-linked charts. Referring to it for a non-linked chart will generate a runtime error. However if we're going to use its existance, then check whether a 属性 exists in VBA (a pretty clunky process).例如:
Private Function IsLinked(myShape As Shape)
Dim AutoUpdate As Variant
On Error GoTo Err_Handler
IsLinked = False
AutoUpdate = myShape.LinkFormat.AutoUpdate
IsLinked = True
Err_Handler:
End Function
So you could test if oSh is linked using the above function. The next challenge is that BreakLink doesn't (always) work. The best method is to simply set AutoUpdate to manual update ... like:
oSh.LinkFormat.AutoUpdate = ppUpdateOptionManual
Alternatively, you could always copy the charts in without the links in the first place.
我正在制作 PowerPoint,其中包括 link 中数据的图表 Excel。我想通过 VBA 中的宏删除此 link。 我使用了以下宏,它执行时没有错误,但是当我查看 PowerPoint 时,我仍然可以按照 links 回到我的 Excel 文件。有人能看出问题吗?
Sub BreakAllLinks()
Dim oSld As PowerPoint.Slide
Dim oSh As PowerPoint.Shape
Dim Yes As Integer
Dim PowerPointApp As PowerPoint.Application
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'// Check if more then single powerpoint open
If PowerPointApp.Presentations.Count > 1 Then
MsgBox "Please close all other PowerPoints"
Exit Sub
End If
Yes = MsgBox("Are you sure you want to break all links in your active PowerPoint presentation?", vbYesNo + vbQuestion, "Break ALL links")
If Yes = vbYes Then
For Each oSld In PowerPointApp.ActivePresentation.Slides
For Each oSh In oSld.Shapes
If oSh.Type = msoLinkedOLEObject Then 'SOLUTION EDIT: msoChart
oSh.LinkFormat.BreakLink
End If
Next ' Shape
Next ' Slide
End If
End Sub
提前致谢。
Let me start by saying I'm not an expert in OLE linked charts. Someone else may explain this much better than myself. So disclaimer done ... oSh.Type = msoLinkedOLEObject doesn't correctly identify all linked charts. A linked chart maybe oSh.Type = msoChart for example. I've found that a better way of identifying a chart that is linked is by testing for the existence of the 属性 Shape.LinkedFormat.AutoUpdate ... the 属性 AutoUpdate only exists for linked charts ... and does not exist for non-linked charts. Referring to it for a non-linked chart will generate a runtime error. However if we're going to use its existance, then check whether a 属性 exists in VBA (a pretty clunky process).例如:
Private Function IsLinked(myShape As Shape)
Dim AutoUpdate As Variant
On Error GoTo Err_Handler
IsLinked = False
AutoUpdate = myShape.LinkFormat.AutoUpdate
IsLinked = True
Err_Handler:
End Function
So you could test if oSh is linked using the above function. The next challenge is that BreakLink doesn't (always) work. The best method is to simply set AutoUpdate to manual update ... like:
oSh.LinkFormat.AutoUpdate = ppUpdateOptionManual
Alternatively, you could always copy the charts in without the links in the first place.