I have the following code which is supposed to insert content controls for category and title of the document.
Sub insert_Header()
' Declare the CCs that we use later on
Dim titleCC As contentControl
Dim idCC As contentControl
Dim rng As range
' Set range to the header of the first section
Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).range
' Insert the Category content control
Set idCC = ActiveDocument.ContentControls.Add(wdContentControlText, rng)
With idCC
.Title = "Category"
.xmlMapping.SetMapping "/ns1:coreProperties[1]/ns1:category[1]" ' Correct mapping for Category
End With
' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1
' Insert a _
rng.Text = "_"
' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1
' Insert Title CC
Set titleCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
With titleCC
.Title = "doc. Name"
.xmlMapping.SetMapping "/ns1:coreProperties[1]/ns0:title[1]"
End With
End Sub
Now the issue is that while the category is inserted just fine and linked to the built in doc props, the title just throws a run time error '6324': Reference to undeclared namespace prefix: 'ns1'.
I got these XPaths by inserting content controls into the document via the quick parts menu and converting it to a zip file. I then went into the XML files of the zip to copy paste the XPaths. Is there a different way of finding out XPaths?
I have the following code which is supposed to insert content controls for category and title of the document.
Sub insert_Header()
' Declare the CCs that we use later on
Dim titleCC As contentControl
Dim idCC As contentControl
Dim rng As range
' Set range to the header of the first section
Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).range
' Insert the Category content control
Set idCC = ActiveDocument.ContentControls.Add(wdContentControlText, rng)
With idCC
.Title = "Category"
.xmlMapping.SetMapping "/ns1:coreProperties[1]/ns1:category[1]" ' Correct mapping for Category
End With
' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1
' Insert a _
rng.Text = "_"
' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1
' Insert Title CC
Set titleCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
With titleCC
.Title = "doc. Name"
.xmlMapping.SetMapping "/ns1:coreProperties[1]/ns0:title[1]"
End With
End Sub
Now the issue is that while the category is inserted just fine and linked to the built in doc props, the title just throws a run time error '6324': Reference to undeclared namespace prefix: 'ns1'.
I got these XPaths by inserting content controls into the document via the quick parts menu and converting it to a zip file. I then went into the XML files of the zip to copy paste the XPaths. Is there a different way of finding out XPaths?
Share Improve this question asked Mar 21 at 13:51 MDKMDK 274 bronze badges 3 |1 Answer
Reset to default 0If you work with xml including namespaces you need prefix and uri to register the right namespace to use it in xpath. The ns0 and ns1 are only placeholders for missing registration in your code.
/ns1:coreProperties[1]/ns1:category[1] should be
/cp:coreProperties/cp:category
/ns1:coreProperties[1]/ns0:title[1] should be
/cp:coreProperties/dc:title
===== EXAMPE =====
Dim customXML As CustomXMLPart
Dim xmlData As String
' Loop through all Custom XML Parts in the Word document
For Each customXML In ActiveDocument.CustomXMLParts
' Print XML content to Immediate Window (Ctrl + G)
xmlData = customXML.XML
Debug.Print xmlData
Next customXML
ns1
, you have to configure what namespace that prefix is for. Think of namespace-prefixes like a variable (it can be named anything) and the actual namespace that it's referencing is the value (needs to be set) otherwise is "null". – Mads Hansen Commented Mar 21 at 13:57pyxml2xpath
python package/command or xml2xpath Bash utility. Both use libxml2 in the background. – LMC Commented Mar 21 at 13:58SetMappingByNode
and notSetMapping
– Tim Williams Commented Mar 21 at 15:45