Tip

Create Individual Text Files With Values From A Multi-Value Keyword Field.

This generic script will read in all documents in a database. In a multi-value
keyword field that you select, it will create individual text files with
another value on the document that you select.
For example:
A document has an Email field, and a multi-value keyword field of mailing lists
that they would like to belong to. (Office Equipment, Athletics)
The script will read in the email address(me@me.com) and place that value in
two text documents, one called "Office Equipment.txt", and the other called
"Athletics.txt".
Each email address that has those values will be added to the correct list. The
text documents need not be created beforehand. The keyword list can have as
many values as needed.
Sub Click(Source As Button)
'Declarations
Dim nameofserver As String
Dim datab As String
Dim directory As String
Dim fieldname As String
Dim referencefield As String

'Getting user input for variables
nameofserver = Inputbox$("The name of the server where the database resides:")
datab = Inputbox$("The name of the database with the .nsf extension:")
fieldname = Inputbox$("The Keyword field name:")
referencefield = Inputbox$("The field name that will be added to the text
file:")
directory = Inputbox$("The directory where the text files will be created:")

'Calling the function that will create the lists
Call createLists(nameofserver, datab, directory, fieldname, referencefield)
End Sub


Sub createLists(nameofserver$, database$, directories$, fieldname$,
referencefield$)
'Declarations
Dim textList As Variant
Dim refField As Variant
Dim j As Integer, x As Integer
Dim doc As NotesDocument
Dim collection As NotesDocumentCollection
Dim pathName As String, fileName As String

'Testing to make sure database exists.
Dim db As New NotesDatabase( nameofserver$, database$ )
If Not(db.isopen) Then
Msgbox "Could not open database."
Exit Sub
End If

'Setting collection to all docs in the database.
Set collection = db.AllDocuments

'Getting directory information.
directory$ = Dir( directories$, 16 )
If directory$ = "" Then Mkdir(directories$)
Chdir(directories$)

'Testing for keyword field
For j = 1 To collection.Count
Set doc = collection.GetNthDocument(j)
If doc.HasItem( fieldname$ ) Then
textList = doc.GetItemValue(fieldname$)
Else
Msgbox "Keyword field does not exist."
Exit Sub
End If

'Testing for reference field
x=0
Forall m In textList
If doc.HasItem( referencefield$ ) Then
refField = doc.GetItemValue(referencefield$)
Else
Msgbox "Reference field does not exist."
Exit Sub
End If

'Calling the function to write the text files.
If textList(x) <> "" Then Call WriteToFile(textList(x), refField(0))
x=x+1
End Forall
Next

Msgbox("All lists have been created and/or updated.")
End Sub


Sub WriteToFile(textList$, refField$)
Dim fileNum As Integer
fileNum% = Freefile()
Dim textDoc As Variant
Dim parse_string As String
Dim result As String
Dim location As Integer
parse_string = textList$

If refField$ = "" Then Exit Sub

'Taking out illegal characters for text filenames.
result = CheckString(parse_string)
textDoc = result & ".txt"

'Writing to file
Open textDoc For Append As fileNum%
Print #fileNum%, refField$
Close fileNum%
End Sub

Function CheckString(parse) As String
'Thanks to members of Notes.net for helping me 'with this function.
Const badchars = ":/"
usr_string = ""
ret_string = ""
ch = ""
usr_string = parse
For i = 1 To Len(usr_string)
ch = Mid(usr_string, i, 1)
If Instr(1, badchars, ch) Then
Print Removing Illegal Characters
Else
ret_string = ret_string + ch
CheckString = ret_string
End If
Next i

End Function

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.