在Excel VBA中读取文件路径

时间:2017-07-26 14:30:59

标签: vba excel-vba runtime-error powerpoint filepath

我每周都会使用VBA从Excel中的某些图表创建PowerPoint。但是,第一张幻灯片需要来自PowerPoint创建的持续一周。

文件路径和名称都是变量,因为它们的标题中包含日期。我能够解释这一点并使用实际文件的名称进行检查。对我来说看起来一样。但是,当我尝试打开文件时,我收到ActiveX错误/运行时错误429.任何想法都将不胜感激

Sub CreateNewPres()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim objPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppTextbox As PowerPoint.Shape

Set ppApp = New PowerPoint.Application
ppApp.Visible = True
ppApp.Activate

Set ppPres = ppApp.Presentations.Add

todayDate = Date
myTextDate = Format(todayDate, "yyyy-mm-dd")
myFilePath = "C:\Desktop\Main\" & myTextDate
myFileName = "\Meeting_" & myTextDate & ".pptx"
myFile = myFilePath & myFileName
objPres=_
   Presentations.Open(myFile)

objPres.Slides(1).Copy
ppPres.Slides.Paste (ppPasteEnchancedMetafile)
Set ppTextbox = ppSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, _
    Left:=0, Top:=0, Width:=30, Height:=10)
    With myTextBox.TextFrame.TextRange.Text = todayDate
    End With

1 个答案:

答案 0 :(得分:1)

更改此行:

objPres = Presentations.Open(myFile)

到此:

Set objPres = ppApp.Presentations.Open(myFile)