VBA:如何让这段代码只写2位数的文件名?

时间:2011-11-07 01:15:08

标签: vba powerpoint powerpoint-vba

我使用以下VBA代码将PPT幻灯片导出到图像。问题是,对于幻灯片1到9,文件名只有一个字符(例如:“1.png”)。我需要前九张幻灯片的文件名中包含两个字符(例如:“01.png”)以匹配后面的幻灯片(例如:“10.png”)。

我怎样才能实现这一目标?

提前致谢!

Sub Save_PowerPoint_Slide_as_Images()

Dim sImagePath As String
Dim sImageName As String
Dim oSlide As Slide '* Slide Object
Dim lScaleWidth As Long '* Scale Width
Dim lScaleHeight As Long '* Scale Height
On Error GoTo Err_ImageSave

For Each oSlide In ActivePresentation.Slides
sImageName = oSlide.SlideNumber & ".png"
oSlide.Export sImagePath & sImageName, "PNG"
Next oSlide

Err_ImageSave:
If Err <> 0 Then
MsgBox Err.Description
End If
End Sub

Function sImagePath() As String
sImagePath = ActivePresentaion.Path
End Function

1 个答案:

答案 0 :(得分:3)

尝试使用Format

sImageName = Format(oSlide.SlideNumber, "00") & ".png"