這裏有一個快速&葷一來說明的原則....讓文本文件被稱爲「accounts.txt」具有以下內容:
Account 1
item 1
item 2
item 3
=========
Account 2
item 4
item 5
=========
Account 3
item 6
item 7
=========
現在讓我們看一些非常基本的VBA代碼利用的Open As
和Line Input
和循環結構....
Sub GetAccountFromTextFile(FileName As String, Accnt As String)
Dim MyLine As String, State As String
Open FileName For Input As #1
State = "Searching" ' we could make this much simpler but
' want to illustrate the different stati
' the loop is reaching
Do While Not (EOF(1) Or State = "End")
Line Input #1, MyLine ' read next line from text file
' now process as function of
' content and current state
If State = "Reading" And MyLine = "=========" Then
State = "End"
ElseIf MyLine = "Account " & Accnt Then
Selection.InsertAfter "Account " & Accnt & vbCrLf
State = "Reading"
ElseIf State = "Reading" Then
Selection.InsertAfter MyLine & vbCrLf
End If
Loop
Close #1
End Sub
您可以通過另一個子稱之爲
如何
Account 1
item 1
item 2
item 3
Account 3
item 6
item 7
Account 2
item 4
item 5
現在你可以在你的主子很有創意(也許是對話形式):
啓動測試()在一個Word文檔的任何地方,而下面將要粘貼到文檔要獲取文件名和帳號,然後才能調用帳號獲取器,並且需要修改用於查找帳號和分離模式的條件。不是很複雜,但應該足以讓你去。
祝你好運 MikeD