首页 文章

编写Excel VBA代码/宏以使用Excel单元格值填充Powerpoint文本框

提问于
浏览
0

我试图在Excel单元格中取值并填充PowerPoint文本框 . 我不想将PowerPoint表链接到Excel电子表格,因为电子表格不断变化,并且值不总是在相同的行或相同的顺序中 .

所以我正在编写这个VBA代码来尝试填充文本框 . 我做了很多VBA,但从未尝试过这种组合 . 下面是我到目前为止(更多的代码将被添加到其他文本框,但需要首先工作) . 我意识到这个问题与未正确处理的对象有关,但不确定如何纠正它 .

我正在使用Excel和PowerPoint 2007.粗体语句是我收到错误的地方 - 438对象不支持此属性或方法 .

谢谢!

Sub valppt()

 Dim PPT As PowerPoint.Application
    Dim newslide As PowerPoint.Slide
    Dim slideCtr As Integer
    Dim tb As PowerPoint.Shape
    Set PPT = CreateObject("PowerPoint.Application")
    PPT.Visible = True

    PPT.Presentations.Open "C:\Documents\createqchart.pptx"

    Range("F2").Activate
    slideCtr = 1

    Set newslide = ActivePresentation.Slides(slideCtr).Duplicate
    Set tb = newslide.Shapes("TextBox1")

    slideCtr = slideCtr + 1
    ' Do Until ActiveCell.Value = ""
    Do Until slideCtr > 2
        If slideCtr = 2 Then
           tb.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
        End If
        ActiveCell.Offset(0, 1).Activate
        slideCtr = slideCtr + 1

        If slideCtr = 38 Then
            Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
            ActiveCell.Offset(1, -25).Activate
        End If

      Loop

   End Sub

更新5/17

虽然幻灯片的复制工作,我仍然无法评估文本框 . 我无法在语句之前提出正确的set语句,以便将值赋给文本框 . 现在我在那里甚至没有固定声明,因为我没能得到合适的声明 . 任何帮助表示赞赏 . 以下是最新代码 .

Sub shptppt()
'
' shptppt Macro
'

    Dim PPT As PowerPoint.Application
    Dim pres As PowerPoint.Presentation
    Dim newslide As PowerPoint.Slide
    Dim slideCtr As Integer
    Dim tb As PowerPoint.Shape


    Set PPT = CreateObject("PowerPoint.Application")
    PPT.Visible = True

    Set pres = PPT.Presentations.Open("C:\Documents\createqchart.pptx")

    Range("F2").Activate
    slideCtr = 1

    'Set newslide = ActivePresentation.Slides(slideCtr).Duplicate
    ' Set tb = newslide.Shapes("TextBox1")


    pres.Slides(slideCtr).Copy
    pres.Slides.Paste
    Set newslide = pres.Slides(pres.Slides.Count)
    newslide.MoveTo slideCtr + 1

    slideCtr = slideCtr + 1
    ' Do Until ActiveCell.Value = ""
    Do Until slideCtr > 2
        If slideCtr = 2 Then
            tb.Slides.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
        End If
        ActiveCell.Offset(0, 1).Activate
        slideCtr = slideCtr + 1

        If slideCtr = 38 Then
            Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
            ActiveCell.Offset(1, -25).Activate
        End If

    Loop

End Sub

2 回答

  • 1

    txtReqBase 无效 . 它当然不是Powerpoint支持的属性/方法,并且's why you'得到了438错误 .

    要在形状中插入文本,您需要识别形状,然后操纵它 .Text . 我发现用形状变量做这件事最容易 .

    '## If you have enabled reference to Powerpoint, then:'
    Dim tb As Powerpoint.Shape
    '## If you do not enable Powerpoint reference, use this instead'
    'Dim tb as Variant '
    
    Set tb = newSlide.Shapes("TextBox1")  '## Update this to use the correct name or index of the shapes collection ##'
    
    tb.TextFrame2.TextRange.Characters.Text = ActiveCell.Value
    

    UPDATE 对于不匹配错误设置 tb .

    我收到了不匹配错误,因为你有 PPT As Object 而不是启用对Powerpoint对象库的引用,这将允许你将它完全标注为 PowerPoint.Application .

    您当前的代码解释 Dim tb as Shape 指的是Excel.Shape,而不是Powerpoint.Shape .

    如果启用对Powerpoint对象库的引用,则可以执行此操作

    Dim PPT as Powerpoint.Application
    Dim newSlide as Powerpoint.Slide
    Dim tb as Powerpoint.Shape
    

    如果您没有启用对PPT对象库的引用,请尝试 Dim tb as VariantDim tb as Object ,这可能有效 .

    UPDATE 2 如何启用Powerpoint参考:

    在VBE中,来自Tools |参考,选中与您机器上支持的PPT版本对应的框 . 在Excel 2010中,这是14.0 . 在2007年,我认为它是12.0 .

    Enable reference to PPT Object Library

    Update 3

    Duplicate 方法似乎在2007年不可用 . 在任何情况下,它也会在2010年导致奇怪的错误,尽管幻灯片被正确复制,但未设置变量 .

    试试这个:

    Sub PPTTest()
    
    Dim PPT As PowerPoint.Application
    Dim pres As PowerPoint.Presentation
    Dim newslide As PowerPoint.Slide
    Dim slideCtr As Integer
    Dim tb As PowerPoint.Shape
    
    Set PPT = CreateObject("PowerPoint.Application")
    PPT.Visible = True
    
    
    'Control the presentation with a variable
    Set pres = PPT.Presentations.Open("C:\users\david_zemens\desktop\Presentation1.pptx")
    
    Range("F2").Activate
    slideCtr = 1
    
    '## This only works in 2010/2013 ##
    'pres.Slides(slideCtr).Duplicate
    
    '## Use this method in Powerpoint 2007 (hopefully it works)
    pres.Slides(slideCtr).Copy
    pres.Slides.Paste
    Set newslide = pres.Slides(pres.Slides.Count)
    newslide.MoveTo slideCtr + 1
    ...
    
  • 0

    我忘记了我已经从文本框切换到activex控件文本框 . 这是现在正确的代码 .

    valppt()
    Dim PPT As PowerPoint.Application
    Dim newslide As PowerPoint.SlideRange
    Dim slideCtr As Integer
    Dim tb As PowerPoint.Shape
    Set PPT = CreateObject("PowerPoint.Application")
    PPT.Visible = True
    
    PPT.Presentations.Open ("C:\Documents\createqchart.pptx")
    
    Range("F2").Activate
    slideCtr = 1
    
    Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
    Set tb = newslide.Shapes("TextBox" & slideCtr)
    
    slideCtr = slideCtr + 1
    Do Until ActiveCell.Value = ""
    'Do Until slideCtr > 2
        If slideCtr = 2 Then
           tb.OLEFormat.Object.Value = Format(ActiveCell.Value, "m/d/yyyy")
        End If
        ActiveCell.Offset(0, 1).Activate
        slideCtr = slideCtr + 1
    
        If slideCtr = 38 Then
            Set newslide = PPT.ActivePresentation.Slides(slideCtr).Duplicate
            ActiveCell.Offset(1, -25).Activate
        End If
    
    Loop
    End Sub
    

相关问题