This example shows how to use the TXTextControl.LoadSaveAttribute property to set and get document properties for rich text formats such as RTF, DOC and DOCX in a Text Control application.
Document properties describe or identify a document through meta-data, like title, subject or author name.
The code shown here is contained in the Samples\VB6\DocProperties sample source directory.
The LoadSaveAttribute property can be used to set and get various attributes when loading and saving a document.
In this sample project, the use of the following document properties is shown:
When the program is started, default values are set for each attribute, including the creation and modification dates, and shown in the text boxes. The user is now able to edit each value except the creation and modification dates, which are set automatically. After the Save button has been clicked, the document properties are retrieved from the text boxes and assigned to each attribute. The creation and modification dates are set automatically to the current system date and time, however the creation date is only set, if it has not been retrieved previously so it will not be overwritten with the current date and time every time the document is saved.
[Visual Basic 6] 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
When loading a document, the document's attributes are retrieved from the LoadSaveAttribute property and assigned to the text boxes.
[Visual Basic 6] 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
There are 2 helper functions used in this sample to convert between TAB and comma separated strings, GetKeyWords and SetKeyWords. Both functions expect a string and return the modified string.
GetKeyWords replaces all TAB characters in a given string with commas, SetKeyWords replaces all commas with TAB characters.