Delete Custom Document Property in MS Word

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?

What version of 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

Yeah, my guess is that you can’t delete them using applescript.

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.

This method exists. But as often, it’s not maintained in Applescript.
You could use VBA to achieve this.

Save this macro in your “Normal” document:

Sub deleteCDP()
 For Each cdp In ActiveDocument.CustomDocumentProperties
    cdp.Delete
   Next cdp
End Sub

Then use this Applescript to call it:

tell application "Microsoft Word" to run VB macro macro name "deleteCDP"
2 Likes

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?

EDIT: This post contains some info that’s not entirely correct. See @ionah post below.

You can’t (unless I’m unaware of some super-advanced technique to do so).

You can only call existing Word macros by name.

Several years ago, Microsoft helpfully removed the ability to call VB scripts directly from AppleScript.

I don’t remember the exact syntax, but you could do something like this:

do VB script "your VB script string here"

Not anymore.

The excuse to remove this option was some pseudo-security mumbo-jumbo related to either sandboxing or something else.

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

Thanks for posting this solution. I wasn’t aware of this.

I wonder then if it’s possible to pass an entire VB script to an existing Word macro this way?

My VB knowledge is very minimal.

For example, is it possible to create a Word macro that would do something like this:

-Read VB script from a file,
-Run this script?

Not sure, but I think it’s not possible using pure VBA.

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.

Is it working on your system?
If yes, what version of Word & macOS are you using?

It does work. Latest Word 16.73 on macOS 13.4.

Ionah, Thanks for your VBA script,

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:

  1. 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

  1. 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

Application.Run tempModule.Name & “.macroNameFromScriptFile”

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.

@akim, I have the same issue here. Last versions of Word and Monterey.

@leo_r, Is there another setting we could have missed?

I’m wondering: could the license type be the problem?
I’m using a family subscription purchased on the Microsoft site.

If I understand your issue correctly, then the script I posted is intended to run a VBA script, not just read a string of values.

So your text file should contain at least one macro, for example:

Sub TestBox()
MsgBox "Test MsgBox"
End Sub

Then insert the macro name in the appropriate line in the script I posted:

Application.Run tempModule.Name & “.TestBox”

Does it answer your question?

Thanks Leo.
I thought “.macroNameFromScriptFile” was a command!