การทำบัญชีข้อมูลต่างๆ ของไฟล์ *.Doc
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 342   1
URL.หัวข้อ / URL
การทำบัญชีข้อมูลต่างๆ ของไฟล์ *.Doc

มีคนถามคำถามนี้ไว้ที่ 1000 tips ดังนี้

จะให้ Word สรุปรายการหรือรายละเอียดของ Files ที่บันทึกได้อย่างไร ถ้าท่านเคยใช้ Word โดยที่ Record ข้อมูลของ File ใน Properties เช่น Tiles, Subject, Version, etc. ที่นี้พอเก็บ File ใน Directory ไปเยอะๆแล้ว จะหา File ตามหัวข้อเรื่องยากมาก ทำอย่างไร ถึงจะให้ Word เก็บข้อมูลจาก File ที่ได้บันทึกไว้ เช่น Filename, Title และ Subject ใน Folder ตามที่ต้องการ แล้วพิมพ์ลงใน New Document เหมือนๆกับจะพยายามทำบัญชีเอกสารน่ะครับ พยายามหาคำตอบเองนานมากไม่พบแสงสว่างซะที ขอบคุณล่วงหน้า

จากคุณ : ช้าง [6 พ.ย. 2544 - 23:16:18]

หลักการ
1. ใช้ FileSearch เข้ามาช่วยวิ่งหาไฟล์ในห้องเป้าหมาย แล้วเปิดไฟล์
2. ดึงข้อมูลเป้าหมายจากข้อมูลที่ต้องการจาก BuiltInDocumentProperties

ผมเลยได้ลองเปิด Help ของ Word 2000 ดู และได้ข้อสรุปออกมาดังนี้

Sub Test()
Dim fs As FileSearch, I As Integer
Set fs = Application.FileSearch
With fs
.LookIn = "C:\"
.FileName = "*.Doc"
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
Debug.Print "There were " & .FoundFiles.Count & _
" file(s) found."
' Open a blank document for the inputs
Documents.Add
For I = 1 To .FoundFiles.Count
Debug.Print .FoundFiles(I)
GetProp (.FoundFiles(I))
Next I
Else
Debug.Print "There were no files found."
End If
End With

End Sub

Function GetProp(strFileName As String)
Dim strTitle As String, strAuthor As String, strSubject As String
Dim rngDoc As Range
Documents.Open FileName:=strFileName, ReadOnly:=True
strTitle = ActiveDocument.BuiltInDocumentProperties("Title")
strSubject = ActiveDocument.BuiltInDocumentProperties("Subject")
strAuthor = ActiveDocument.BuiltInDocumentProperties("Author")
ActiveDocument.Close savechanges:=wdDoNotSaveChanges

Set rngDoc = ActiveDocument.Content
rngDoc.Collapse Direction:=wdCollapseEnd
With rngDoc
.InsertAfter "File Name = "
On Error Resume Next
.InsertAfter strFileName
.InsertParagraphAfter
.InsertAfter "Title = "
On Error Resume Next
.InsertAfter strTitle
.InsertParagraphAfter
.InsertAfter "Subject = "
On Error Resume Next
.InsertAfter strSubject
.InsertParagraphAfter
.InsertAfter "Author = "
On Error Resume Next
.InsertAfter strAuthor
' Add one line here
.InsertParagraphAfter
.InsertParagraphAfter
End With

End Function

คำแนะนำเพิ่มเติม
1. น่าจะเปิดไฟล์ Word ในโหมด Background ได้ ซึ่งน่าจะทำให้การทำงานได้ไวกว่า หรือว่าไง
2. FileSearch จะมีปัญหาในเรื่องของข้อจำกัดในจำนวนไฟล์ที่จะแสดงได้ ถ้ามีไฟล์มากๆ ควรจะใช้ Microsoft Scripting Runtime แทน

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R06482
   
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0784s