Storing User Data
There are times when you want the user to provide the default locations of folders and files that your code may need.
Other times the the code may place a shape in a particular position but the user finds this is never quite right for their job so they want to change the default position.
They may want to change many other defaults such color, font type, size etc.
You have several options.
1. You could allow them to enter the new data within your code but they may make a mistake and they can find this area confusing.
2. Have a text file that they can change and your code reads.
This means you issue both a cdr file and txt file and the txt file is located in a known location. It is slightly messy.
3. Create another page in the CorelDraw file and on this page are stored the variable values that the user can read and change.
Your code can then read the data on this page.
CorelDraw can have a table on the page and this can be read a bit like reading an Excel spreadsheet.
You can even set the new page as UnPrintable so that the whole document can be printed but without the data page.
4. Store data in the Meta Data of a file an area where author, Last Modified is stored. This is accessed via the file properties.
5. Store data as an OLE Property. This is not the same as 4 above.
Instead this meta data is hidden in the file and is accessed via a file called dsofile.dll
All file types I have looked at, including exe, jpg, cdr, txt, can contain this meta data.
Microsoft created the OLE File Property Reader 2.1 a 32 bit file. Despite its name it can also write data.
Microsoft no longer support the file nor do they provide a download which was called DsoFileSetup_KB224351_x86.exe
However, here is the Microsoft file.
This is a zip file but because this site does not allow zip files please alter the extension to zip.
Within the zip file is an exe file called DsoFileSetup_KB224351_x86.exe.
There is no need to install this exe file as I have extracted the dsoFile.dll for you.
However, if you wish you can look inside the exe file with WinZip and see its contents without installation.
Chip Pearson has a very good description of dsofile.dll
A 64 bit dsofile.dll file can be downloaded from https://www.keysolutions.com/blogs/kenyee.nsf/d6plinks/KKYE-79KRU6.
Before using the dsofile it must be registered.
Place the file dsofile.dll in C:\Windows\System32
To register a file the command prompt must be opened in administrator mode.
Right button the Command Prompt icon and select run as Administrator.
Navigate to C:\Windows\System32 in the Command Prompt.
Then type regsvr32 dsofile.dll and press Enter to register the dll.
If you do not run as Administrator you get error code 0x80070005.
Dsofile.dll accesses 2 storage areas, a Summary Properties and Custom Properties.
Summary Properties should be called Standard Properties as their do not summarise anything.
Summary Properties
These are properties permanently assigned to a file and cannot be deleted.
The only properties that can be have their value changed appear to be properties with string values.
The Summary Properties are:
|
Long and Boolean properties are read-only.
Strings can be a maximum of 249 characters long but this number includes the characters in the property name.
Change the Value of a Summary Property.
The only properties that can be have their value changed appear to be properties with string values.
Dim FileProps As DSOFile.OleDocumentProperties
Dim FileSummaryProps As DSOFile.SummaryProperties
Set FileProps = New DSOFile.OleDocumentProperties
FileProps.Open "D:\Users\Graeme\Desktop\Sample.txt", False, dsoOptionDefault
Set FileSummaryProps = FileProps.SummaryProperties
'Change the value of a Property.
'It appears that properties of type Long are all Read-Only.
FileSummaryProps.Author = "Me"
'It is important to close the FileProperties otherwise the FileProperties cannot be opened a second time unless the macro application closes.
'The FileProperties must be saved for the custom property to be saved.
'FileProps.Save
'FileProperties.Close([Save before Closing})
'FileProps.Close
'Or
FileProps.Close True
Custom Properties
In this area you create your own property name and give it a value.
Do not attempt to create a property if its name already exists as there will be an error.
The property type is automatically saved as a variant and cannot be altered.
The total string length of all Custom Properties in a file is no greater than 1,048,477.
For multiple Custom Properties the total string length of all properties decreases.
Create, Change, Delete and Read a Custom Property
Custom properties are all string type.
Dim FileProps As DSOFile.OleDocumentProperties
Dim FileCustProps As DSOFile.CustomProperties
Set FileProps = New DSOFile.OleDocumentProperties
'FileProps.Open(File Path,REadOnly True or False, File Open Options)
'File Open Options
'dsoOptionDefault
'dsoOptionDontAutoCreate
'dsoOptionOnlyOpenOLEFiles
'dsoOptionOpenReadOnlyIfNoWriteAccess
'dsoOptionUseMBCStringsForNewSets
FileProps.Open "D:\Users\Graeme\Desktop\Sample.txt", False, dsoOptionDefault
Set FileCustProps = FileProps.CustomProperties
'Add 2 new Custom Properties.
'FileCustProps.Add PropertyName, PropertyValue
FileCustProps.Add "Name", "Trevor"
FileCustPropsAdd "Name2", 8
'Change the value of a Custom Property.
'FileCustProps.Item(PropertyName).Value = PropertyValue
FileCustProps.Item("Name").Value = "John"
'Delete a Custom Property.
'FileCustProps.Item(PropertyName).Remove
FileCustProps.Item("Name").Remove
'It is important to close the FileProperties otherwise the FileProperties cannot be opened a second time unless the macro application closes.
'The FileProperties must be saved for the custom property to be saved.
'FileProps.Save
'FileProperties.Close([Save before Closing])
'FileProps.Close
'Or
FileProps.Close True
Examine Every Custom Property
Dim FileProps As DSOFile.OleDocumentProperties
Dim FileCustProps As DSOFile.CustomProperties
Dim FileCustProp As DSOFile.CustomProperty
Set FileProps = New DSOFile.OleDocumentProperties
FileProps.Open "D:\Users\Graeme\Desktop\Sample.txt", False, dsoOptionDefault
Set FileCustProps = FileProps.CustomProperties
For Each FileCustProp In FileCustProps
Debug.Print FileCustProp.Name, FileCustProp.Type, FileCustProp.Value
Next FileCustProp
FileProps.Close
6. An alternative is to store the data in an area called Object Data. Here objects are asssigned data.
This area is usually used to store an objects data such as color, weight, size etc.
It could be used to store data totally unrelated to the object that it is attached to.
It is not an ideal solution.
7. A good solution is to use CorelDraw's own database, called Properties, within each CorelDraw file.
These are used to store custom data with a document. They are only created and accessed via VBA.
With ActiveDocument
'Properties are addressed by a string and a long variable.
'In this example 3 properties called "TestData", 1, "TestData", 2 and "TestData", 3 are created
.Properties("TestData", 1) = "Hello" ' String
.Properties("TestData", 2) = 1 ' Integer
.Properties("TestData", 3) = 4/3 ' Double
'Now read each data item.
MsgBox .Properties("TestData", 1)
MsgBox .Properties("TestData", 2)
MsgBox .Properties("TestData", 3)
End With
To Delete Properties.
ActiveDocument.Properties.DeleteByIndex (2)
ActiveDocument.Properties.Delete "TestData", 2
'To delete all properties of type TestDataA.
ActiveDocument.Properties.DeleteAll "TestDataA"
It is possible to hide a graphic or any file type including a cdr file, within a CorelDraw file, so that the user cannot find it.
The secret is to convert the file, to hide, into a string. Then hide the string.
Excel has a method of hiding a worksheet called xlVeryHidden that hides a worksheet via VBA and it cannot be made visible again except via VBA.
It is a pity CorelDraw does not have something similar.
|