DIR函數只返回文件名,不包含完整路徑
所以請重新修改為以下代碼:
Sub abc()
Dim App, WrdDoc, MyPath, MyFile, BM, Str
Mypath = "文件實際路徑" '請修改實際儲存路徑!
Set App = CreateObject("Word.Application") '用Set關鍵字創建Word應用成序對象!
MyFile = Dir(Mypath & "\*.doc") ' 獲得第壹個WORD文檔
do while MyFile <> "" ' 遍歷Mypath下面的所有WORD文檔
App.Visible = True
Set WrdDoc = App.Documents.Open(Mypath & "\" & MyFile) '打開這個Word文件!
for each BM in WrdDoc.Bookmarks ' 遍歷文檔中的所有書簽
Str = BM.Range ' 讀取書簽內容
next BM
WrdDoc.Close ' 關閉文件
MyFile = Dir ' 下壹個WORD文檔
Loop
Set App = Nothing
End Sub