Office 2016/365 VBA fails to update PPT links from old script

爱⌒轻易说出口 提交于 2019-12-24 08:22:46

问题


I have a need to change lists of links in large PPT presentations. I had working code that did it fine. Recently I upgraded to Office 2016 and now the script is not working correctly.

I simple put my old and new path strings in a 2 dimensional array and loop through them correcting the links in the PPT.

This has some debug code in it, and come commented lines where I was trying things to fix it, please ignore.

For j = 0 To UBound(MyArray)

  oldString = MyArray(j, 0)
  newString = MyArray(j, 1)

  If Len(oldString) > 1 Then
    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, (pptShape.LinkFormat.SourceFullName), (oldString), 1) Then

                ' debug points
                q = pptShape.Id
                r = pptShape.Name
                s = pptSlide.SlideIndex
                t = pptSlide.SlideNumber

                ' The actual source link
                x = pptShape.LinkFormat.SourceFullName
                ' substitute source link with new string
                y = Replace(x, oldString, newString)
                ' Further adjust new link so any spaces in tab names have %20
                y = Replace(y, "%20", " ")
                'y = Replace(y, " ", "%20")
                'y = Replace(y, "file:///", "")


                ' ignore errors when can't see new link
                On Error Resume Next

                pptShape.LinkFormat.SourceFullName = y
                DoEvents
                ' debug assignment
                w = pptShape.LinkFormat.SourceFullName

                   Debug.Print "old string " & oldString
                   Debug.Print "replacement string " & newString
                   Debug.Print "org link x " & x
                   Debug.Print "new link y " & y
                   Debug.Print "result   w " & w
                   Debug.Print "---"
                   Debug.Print ""


                End If

'            End With
        End If
      DoEvents
      Next pptShape
    DoEvents
    Next pptSlide

  End If

Next

PPT 2016 has changed the links to look like HTML, with a preceeding file:/// and changed all spaces to "%20"

Here's a couple of examples running my script. Now, if I have a different path and a different filename, it's requiring 2 passes of the code with different "matching" strings (one with and one without %20) in each pass. If the filename is the same, then it will do it in one pass of the code, but it opens the files 4 times per link. As I have hundreds of links that's extremely time consuming.

Also in office 2015, as long as I had the target open, no reopening happened. Now, if I have the target open, I get an error saying it can't open 2 files by the same name.

Example 1 with diff paths and diff filenames, all flies closed.

Array fed to code with links to be changed:

MyArray(0, 0) = "file:///\\aFolder\bFolder\Reports\daily%20report%20generator.xlsm!Site%20Completions!R2C2:R17C17"
MyArray(0, 1) = "file:///\\aFolder\bFolder\Reports\Rpt-Other\daily%20report%20generator2.xlsm!Site%20Completions!R2C2:R17C17"

Orig link reported in debugger:

file:///\\aFolder\bFolder\Reports\daily%20report%20generator.xlsm!Site%20Completions!R2C2:R17C17

1st pass, opens files twice (assume target and source), results in the link missing the %20's but no path or filename changed:

\\aFolder\bFolder\Reports\daily report generator.xlsm!Site Completions!R2C2:R17C17

Change the array's 1st element (source path) and run the code a 2nd time. This time the match string is the same except w/o %20. Again, it opened 2 files in this pass, this time it changed the path and filename:

\\aFolder\bFolder\Reports\Rpt-Other\daily report generator2.xlsm!Site Completions!R2C2:R17C17

2nd example, filenames are the same, only path changes. Neither file open. There is only one link to update.

Original link:

file:///\\aFolder\bFolder\Reports\daily%20report%20generator.xlsm!Site%20Completions!R2C2:R17C17

pass 1 opens a file 4 times, but does not take 2 runs of the script as it did in example 1, results in:

\\aFolder\bFolder\Reports\Rpt-Other\daily report generator.xlsm!Site Completions!R2C2:R17C17

Not a big deal but worth noting maybe: If I save file, links don't change, but if I Save, close reopen, PPT adds file:/// and %20's back to the original links.

It appears the HTMLizing of the filenames has hosed the way it's working. And it's causing the files to be opened 4 times per link. Apparently twice per source and target.

Worst of all in the 1st example, which changes both the filename and the path, I have to run the scrip twice, changing the source/match string from with to without "%20" to get it to succeed.

Anyone know how to fix this?

1/26/17: Edited some for better formatting and clarity.

来源:https://stackoverflow.com/questions/41861758/office-2016-365-vba-fails-to-update-ppt-links-from-old-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!