所以我有多个幻灯片,大约有100张幻灯片都需要添加音频。我想以编程方式将音频导入到幻灯片中,并且我尽可能地继承...请注意我不是VB开发人员,因此非常感谢任何帮助。 TIA!
Const audioFileName As String = "C:\Users\user\audio\Lesson 1 - Slide "
Sub AddMedia()
Dim A As Integer
A = 0
For Each sld In ActivePresentation.Slides
Dim shp As Shape
Dim CurrentAudioFileName As String
If Len(A) <= 2 Then 'zero pad that position
CurrentAudioFileName = audioFileName & String(2 - Len(A), "0") & A & ".mp3"
Else
If Len(CStr(A)) <= 2 Then 'zero pad that position
CurrentAudioFileName = audioFileName & String(2 - Len(CStr(A)), "0") & A & ".mp3"
Else
CurrentAudioFileName = audioFileName & CStr(A) & ".mp3"
End If
' This audio is embedded, not linked.
Set shp = sld.Shapes.AddMediaObject2(CurrentAudioFileName, msoFalse, msoTrue, 350, 10)
DisplayMediaInfo shp
A = A + 1
Next
End Sub
Private Sub DisplayMediaInfo(shp As Shape)
If shp.Type = msoMedia Then
Debug.Print "Embedded: " & shp.MediaFormat.IsEmbedded
Debug.Print "Linked: " & shp.MediaFormat.IsLinked
End If
End Sub
现在,当我通过代码并来到这篇文章时
Set shp = sld.Shapes.AddMediaObject2(CurrentAudioFileName, msoFalse, msoTrue, 350, 10)
DisplayMediaInfo shp
A = A + 1
Next
我收到编译错误:没有For的Next,而VisualBasic确定了Next。
答案 0 :(得分:0)
我相信.AddMediaObject2需要引用幻灯片。 此外,.ToString方法在VBA中不可用,而是使用CSTR。
尝试更改,
CurrentAudioFileName = audioFileName & A.ToString & ".mp3"
' This audio is embedded, not linked.
Set shp = .AddMediaObject2(CurrentAudioFileName, msoFalse, msoTrue, 350,10)
要,
If len(CSTR(A)) <= 2 then 'zero pad that position
CurrentAudioFileName = audioFileName & String(2 - Len(CSTR(A)), "0") & a & ".mp3"
else
CurrentAudioFileName = audioFileName & CSTR(A) & ".mp3"
end if
' This audio is embedded, not linked.
Set shp = sld.Shapes.AddMediaObject2(CurrentAudioFileName, msoFalse, msoTrue, 350,10)
在Shapes集合上调用方法时,我们正在引用当前幻灯片( sld here )!
此外,我们需要关闭for循环,而不是代码中的With块。
我们可以说End With
(或Next
,而不是Next sld
,这样您就可以看到该行的工作方式了。
最终,该子例程还需要对End语句进行一些修复
Sub AddMedia()
Dim A As Integer
A = 0
For Each sld In ActivePresentation.Slides
Dim shp As Shape
Dim CurrentAudioFileName As String
If Len(A) <= 2 Then 'zero pad that position
CurrentAudioFileName = audioFileName & String(2 - Len(A), "0") & A & ".mp3"
Else
If Len(CStr(A)) <= 2 Then 'zero pad that position
CurrentAudioFileName = audioFileName & String(2 - Len(CStr(A)), "0") & A & ".mp3"
Else
CurrentAudioFileName = audioFileName & CStr(A) & ".mp3"
End If
' This audio is embedded, not linked.
shp = sld.Shapes.AddMediaObject2(CurrentAudioFileName, msoFalse, msoTrue, 350, 10)
DisplayMediaInfo shp
End If
A = A + 1
Next
End Sub