PPT宏以批量更改源链接

时间:2016-11-28 19:15:24

标签: powerpoint-vba

无法找到答案。试图为powerpoint套牌中链接的一堆图表和对象更改excel源文件。我发现了这个:

Sub ChangeOLELinks()

Dim oSld As Slide
Dim oSh As Shape
Dim sOldPath As String
Dim sNewPath As String

' EDIT THIS TO REFLECT THE PATHS YOU WANT TO CHANGE
sOldPath = InputBox("Enter Old Project ie: \Development\", "Old Path") 
sNewPath = InputBox("Enter New Project ie: \Test\", "New Path") 

On Error GoTo ErrorHandler

For Each oSld In ActivePresentation.Slides
    For Each oSh In oSld.Shapes
        If oSh.Type = msoLinkedOLEObject Then

            Dim stringPath   As String
            stringPath = Replace(oSh.LinkFormat.SourceFullName, sOldPath, sNewPath, 1, , vbTextCompare)

            oSh.LinkFormat.SourceFullName = stringPath
           ' set update mode to auto and update then set it back to manual
            oSh.LinkFormat.AutoUpdate = ppUpdateOptionAutomatic
            oSh.LinkFormat.Update
            oSh.LinkFormat.AutoUpdate = ppUpdateOptionManual

        End If
    Next oSh
Next oSld
ActivePresentation.Save

MsgBox ("Done!")

NormalExit:
    Exit Sub

ErrorHandler:
    MsgBox ("Error " & Err.Number & vbCrLf & Err.Description)
    Resume NormalExit

End Sub

哪个效果很好,但仅适用于OLE对象/链接,它不会更新任何链接图表。如何修改它以便它也包含图表?

1 个答案:

答案 0 :(得分:0)

由于图表是通过Excel粘贴链接(第4个选项),因此它们属于msoChart类型。
y <-c(220, 160, 145, 127, 117, 114, 106, 100, 101, 97, 87, 70, 64, 65) t <- seq(1,14,1) # starting values: lm <-lm(log(y) ~ log(t)) # y <-c(220, 160, 145, 127, 117, 114, 106, 100, 101, 97, 87, 70, 64, 65) t <- seq(1,14,1) c <- c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0) d <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0) e <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0) # starting values: lm <-lm(log(y) ~ log(t)) # Exp. Least-Squares minimization: func <-function(pars) { a <- pars["a"] b <- pars["b"] c <- pars["c"] d <- pars["d"] e <- pars["e"] fitted <- a*exp(b*t) + c + d + e sum((y-fitted)^2) } a <-lm$coefficients[[1]] b <-lm$coefficients[[2]] c <- 0 d <- 0 e <- 0 result <- optim(c(a=a, b=b, c=c, d=d, e=e), func) # final parameters: a <- result$par["a"] b <- result$par["b"] c <- result$par["c"] d <- result$par["d"] e <- result$par["e"] # predict values: pred <- a*exp(b*t) dat = data.frame(y=y, t=t, pred=pred) library(ggplot2) ggplot(dat, aes(x=t, y=y)) + geom_point() + geom_line(data=dat, aes(x=t, y=pred), color='blue') 属性也适用于这种类型,因此您只需替换

LinkFormat.SourceFullName

使用

If oSh.Type = msoLinkedOLEObject