Document Properties
Document properties describe or identify a document through metadata, such as its title, its subject or the name of its author. In rich text formats such as RTF, DOC and DOCX, TXTextControl sets and reads them with the LoadSaveAttribute property, and a Visual Basic sample shows how a form edits them, saves them with a document and reads them back.
The code shown here is in the Samples\VB6\DocProperties sample source directory.
How LoadSaveAttribute Works
LoadSaveAttribute sets and gets various attributes when a document is loaded or saved. You pass the attribute as a constant: set the value before you call Save to store it in the document, and read it after you call Load to get the value the document contains.
The sample uses these document properties:
| Constant | Document property |
|---|---|
txDocTitle | Title |
txDocSubject | Subject |
txAuthor | Author |
txDocKeywords | Keywords |
txCreationDate | Creation date |
txLastModificationDate | Date of last modification |
The constants of LoadSaveAttribute list all attributes.
Save the Document Properties
When the program starts, it sets a default value for each attribute, including the creation and modification dates, and shows the values in text boxes. The user can edit every value except the two dates, which are set automatically.
When the user clicks the Save button, the document properties are taken from the text boxes and assigned to the attributes:
Private Sub SaveButton_Click()
' Retrieve each attribute from the corresponding text box
With TXTextControl1
.LoadSaveAttribute(txDocTitle) = Text_Title.Text
.LoadSaveAttribute(txDocSubject) = Text_Subject.Text
.LoadSaveAttribute(txAuthor) = Text_Author.Text
.LoadSaveAttribute(txLastModificationDate) = DateTime.Now
.LoadSaveAttribute(txDocKeywords) = SetKeyWords(Text_Keywords.Text)
' Check if a creation date had already been through a load operation.
' If a creation date has not been set, use the current date and time.
If (Text_CreationDate.Text = "") Then .LoadSaveAttribute(txCreationDate) = DateTime.Now
.Save App.Path + "\Document_Properties.rtf", 0, 5, False
End With
' Show message box to the user to make sure the Load button is clicked next.
MsgBox "Document properties have been saved!" + vbCr + "Please click on the Load button.", vbOKOnly, "Document properties saved"
' Clear the text boxes once the document has been saved.
Text_Title.Text = ""
Text_Subject.Text = ""
Text_Author.Text = ""
Text_Keywords.Text = ""
Text_ModDate.Text = ""
Text_CreationDate.Text = ""
' Reset and clear the TXTextControl
TXTextControl1.ResetContents
End Sub
The code works as follows:
- Each text box value is assigned to its
LoadSaveAttributeconstant. txLastModificationDateis set to the current system date and time on every save.txCreationDateis set to the current date and time only if no creation date was retrieved before, by a load operation. This way it isn't overwritten each time the document is saved.Savewrites the document as RTF to Document_Properties.rtf in the application directory.- A message box tells the user to click the Load button next. Then the text boxes are cleared, and ResetContents clears
TXTextControl.
Load the Document Properties
When the user loads the document, its attributes are read from LoadSaveAttribute and assigned to the text boxes:
Private Sub LoadButton_Click()
' Retrieve each attribute from the loaded document
With TXTextControl1
.Load App.Path + "\Document_Properties.rtf", 0, 5, False
Text_Title.Text = .LoadSaveAttribute(txDocTitle)
Text_Subject.Text = .LoadSaveAttribute(txDocSubject)
Text_Author.Text = .LoadSaveAttribute(txAuthor)
Text_Keywords.Text = GetKeyWords(.LoadSaveAttribute(txDocKeywords))
Text_ModDate.Text = .LoadSaveAttribute(txLastModificationDate)
Text_CreationDate.Text = .LoadSaveAttribute(txCreationDate)
End With
End Sub
Convert the Keywords
Two helper functions of the sample convert between TAB-separated and comma-separated strings. Both take a string and return the converted string:
GetKeyWordsreplaces all TAB characters of a string with commas. The sample uses it after loading, to show the keywords in a text box.SetKeyWordsreplaces all commas with TAB characters. The sample uses it before saving, to assign the keywords totxDocKeywords.