#1
|
|||
|
|||
Get value from Content control from SharePoint document
I have a Word-document which is created in SharePoint. In VBA I get the value from various Content Controls like this:
aProjName = ActiveDocument.ContentTypeProperties("Project name").Value No problem – I works like a dream. With the XML Mapping Pane on the Developer-tab, I can see the Content Controls are in http://schemas.microsoft.com/office/...ata/properties The problem is to get the value of a Content Control from http://schemas.microsoft.com/office/...ta/ContentType I can right-click on the name and insert the content control in the document, but I cannot figure out how to get the value with VBA. Can anyone help? |
#2
|
||||
|
||||
If you place a mapped Content Control into your document then you can interrogate that CC to find out its relevant details including the CustomXmlPart and xPath location.
Code:
Sub GetXPath() Dim aCC As ContentControl For Each aCC In Selection.Range.ContentControls Debug.Print aCC.Title, aCC.XMLMapping.XPath, aCC.XMLMapping.CustomXMLPart.NamespaceURI, aCC.XMLMapping.CustomXMLNode.BaseName Next aCC End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thank You Guessed
Running Your macro gave this result: aCC.Title returned: blank aCC.XMLMapping.XPath returned: /ns0:contentTypeSchema[1]/@nsl:contentTypeName aCCXMLMapping.CumstomXMLPart.NamespaceURI returned: http://schemas.microsoft.com/office/...ta/contentType aCC.XMLMapping.CustomXMLNode.Basename returned: contentTypeName But I cannot figure out, how to find the right Node i the CustomXMLPart. Could You please help a bit more? |
#4
|
|||
|
|||
Not sure I follow what exactly you are after but maybe:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/25/2018 Dim oCC As ContentControl Dim oNode As CustomXMLNode For Each oCC In ActiveDocument.ContentControls If oCC.XMLMapping.IsMapped Then Set oNode = oCC.XMLMapping.CustomXMLNode.SelectSingleNode(oCC.XMLMapping.XPath) MsgBox oNode.Text Set oNode = Nothing End If Next oCC lbl_Exit: Exit Sub End Sub |
#5
|
|||
|
|||
Thanks for Your reply.
In the XML Mapping Pane select http://schemas.microsoft.com/office/...ta/contentType You can see the contentType @contentTypeName. I want to get the value (which in the sample-document is "Contract") of this content type. |
#6
|
|||
|
|||
Your content control in the document showing "Contract" is a richtext content control. The "Value" stored in the customXMLPart is the entire OpenOfficeXMLContent package associated with a word document which contains the single word "Contract."
You can see this if you delete the content control and insert a new mapped "Plain Text" CC to that node. You will end up with several pages of XML gobbledeegook. You can extract the text content from that using: Code:
Sub ScratchMacro2() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/25/2018 Dim oCC As ContentControl Dim oNode As CustomXMLNode Dim strContent As String Dim arrParts() As String For Each oCC In ActiveDocument.ContentControls 'You only have one. If oCC.XMLMapping.IsMapped Then Set oNode = oCC.XMLMapping.CustomXMLNode.SelectSingleNode(oCC.XMLMapping.XPath) strContent = oNode.Text arrParts = Split(strContent, "w:t>") strContent = Left(arrParts(1), Len(arrParts(1)) - 2) MsgBox strContent Set oNode = Nothing End If Next oCC lbl_Exit: Exit Sub End Sub |
#7
|
|||
|
|||
Thank You, Greg – it works 😊
Would it be possible to get the value of the control, if it is not inserted in the document as a Content Control? |
#8
|
|||
|
|||
If the "control" is not inserted in the document then it is not a control. If you want to
simply get the "content" of the OpenOfficeXMLFileFormat document defined by the value of the node then: Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/25/2018 Dim oXMLPart As CustomXMLPart Dim oNode As CustomXMLNode Dim strContent As String Dim arrParts() As String Set oXMLPart = ActiveDocument.CustomXMLParts.SelectByNamespace("http://schemas.microsoft.com/office/...tType").Item(1) Set oNode = oXMLPart.SelectSingleNode("/ns0:contentTypeSchema[1]/@ns1:contentTypeName") strContent = oNode.Text arrParts = Split(strContent, "w:t>") strContent = Left(arrParts(1), Len(arrParts(1)) - 2) MsgBox strContent Set oNode = Nothing Set oXMLPart = Nothing lbl_Exit: Exit Sub End Sub |
#9
|
|||
|
|||
Thank you so much, Greg
You saved my day |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Content Control content deleted when re-uploading to SharePoint | Peterson | Word | 5 | 06-27-2018 08:13 PM |
Combo Box Content Control: How to update the list throughout the whole document? | deepak_fer | Word | 9 | 03-18-2018 04:10 AM |
Gallery Content control and protect document | DarrenOakey | Word | 2 | 08-13-2015 04:56 AM |
How to protect all parts of a document not in a content control? | XmisterIS | Word | 2 | 05-20-2014 04:59 AM |
Deleting a table from a content control -- preserving the content control | BrainSlugs83 | Word Tables | 8 | 11-14-2013 03:06 AM |