Delete Custom Document Property in MS Word

I have attempted another approach using AppleScript to loop through the names of Word’s custom document properties and then deleting them by calling a VBA code.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

#	Choose one or several custom custom document properties in Word's  active document
tell application "Microsoft Word"
	set cdp to name of custom document properties of active document
end tell
set cdpList to (choose from list cdp with multiple selections allowed)

my DeleteCustomDocumentProprerty:cdpList
on DeleteCustomDocumentProprerty:cdpList
	
	tell application "Microsoft Word"
		
		#	Count  initial number  of custom document properties to insert into  dialog text at end of script
		set cdpInitialCount to count of custom document properties of active document
		
		repeat with cdp in cdpList
			
			#	Modify or create a Word variable
			#	based upon oliver_osswald at https://answers.microsoft.com/en-us/msoffice/forum/all/can-i-pass-parameters-to-a-word-2011-macro-from/bfd3fe1d-317e-4d96-85f3-1758638e2653
			if exists variable "CDPName" in active document then
				set variable value of variable "CDPName" in active document to cdp
			else
				make new variable at active document ¬
					with properties {name:"CDPName", variable value:cdp}
			end if
			
			
			# Insert  the following VB macro, beginning at Sub and ending at End Sub,  into Word's VBA and save in Normal Template
			# based upon  ionah's suggestion on this page https://www.macscripter.net/t/delete-custom-document-property-in-ms-word/74728/19 	
			
			(*
		
			Sub deleteCDPfromAS()
			Dim CDPValue As String
			
			CDPValue = ActiveDocument.Variables("CDPName").Value
			ActiveDocument.CustomDocumentProperties(CDPValue).Delete
			'The following line does not appear to function, ie to delete the variable CDPName
			ActiveDocument.Variables("CDPName").Delete
			End Sub
			
			*)
			run VB macro macro name "NewMacros.deleteCDPfromAS"
		end repeat
		
		set cdpFinalCount to count of custom document properties of active document
	end tell
	
	# Create a dialog display to summarize the action of this Applescript
	set text item delimiters to " and "
	set cdpString to cdpList as string
	set text item delimiters to ""
	display dialog cdpString & " have been deleted." & return & return & "Custom Document Property Count has decreased from " & cdpInitialCount & " to " & cdpFinalCount with title "Custom Document Properties Deleted"
end DeleteCustomDocumentProprerty:

Good catch @akim!

You say you can’t delete the variable from within the macro.
As you’re able to create it in AS, could you try to delete it there?

Ionah, following your last suggestion, I rewrote my previous AppleScript, to delete the temporary Word variable “CDPName”, and removed the VBA command to perform the same, as its execution appeared dubious. The AppleScript is now a bit more concise in that it deletes the Word variable “CDPName”, at its beginning rather than modifying it, then creates the variable, and at the end of the script deletes it again, as its presence is no longer necessary.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

#	Choose one or several custom custom document properties in Word's  active document.
tell application "Microsoft Word"
	set cdp to name of custom document properties of active document
end tell
set cdpList to (choose from list cdp with multiple selections allowed)

my DeleteCustomDocumentProprerty:cdpList
on DeleteCustomDocumentProprerty:cdpList
	
	tell application "Microsoft Word"
		
		#	Count initial number of custom document properties to insert into  dialog text at end of script
		set cdpInitialCount to count of custom document properties of active document
		
		repeat with cdp in cdpList
			
			#	Delete variable "CDPName" , should it exist in the active document
			delete variable "CDPName" in active document
			
			#	based upon oliver_osswald at https://answers.microsoft.com/en-us/msoffice/forum/all/can-i-pass-parameters-to-a-word-2011-macro-from/bfd3fe1d-317e-4d96-85f3-1758638e2653
			make new variable at active document ¬
				with properties {name:"CDPName", variable value:cdp}
			
			# Insert  the following VB macro, beginning at Sub and ending at End Sub,  into Word's VBA and save in Normal Template
			# based upon  ionah's suggestion at this page https://www.macscripter.net/t/delete-custom-document-property-in-ms-word/74728/19 	
			
			(*
		
			Sub deleteCDPfromAS()
			Dim CDPValue As String
			
			CDPValue = ActiveDocument.Variables("CDPName").Value
			ActiveDocument.CustomDocumentProperties(CDPValue).Delete
			
			End Sub
			
			*)
			run VB macro macro name "NewMacros.deleteCDPfromAS"
			
			#	Delete the variable  "CDPName" from the active Word document, as its purpose was only  temporary to pass an AppleScript varible to the VB macro "deleteCDPfromAS" in Word's VBA "NewMacros" module.
			delete variable "CDPName" in active document
			
		end repeat

			#	Count final number of custom document properties to insert into dialog text at end of script.

		set cdpFinalCount to count of custom document properties of active document
	end tell
	
	# Create a dialog display to summarize the action of this Applescript
	set text item delimiters to " and "
	set cdpString to cdpList as string
	set text item delimiters to ""
	display dialog cdpString & " have been deleted." & return & return & "Custom Document Property Count has decreased from " & cdpInitialCount & " to " & cdpFinalCount with title "Custom Document Properties Deleted"
end DeleteCustomDocumentProprerty: