使用VBA powerpoint检查文件是否已存在

时间:2016-11-08 11:48:01

标签: vba macros powerpoint-vba

我想检查一个FileName是否已经存在。当我在本地桌面上测试时,我写的内容非常有效。但所有FileNames都保存在Sharepoint中。当我在那里测试它时,它不起作用!我总是收到错误消息:错误的文件名称或号码。 这就是我写的:

Private Sub CommandButton21_Click()

Dim NewFileName             As String
Dim OwnPathName             As String

oldWeekDay = Weekday(Now)

Select Case oldWeekDay

Case 1
    NewFileName = "PT PM Weekly " & Format(Date + 4, "yyyymmdd")
Case 2
    NewFileName = "PT PM Weekly " & Format(Date + 3, "yyyymmdd")
Case 3
    NewFileName = "PT PM Weekly " & Format(Date + 2, "yyyymmdd")
Case 4
    NewFileName = "PT PM Weekly " & Format(Date + 1, "yyyymmdd")
Case 5
    NewFileName = "PT PM Weekly " & Format(Date, "yyyymmdd")
Case 6
    NewFileName = "PT PM Weekly " & Format(Date + 6, "yyyymmdd")
Case 7
    NewFileName = "PT PM Weekly " & Format(Date + 5, "yyyymmdd")

End Select

OwnPathName = ActivePresentation.Path
FullFileName = OwnPathName & "\" & NewFileName

'for debug only (can remove it later)
'MsgBox OwnPathName
'MsgBox FullFileName


 Dim StrFile             As String
 Dim FileFound           As Boolean

 FileFound = False
 'look for all types of PowerPoint files only (filter only to PowerPoint files to save time)
 StrFile = Dir(OwnPathName & "\*pptm*")

 Do While Len(StrFile) > 0
 If InStr(StrFile, NewFileName) > 0 Then
    FileFound = True
    Exit Do
End If
StrFile = Dir
Loop

If FileFound Then
MsgBox "Modification already done"
Else
RemoveTextboxes
AllBlackAndDate
SaveAllPresentations (FullFileName)
End If

End Sub

我不明白为什么它不起作用! 能帮助我解决问题吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这可能有用,我一直用它来遍历我的电脑上的目录或我有权访问的任何网络。

F = Dir(StrFile, 7)  'sets f equal to the first file name
Do While F <> ""        'loops until there are no more files in the directory

 If InStr(StrFile, NewFileName) > 0 Then
    FileFound = True
    Exit Do
 End If  

  F = Dir            'set f equal to the next file name in the directory

Loop