首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA保存Powerpoint

VBA保存Powerpoint
EN

Stack Overflow用户
提问于 2018-05-01 17:24:36
回答 1查看 155关注 0票数 0

我已经设法打开了我的powerpoint模板,然后添加了excel的图表,现在我试图用一个新的名称保存它,但是我的代码总是掉下来。错误是

运行时错误'91':对象变量或块变量未设置

主码

代码语言:javascript
复制
Option Explicit

Public PPT As PowerPoint.Application
Public PPT_pres As PowerPoint.Presentation

Sub master()

Dim fileNameString As String

fileNameString = "C:\Users\Person\Desktop\Testfolder\Test1.pptx"

Call Module4.OpenPowerpoint

Call Module4.PasteCharts

PPT_pres.SaveAs fileNameString, 1 'ERROR HERE

PPT.Quit

End Sub

其中模块4.是

代码语言:javascript
复制
Public Sub OpenPowerpoint()
    Set PPT = New PowerPoint.Application

    PPT.Visible = True
    Set PPT_pres = PPT.Presentations.Open(Filename:="C:\Users\Person\Desktop\Testfolder\Test - Template.pptx")

End Sub

过去的图表代码--对不同的图表重复几次。

代码语言:javascript
复制
Public Sub CopyPasteForecastTopGraph()
    If PPT Is Nothing Then Exit Sub
    If PPT_pres Is Nothing Then Exit Sub

    Dim rng As Range
    Dim mySlide As Object
    Dim myShape As Object
    Dim cht As Chart

    Set mySlide = PPT_pres.Slides(5)

    With mySlide
    .Select
    Set cht = ThisWorkbook.Worksheets("Forecast").ChartObjects("FcChart").Chart

       cht.CopyPicture Appearance:=xlScreen, Format:=xlPicture, Size:=xlScreen
       .Shapes.Paste.Select

        '''''''''''''''''''''''''''''''''
        'Paste as Chart and break link. '
        '''''''''''''''''''''''''''''''''
        'cht.ChartArea.Copy
        '.Shapes.Paste.Select


    'With .Shapes("HcChart")
        '.LinkFormat.BreakLink
    'End With

        PPT_pres.Windows(1).Selection.ShapeRange.Left = 35
        PPT_pres.Windows(1).Selection.ShapeRange.Top = 110
        PPT_pres.Windows(1).Selection.ShapeRange.Width = 720
        PPT_pres.Windows(1).Selection.ShapeRange.Height = 300

        End With

    'Clear The Clipboard
    Application.CutCopyMode = False
    Application.Wait (Now + TimeValue("00:00:01"))

End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-01 17:56:58

由于您没有显示PasteCharts的代码,所以很难分辨。因为在对象中使用全局变量来编码PPT.quit是“杂乱无章”的,特别是当您不关闭PPT_pres并始终在Module4中创建Powerpoint的新实例时。

要么完全摆脱全局变量并重新对代码进行重新分析,要么在错误发生的代码行ppt_pres.SaveAs fileNameString, 1之后尝试以以下方式更改代码

代码语言:javascript
复制
ppt_pres.Close
Set ppt_pres = Nothing
PPT.Quit
Set PPT = Nothing
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50120944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档