简体   繁体   English

Excel 到 PowerPoint VBA

[英]Excel to PowerPoint VBA

I am trying to paste few cells from Excel to a slide in PowerPoint, using an ActiveX CheckBox in Excel as control.我正在尝试将 Excel 中的几个单元格粘贴到 PowerPoint 中的幻灯片,使用 Excel 中的 ActiveX CheckBox 作为控件。 There is no problem of transferring one slide to my designated PowerPoint Presentation, but the problem appears when I ticked more than one box.将一张幻灯片传输到我指定的 PowerPoint 演示文稿没有问题,但是当我勾选多个框时出现问题。

So what I do is basically making a temporary template presentation, and when I click on another button called the "Launch" button, it will be pasted to my designated presentation.所以我所做的基本上是制作一个临时模板演示文稿,当我单击另一个名为“启动”按钮的按钮时,它将被粘贴到我指定的演示文稿中。 This is my code:这是我的代码:

Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then

Dim PPSlide As PowerPoint.Slide
Dim PPPres As PowerPoint.Presentation
Dim PP As PowerPoint.Application
Set PP = CreateObject("PowerPoint.Application")
Set PPPres = PP.Presentations.Open("(my temporary filename)")
Dim iCheckCount As Integer
iCheckCount = 0
Dim obj As OLEObject

    For Each obj In ActiveSheet.OLEObjects
        If obj.Object.Value = True Then iCheckCount = iCheckCount + 1

            Next


If iCheckCount = 1 Then

Set PPSlide = PPPres.Slides(1)


With PPSlide
.Shapes("Textfeld 2").TextFrame.TextRange.Text = ActiveSheet.Range("G3").Text
.Shapes("Textfeld 3").TextFrame.TextRange.Text = ActiveSheet.Range("B3").Text
.Shapes("Textfeld 4").TextFrame.TextRange.Text = ActiveSheet.Range("C3").Text
.Shapes("Textfeld 5").TextFrame.TextRange.Text = ActiveSheet.Range("D3").Text
.Shapes("Textfeld 6").TextFrame.TextRange.Text = ActiveSheet.Range("F3").Text
End With
PPPres.Slides(1).Copy

Else
If iCheckCount > 1 Then
    PPPres.Slides.Paste
    PPPres.Slides(2).Copy

Set PPSlide = PPPres.Slides(1)
With PPSlide
.Shapes("Textfeld 2").TextFrame.TextRange.Text = ActiveSheet.Range("G3").Text
.Shapes("Textfeld 3").TextFrame.TextRange.Text = ActiveSheet.Range("B3").Text
.Shapes("Textfeld 4").TextFrame.TextRange.Text = ActiveSheet.Range("C3").Text
.Shapes("Textfeld 5").TextFrame.TextRange.Text = ActiveSheet.Range("D3").Text
.Shapes("Textfeld 6").TextFrame.TextRange.Text = ActiveSheet.Range("F3").Text
End With

End If
End If
End If
End Sub

I know that it won't work for more than 2 boxes (copied to designated Presentation).我知道它不能用于超过 2 个盒子(复制到指定的演示文稿)。 So my questions are:所以我的问题是:

1) How can you copy more than 1 Slide at once? 1)如何一次复制多张幻灯片? I´ve tried我试过了

For i = 1 to PPPres.Slides.Count
PPPres.Slides.Item(i).Copy
Next i

but it won't work.但它不会工作。

2) I found an if code for every ticked ActiveX CheckBox I have. 2) 我为我拥有的每个勾选的 ActiveX CheckBox 找到了一个 if 代码。 But the problem is, how can I mention all the Sub for CheckBox_Click and ask the program to do it?但问题是,我怎么能提到 CheckBox_Click 的所有 Sub 并让程序去做呢? The names of the Sub are Box1, Box2, Box3,...,Box46. Sub 的名称是 Box1、Box2、Box3、...、Box46。

I know that my questions are really messy and I'm not explaining it very well since I am also new to VBA.我知道我的问题真的很乱,我解释得不是很好,因为我也是 VBA 的新手。 Don't hesitate to ask me if you want to know more about my code.如果您想了解更多关于我的代码的信息,请随时问我。

Thankyou!谢谢!

You may use eg:您可以使用例如:

ActivePresentation.Slides.Range(Array(1, 2, 3)).Duplicate
' Or
For i = 1 to PPPres.Slides.Count
    PPPres.Slides.Item(i).Duplicate
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM