I want to delete the first custom document property in Microsoft Word, but my script errs when the delete command is called from within the active document or from the Microsoft application.
tell application "Microsoft Word"
tell active document
every custom document property
set targetCustomDocProp to first item of (get every custom document property)
delete targetCustomDocProp --> errs with "Can’t get active document"
end tell
targetCustomDocProp
delete targetCustomDocProp --> errs with "Can’t get active document"
end tell
How do I correctly delete a custom document property in Microsoft Word?
In Word 2011, you can’t delete custom document properties (or regular ones). When I run your script, I get a -1708 error advising that the custom property doesn’t understand the ‘delete’ message.
While you may not be able to delete the property, you can set the name and value of the custom property to “”. Not sure if that will help.
tell application "Microsoft Word"
set ad to active document
set cdp to custom document properties of ad
set c1 to item 1 of cdp
properties of c1
--> {class:custom document property, name:"Client", value:"me", document property type:string, link to content:false, link source:missing value}
set value of c1 to "" -- do 'value' first, otherwise it may generate an error
set name of c1 to ""
properties of c1
--> {class:custom document property, name:"", value:"", document property type:string, link to content:false, link source:missing value}
end tell
If you delete all of the properties in the Properties dialogue, then you should see a missing value returned for the set cdp to… line.
Mockman, Thanks for your help, but unfortunately, setting c1’s value and name to “” results in a custom document property that has both a name and a value of “”. The custom document property remains in the list of the active document’s custom document properties, such as is returned with the following command.
tell application "Microsoft Word"
set cdp to custom document properties of active document
end tell
For unclear reasons, MS Word’s custom document property class, inheriting the elements and properties of the document property class, might be the cause of the problem. Perhaps MS Word developers never developed a method to remove a custom document property, even though they developed a method to add one.
Thanks Ionah, and I was able to implement your method. I would like to make this method more specific, to allow AppleScript to run a VB macro with arguments so as to delete a specific custom document property, using VBA.
If this is successful, then my overall goal will to create a loop to remove mutiple specified custom document properties in multiple word documents.
Sub deleteCDP(CDPName)
ActiveDocument.CustomDocumentProperties(CDPName).Delete
End Sub
I, however, cannot find a method using MW Word’s AppleScript library for assigning a parameter argument to the AppleScript, that will allow me to assign a variable value to the CDPName variable.
tell application "Microsoft Word" to run VB macro macro name "deleteCDP"
Do you have any ideas on how to pass an AppleScript variable to a VBA variable?
In your Applescript script, save the name of properties you want to delete in a text file.
Each name in a separate line, each line ending with a linefeed (this is imperative).
Then use the following macro:
Sub deleteCDP()
Dim filePointer As Integer
Dim lineContent As String
filePointer = FreeFile()
Open "/Users/username/Desktop/deleteCDP.txt" For Input As filePointer
While Not EOF(filePointer)
Line Input #filePointer, lineContent
'MsgBox lineContent 'uncomment this line if you want to display the name
ActiveDocument.CustomDocumentProperties(lineContent).Delete
Wend
End Sub
Ha. It looks like I found a way to do this (run a VBA script from file) - with a help from ChatGPT. Imagine that.
After couple hours of debugging and tests (considering my near zero knowledge of VB), this macro seems to work. The script after all is from ChatGPT nearly as is:
Sub RunVBAFromFile()
Dim filePath As String
Dim scriptContent As String
Dim tempModule As Object
' Set the file path to the location of your VBA script file
filePath = "path-to-vba-script"
' Read the content of the VBA script file
Open filePath For Input As #1
scriptContent = Input$(LOF(1), 1)
Close #1
' Create a temporary module in Word
Set tempModule = ActiveDocument.VBProject.VBComponents.Add(1)
' Add the script content to the temporary module
tempModule.CodeModule.AddFromString scriptContent
' Run the VBA script
Application.Run tempModule.Name & ".macroNameFromScriptFile"
' Remove the temporary module
ActiveDocument.VBProject.VBComponents.Remove tempModule
End Sub
“Trust access to the VBA project object model” should be enabled in Preferences > Security.
The reason I wanted this functionality is to find a replacement for an AppleScript command that doesn’t work in Word (due to a bug) while its VBA analog does work.
Thanks again for posting your solution. This helped me finally discover a workaround for this AppleScript bug.
Sub deleteCDP()
Dim filePointer As Integer
Dim lineContent As String
filePointer = FreeFile()
Open “/Users/username/Desktop/deleteCDP.txt” For Input As filePointer
While Not EOF(filePointer)
Line Input #filePointer, lineContent
'MsgBox lineContent 'uncomment this line if you want to display the name
ActiveDocument.CustomDocumentProperties(lineContent).Delete
Wend
End Sub
which I was able to run successfully from the AppleScript.
Leo_r, I was unable to run your script in VBA. Perhaps I did not understand your method correctly, and perhaps you can correct me.
I created two text files on Desktop:
A paragraph return delimited text containing the custom document property names that I want to delete, such as recommended by Ionah. In my case the text of that document was
Occupation
Age
A text file containing the VBA script that Ionah recommended above.
I then created a VBA in MS Word such as you recommended, and defined the FilePath variable as “/Users/alan/Desktop/VBAScript.txt”, in effect creating the following script.
Sub deleteCDP()
Sub RunVBAFromFile()
Dim filePath As String
Dim scriptContent As String
Dim tempModule As Object
’ Set the file path to the location of your VBA script file
filePath = “/Users/alan/Desktop/VBAScript.txt”
’ Read the content of the VBA script file
Open filePath For Input As #1
scriptContent = Input$(LOF(1), 1)
Close #1
’ Create a temporary module in Word
Set tempModule = ActiveDocument.VBProject.VBComponents.Add(1)
’ Add the script content to the temporary module
tempModule.CodeModule.AddFromString scriptContent
’ Run the VBA script
Application.Run tempModule.Name & “.macroNameFromScriptFile”
’ Remove the temporary module
ActiveDocument.VBProject.VBComponents.Remove tempModule
End Sub
When I ran this last script in Word’s VBA editor, the VBA erred with a dialog “Can’t run the specified macro” on highlighted the the culprit VBA code as
Not conversant in VBA, I am not sure how to correct this. If you know how, I would appreciate your insight. If you could share your scripts and text files that contain the data requested by the VBA script, I might be able to model my script based upon yours.
Well you don’t really need my script. The solution from @ionah is all you need in your case. Sorry if I confused you with the deflection from your original question.
It’s just @ionah’s solution prompted me to investigate a possible workaround for my own issue, which I was looking for for as long time. It’s not, once again, related to your original question.