运行Powerpoint VBA更新Excel链接时的奇怪行为
我需要一些关于Powerpoint中一些奇异VBA代码行为的帮助。目的很简单-更新Powerpoint演示文稿上的Excel链接。我有一个演示文稿,其中包含链接到Excel文件的对象。从Powerpoint运行代码时,系统会提示用户选择硬盘上的源Excel文件,此Excel文件的位置用于替换Powerpoint演示文稿中已保存的Excel文件的以前位置。 运行宏,检查链接,更新其路径。单击“保存”,关闭演示文稿。你打开演示文稿,一切都很好。 现在假设您更改了Excel文件的名称。运行宏,检查链接,更新其路径。单击“保存”,关闭演示文稿。您打开演示文稿,只更新了一半的链接。谁能看一下吗?谢谢运行Powerpoint VBA更新Excel链接时的奇怪行为,excel,vba,integration,powerpoint,Excel,Vba,Integration,Powerpoint,我需要一些关于Powerpoint中一些奇异VBA代码行为的帮助。目的很简单-更新Powerpoint演示文稿上的Excel链接。我有一个演示文稿,其中包含链接到Excel文件的对象。从Powerpoint运行代码时,系统会提示用户选择硬盘上的源Excel文件,此Excel文件的位置用于替换Powerpoint演示文稿中已保存的Excel文件的以前位置。 运行宏,检查链接,更新其路径。单击“保存”,关闭演示文稿。你打开演示文稿,一切都很好。 现在假设您更改了Excel文件的名称。运行宏,检查链接
Private Sub CommandButton1_Click()
Dim xlApp As Object
Dim xlWorkBook As Object
Dim pptSlide As Slide
Dim pptShape As Shape
Dim oldString, tempString, newString As String
Dim intLength As Integer
Dim sPath As String
Dim ExcelFileName As String
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Please select the file to update links in the presentation"
' Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "Excel Workbook", "*.xlsx"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
newString = .SelectedItems(1) 'replace txtFileName with your textbox
End If
End With
'show "macro running" screen
UserForm1.Show False
'open excel file with links
Set xlApp = CreateObject("Excel.Application")
Set xlWorkBook = xlApp.Workbooks.Open(newString, True, False)
'grab old full path to replace link in objects
For Each pptSlide In ActivePresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoLinkedOLEObject Then
tempString = pptShape.LinkFormat.SourceFullName
intLength = InStr(tempString, "!")
oldString = Mid(tempString, 1, intLength - 1)
GoTo 1
End If
If pptShape.Type = msoChart Then
oldString = pptShape.LinkFormat.SourceFullName
GoTo 1
End If
Next pptShape
Next pptSlide
1
'replace old full path to new full path
For Each pptSlide In ActivePresentation.Slides
For Each pptShape In pptSlide.Shapes
If pptShape.Type = msoLinkedOLEObject Or pptShape.Type = msoChart Then
With pptShape.LinkFormat
If InStr(1, UCase(.SourceFullName), UCase(oldString)) Then
.SourceFullName = Replace(.SourceFullName, oldString, newString)
End If
End With
pptShape.LinkFormat.Update
End If
'DoEvents
Next pptShape
'DoEvents
Next pptSlide
'close excel file with links
xlWorkBook.Close (False)
xlApp.Quit
Set xlApp = Nothing
Set xlWorkBook = Nothing
'hide "macro running" screen
UserForm1.Hide
End Sub
是否存在更新链接和不更新链接的模式?此外,请说明您使用的PPT的版本/版本和内部版本。是的,所有工作表对象都已更新,但没有图表。i、 e.当您调用Excel编辑链接窗口时,所有“类型:图表”都不会更新,所有其他链接,即“类型:工作表”都会更新。此外,宏运行速度非常慢-有关于速度优化的建议吗?我使用的是Office 2016/365,版本1708,构建8431.2250,我认为可能是这样。msoChart对象是本机PowerPoint图表,而不是链接的Excel图表对象。在这里,如果我选择其中一个并尝试,例如MsgBox ActiveWindow.Selection.ShaperAge(1).LinkFormat.SourceFullname,我会收到一条错误消息:LinkFormat(未知成员):请求无效。此操作需要一个链接对象。请重新设置宏,使其缓慢运行:可能是因为每次调用Update时(也可能是更改链接的源文件名时),宏都会启动Excel。在开始更新链接时添加断点,并在逐步完成代码时在TaskManager中查看Excel实例。然后在运行代码之前,尝试手动打开所选的Excel文件,看看这是否会大大加快速度。