I'm having some trouble setting document properties using VBA in Word 2010.
I have a document containing several Heading 1
sections and I use a macro to extract a selected section (along with it's contents) and paste it to a new document.
This part works fine, but at the end I need to set several document properties, but none of them are being set.
I'm trying to set both built-in and custom properties, but for the purpose of this question I'd like to set title, subject and, category.
I've created a function to set the properties I desire (as below), and VBA is throwing no error (even when I remove error handling in the function).
Does anybody know what I am doing wrong?
Here is a brief summary of what the function should do, but the full function is below should you find it easier to check that -
default
property
PropertyTypeUsed
variable to default
custom
property
PropertyTypeUsed
variable to custom
PropertyTypeUsed
variable to custom
default
property should have been set
custom
property should have been set
Function UpdateDocumentProperty(ByRef doc As Document, _
ByVal propertyName As String, _
ByVal propertyValue As Variant, _
Optional ByVal propertyType As Office.MsoDocProperties = 4)
'** Set the result to 'False' by default '*
Dim result As Boolean
result = False
'** A property to hold whether or not the property used is default or custom *'
Dim propertyTypeUsed As String
'** Check to see if the document property already exists *'
If PropertyExists(doc, propertyName) Then ' A default property exists, so use that
doc.BuiltInDocumentProperties(propertyName).value = propertyValue
propertyTypeUsed = "default"
ElseIf PropertyExists(doc, propertyName, "custom") Then ' A custom property exists, so use that
doc.CustomDocumentProperties(propertyName).value = propertyValue
propertyTypeUsed = "custom"
Else ' No property exists, so create a custom property
doc.CustomDocumentProperties.Add _
name:=propertyName, _
LinkToContent:=False, _
Type:=propertyType, _
value:=propertyValue
propertyTypeUsed = "custom"
End If
'** Check whether or not the value has actually been set *'
On Error Resume Next
If propertyTypeUsed = "default" Then
result = (doc.BuiltInDocumentProperties(propertyName).value = propertyValue)
ElseIf propertyTypeUsed = "custom" Then
result = (doc.CustomDocumentProperties(propertyName).value = propertyValue)
End If
On Error GoTo 0
UpdateDocumentProperty = result
End Function
The full code for this project can be found in two Paste Bins -
I'm not sure if it's possible to get the code for actually creating the form (short of exporting it, but I have no where to put it), but in any case it's very simple -
frmChooseDocument
lblChooseDocument
(Which New Starter document would you like to export?)comChooseDocument
btnCancel
btnOK
(Initially disabled)In reality I'm using the document that houses this code as a 'master' document for new startes, containing detailed instructions on how to use variouse applications.
The code itself looks for Heading 1
formatted text within the document and adds them to the combobox in the form, allowing the user to select a section to export. A new document is then created and saved as a PDF.
As suggested in the comments, I have checked that the type of value being set matches that of the value being passed to the function and it does.
In the case of all 3 properties described above, both the value that I am passing and the property as stored against the document are of type string
.
I've added a couple of lines to output the type and value where I am setting the result and all looks well, but obviously it is not!
Debug.Print "My value: (" & TypeName(propertyValue) & ")" & propertyValue
Debug.Print "Stored property: (" & TypeName(doc.BuiltInDocumentProperties(propertyName).value) & ")" & doc.BuiltInDocumentProperties(propertyName).value
Here is the output -
My value: (String)New Starter Guide - Novell
Stored property: (String)New Starter Guide - Novell
My value: (String)New starter guide
Stored property: (String)New starter guide
My value: (String)new starters, guide, help
Stored property: (String)new starters, guide, help
I managed to set my word document title by saving the document after changing the property. I set the "Saved" property to false first to make sure that Word registers the change in state.
Function ChangeDocumentProperty(doc As Document, sProperty As String, sNewValue As String)
Debug.Print "Initial Property, Value: " & sProperty & ", " & doc.BuiltInDocumentProperties(sProperty)
doc.BuiltInDocumentProperties(sProperty) = sNewValue
doc.Saved = False
doc.Save
ChangeDocumentProperty = (doc.Saved = True And doc.BuiltInDocumentProperties(sProperty) = sNewValue)
Debug.Print "Final Property, Value: " & sProperty & ", " & doc.BuiltInDocumentProperties(sProperty)
End Function
Immediate Window:
? ThisDocument.ChangeDocumentProperty(ThisDocument, "Title", "Report Definitions")
Initial Property, Value: Title, Report Glossary
Final Property, Value: Title, Report Definitions
True
Permanent object properties cannot be set by functions. In other words, VBA does not allow functions to have side effects that persist after the function is finished running.
Re-write the function as a Sub and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With