2016-11-08 139 views
0

我想檢查一個文件名是否已經存在。當我在本地桌面上測試它時,我所寫的內容完美無瑕。但是所有的FileNames都保存在Sharepoint中。當我在那裏測試時,它不起作用!我總是得到錯誤信息:壞文件名或數字。 這是我寫的:使用VBA powerpoint檢查文件是否已經存在

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 

我不明白爲什麼這是行不通的! 你能幫我解決問題嗎? 在此先感謝!

+0

My OwnpathName = https://sec-ishare.infineon.com/sites/MC_PM/Shared%20Documents/09_Reporting/PT/2016 – Zigouma

回答

0

我認爲這可能會工作,我一直使用它來循環訪問我的PC或任何我可以訪問的網絡上的目錄。

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