根据 table 中提供的输入将其他演示文稿中的特定幻灯片插入当前
Insert specific slides from other presentations to current on based on input provided in table
我对 VBA 很陌生(不到一周)。我正在尝试创建一个宏,该宏将根据幻灯片 1 上 table 中提供的文件路径和幻灯片范围在当前演示文稿中插入幻灯片。我创建了以下代码,但我认为我做错了,因为它不起作用。请帮忙...
Sub Insert_Slides()
Dim sl As Slide
Dim tbl As Table
Dim shp As Shape
Dim filepath As String
Dim slidestart As String
Dim slideend As String
Set sl = ActivePresentation.Slides(1)
Set tbl = sl.Shapes("Contents").Table
Set filepath = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 1).Select
Set slidestart = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 2).Select
Set slideend = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 3).Select
ActivePresentation.Slides.InsertFromFile _
filepath, 1, slidestart, slideend
End Sub
阿什温,
两个对 VBA 新用户来说并不明显的提示:
1) 始终使用 Option Explicit 启动每个模块。如果你去工具 |选项 |编辑器选项卡并在 "Require Variable Declaration" 旁边打勾,VBA 将自动为您执行此操作。除非你在Mac。
1a) 如果您在变量名称中使用不同类型的大小写...FilePath 或 Filepath 而不是 filepath...您可以按照自己喜欢的方式键入名称,VBA 编辑器将会更改你声明的外壳。如果没有,您就知道您输入了错误的变量名,或者一开始就没有声明它。
2) 始终选择调试 |在尝试 运行 你的代码之前编译。继续编译,直到找到并修复所有编译器错误。
这是您的代码的修改版本。我没有时间设置一组测试演示文稿来试用它,但我会给出比偶数更好的概率,你有这个方便。试一试,让我们知道结果如何。
Option Explicit
Sub Insert_Slides()
Dim sl As Slide
Dim tbl As Table
Dim shp As Shape
Dim filepath As String
' These need to be Longs, not Strings
Dim slidestart As Long
Dim slideend As Long
Set sl = ActivePresentation.Slides(1)
Set tbl = sl.Shapes("Contents").Table
' Use SET to assign objects to object variables, but not to assign values to
' string/numeric variables such as you're using.
' AND never select anything unless there's an absolute need to. It can slow your code down by a factor of 10 and
' can prevent it from running in some circumstances.
' AND by using With/End With you can shorten the code considerably;
' easier to read and, at least in theory, runs a bit faster
' SO ...
'Set filepath = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 1).Select
With ActivePresentation.Slides(1).Shapes("Contents").Table
filepath = .Cell(2, 1).Shape.TextFrame.TextRange.Text
' since the table cells contain text (strings), you should convert them to Longs before assigning
' them to a Long variable:
slidestart = CLng(.Cell(2, 2).Shape.TextFrame.TextRange.Text)
slideend = CLng(.Cell(2, 3).Shape.TextFrame.TextRange.Text)
End With
' and you might want to add a test here;
' make sure the file exists before trying to access it:
If Len(Dir$(filepath)) > 0 Then
ActivePresentation.Slides.InsertFromFile _
filepath, 1, slidestart, slideend
Else
MsgBox "Cannot locate file " & filepath
End If
End Sub
我对 VBA 很陌生(不到一周)。我正在尝试创建一个宏,该宏将根据幻灯片 1 上 table 中提供的文件路径和幻灯片范围在当前演示文稿中插入幻灯片。我创建了以下代码,但我认为我做错了,因为它不起作用。请帮忙...
Sub Insert_Slides()
Dim sl As Slide
Dim tbl As Table
Dim shp As Shape
Dim filepath As String
Dim slidestart As String
Dim slideend As String
Set sl = ActivePresentation.Slides(1)
Set tbl = sl.Shapes("Contents").Table
Set filepath = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 1).Select
Set slidestart = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 2).Select
Set slideend = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 3).Select
ActivePresentation.Slides.InsertFromFile _
filepath, 1, slidestart, slideend
End Sub
阿什温,
两个对 VBA 新用户来说并不明显的提示:
1) 始终使用 Option Explicit 启动每个模块。如果你去工具 |选项 |编辑器选项卡并在 "Require Variable Declaration" 旁边打勾,VBA 将自动为您执行此操作。除非你在Mac。
1a) 如果您在变量名称中使用不同类型的大小写...FilePath 或 Filepath 而不是 filepath...您可以按照自己喜欢的方式键入名称,VBA 编辑器将会更改你声明的外壳。如果没有,您就知道您输入了错误的变量名,或者一开始就没有声明它。
2) 始终选择调试 |在尝试 运行 你的代码之前编译。继续编译,直到找到并修复所有编译器错误。
这是您的代码的修改版本。我没有时间设置一组测试演示文稿来试用它,但我会给出比偶数更好的概率,你有这个方便。试一试,让我们知道结果如何。
Option Explicit
Sub Insert_Slides()
Dim sl As Slide
Dim tbl As Table
Dim shp As Shape
Dim filepath As String
' These need to be Longs, not Strings
Dim slidestart As Long
Dim slideend As Long
Set sl = ActivePresentation.Slides(1)
Set tbl = sl.Shapes("Contents").Table
' Use SET to assign objects to object variables, but not to assign values to
' string/numeric variables such as you're using.
' AND never select anything unless there's an absolute need to. It can slow your code down by a factor of 10 and
' can prevent it from running in some circumstances.
' AND by using With/End With you can shorten the code considerably;
' easier to read and, at least in theory, runs a bit faster
' SO ...
'Set filepath = ActivePresentation.Slides(1).Shapes("Contents").Table.Cell(2, 1).Select
With ActivePresentation.Slides(1).Shapes("Contents").Table
filepath = .Cell(2, 1).Shape.TextFrame.TextRange.Text
' since the table cells contain text (strings), you should convert them to Longs before assigning
' them to a Long variable:
slidestart = CLng(.Cell(2, 2).Shape.TextFrame.TextRange.Text)
slideend = CLng(.Cell(2, 3).Shape.TextFrame.TextRange.Text)
End With
' and you might want to add a test here;
' make sure the file exists before trying to access it:
If Len(Dir$(filepath)) > 0 Then
ActivePresentation.Slides.InsertFromFile _
filepath, 1, slidestart, slideend
Else
MsgBox "Cannot locate file " & filepath
End If
End Sub