【发布时间】:2018-01-18 21:51:13
【问题描述】:
我是 VBA 编程的新手。但是我必须(并且想要)在 Excel 文件中创建宏来自动创建 PowerPoint 演示文稿。
我希望有人能够帮助我或遇到类似的问题。 即 - 我在 Excel 文件中有 6 列:
1 - slide number
2 - file access path
3 - file name
4 - sheet name
5 - slide range
6 - slide title
我希望宏自动输入给定文件->工作表->获取幻灯片的范围,将其复制并粘贴为演示文稿的图片,并为其赋予适当的标题,然后循环到下一行,然后做同样的事情。
有人可以帮助我吗?下面是我设法编写的代码,但是,我不知道如何从给定单元格中引用工作表和幻灯片的范围。
Option Explicit
Sub ExcelRangeToPowerPoint()
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
Dim adr1 As String
Dim shta As Worksheet
Dim wrk As String
Application.DisplayAlerts = False
wrk = ThisWorkbook.Name ' nname
adr1 = Worksheets("Sheet1").Range("B2")
'Copy Range from Excel
' Set rng = ThisWorkbook.ActiveSheet.Range("A1:C12")
'Create an Instance of PowerPoint
On Error Resume Next
'Is PowerPoint already opened?
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
'Clear the error between errors
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
'Optimize Code
Application.ScreenUpdating = False
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Add
ThisWorkbook.Activate
Range("A2").Select
'DO While
Do While ActiveCell.Value <> ""
Workbooks.Open Filename:=(ActiveCell.Offset(0, 1) & "\" & ActiveCell.Offset(0, 2)), UpdateLinks:=0, ReadOnly:=True ' to be sure read-only open
' Worksheet Open from D2
'Copy Range from E2
'Add a slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile + title from F2
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
ActiveWorkbook.Close SaveChanges:=False ' close file and don't save
ActiveCell.Offset(1, 0).Range("A1").Select
Loop
MsgBox ("Ready")
Application.CutCopyMode = False
Application.DisplayAlerts = True
End Sub
【问题讨论】:
-
这会有所帮助,但我想从中创建一些幻灯片的所有数据源都在几个 excel 文件中。这就是我的问题,因为我不知道如何引用其他文件中的工作表和范围。但是感谢您的快速回答:)
-
假设您在表 2 中,如果您想从表 1 中引用,请使用 =sheet1!A1 希望它有所帮助。利用 !参考其他表格
-
好的,但是我怎样才能从另一个工作簿中引用一些工作表和一些范围(警告! - 此工作表的名称和范围在 D 和 E 列中) - 你可以在我的屏幕上查看已添加。
-
对于范围,您是否打算对值求和?还是别的什么(解释一下)?如果你是,试试 =sheet1!SUM(B2:N15)
标签: vba excel powerpoint