Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2018, 03:52 AM
EvaMads EvaMads is offline Get value from Content control from SharePoint document Windows 10 Get value from Content control from SharePoint document Office 2016
Novice
Get value from Content control from SharePoint document
 
Join Date: Sep 2018
Location: Copenhagen
Posts: 5
EvaMads is on a distinguished road
Default 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?
Attached Images
File Type: gif Custom XML mapping.gif (7.1 KB, 36 views)
Reply With Quote
  #2  
Old 09-24-2018, 03:48 PM
Guessed's Avatar
Guessed Guessed is offline Get value from Content control from SharePoint document Windows 10 Get value from Content control from SharePoint document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Once you have that information, you can then use the NamespaceURI to identify the right CustomXMLPart and the XPath to find the right Node in that CustomXMLPart.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 09-25-2018, 12:23 AM
EvaMads EvaMads is offline Get value from Content control from SharePoint document Windows 10 Get value from Content control from SharePoint document Office 2016
Novice
Get value from Content control from SharePoint document
 
Join Date: Sep 2018
Location: Copenhagen
Posts: 5
EvaMads is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 09-25-2018, 10:28 AM
gmaxey gmaxey is online now Get value from Content control from SharePoint document Windows 7 32bit Get value from Content control from SharePoint document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
Can you attach a sanitized sample document.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 09-26-2018, 07:06 AM
EvaMads EvaMads is offline Get value from Content control from SharePoint document Windows 10 Get value from Content control from SharePoint document Office 2016
Novice
Get value from Content control from SharePoint document
 
Join Date: Sep 2018
Location: Copenhagen
Posts: 5
EvaMads is on a distinguished road
Smile

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.
Attached Files
File Type: docx Example Contract.docx (57.2 KB, 13 views)
Reply With Quote
  #6  
Old 09-26-2018, 10:31 AM
gmaxey gmaxey is online now Get value from Content control from SharePoint document Windows 7 32bit Get value from Content control from SharePoint document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 09-27-2018, 01:15 AM
EvaMads EvaMads is offline Get value from Content control from SharePoint document Windows 10 Get value from Content control from SharePoint document Office 2016
Novice
Get value from Content control from SharePoint document
 
Join Date: Sep 2018
Location: Copenhagen
Posts: 5
EvaMads is on a distinguished road
Smile

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?
Reply With Quote
  #8  
Old 09-27-2018, 02:12 AM
gmaxey gmaxey is online now Get value from Content control from SharePoint document Windows 7 32bit Get value from Content control from SharePoint document Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 09-28-2018, 02:40 AM
EvaMads EvaMads is offline Get value from Content control from SharePoint document Windows 10 Get value from Content control from SharePoint document Office 2016
Novice
Get value from Content control from SharePoint document
 
Join Date: Sep 2018
Location: Copenhagen
Posts: 5
EvaMads is on a distinguished road
Smile

Thank you so much, Greg

You saved my day
Reply With Quote
Reply



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
Get value from Content control from SharePoint document Deleting a table from a content control -- preserving the content control BrainSlugs83 Word Tables 8 11-14-2013 03:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:59 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft