【问题标题】:VBA save PowerpointVBA 保存 Powerpoint
【发布时间】:2018-05-02 16:00:21
【问题描述】:

我已经设法打开我的 powerpoint 模板,然后从 excel 中添加图表,现在我正在尝试用新名称保存它,但是我的代码不断出错。错误是

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

主码

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

Module4.OpenPowerPoint 在哪里

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

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

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

【问题讨论】:

  • 你能把PasteCharts的代码贴出来吗?里面有什么东西会改变PPT_pres的变量吗?另外,如果您将OpenPowerpoint 代码移动到master 子代码中会发生什么?那它有用吗?
  • 我建议摆脱全局变量。

标签: vba excel powerpoint


【解决方案1】:

由于您没有显示 PasteCharts 的代码,因此很难分辨。因为您使用全局变量为您的对象编码 PPT.quit 可能会“混乱”,尤其是当您不关闭 PPT_pres 并始终在 Module4 中创建新的 Powerpoint 实例时。

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

ppt_pres.Close
Set ppt_pres = Nothing
PPT.Quit
Set PPT = Nothing

【讨论】:

    猜你喜欢
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    相关资源
    最近更新 更多