UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
> Accessing Content Of Contentcontrols Programmatically, Office 2013    
 
   
MadPiet
post May 14 2019, 01:07 AM
Post#1



Posts: 3,127
Joined: 27-February 09



Well, 2016+

If I have a document built using Bookmarks, it's easy enough to map between those and columns in a table in Access. But what if the document has Content Controls? It looks like Content Controls have a subscript to differentiate them, so how do you know how to map them to a column in a table?

With bookmarks, I could do name each bookmark so it corresponded to say a column in a table, and then mapping was something like

docWord.Bookmarks(rsBkmk.Fields(1).Name)

or
rs.Fields("Salary") = CCur(docWord.Bookmarks("Salary").Text)

and then I could loop through the bookmarks and write their values somewhere.

If Content Controls don't have individual names, how do you map them if you don't know in what order they appear?

It's a theoretical question now, but a job I looked at was trying to process a hundred thousand contracts saved as Word documents. (Or are the poor guys who got the job going to have to read each file manually? That's just plain crazy, in my opinion... hence my question.)

Thanks,

Pieter
Go to the top of the page
 
 
Start new topic
Replies
ADezii
post May 14 2019, 07:24 AM
Post#2



Posts: 2,373
Joined: 4-February 07
From: USA, Florida, Delray Beach


  1. To the best of my knowledge, you can uniquely identify Context Controls via an Index into the ContentControls Collection, as in:
    CODE
    ActiveDocument.ContentControls(1)
  2. Each Content Control also has a Unique ID Property.

This post has been edited by ADezii: May 14 2019, 07:25 AM
Go to the top of the page
 
DanielPineault
post May 14 2019, 08:13 AM
Post#3


UtterAccess VIP
Posts: 6,653
Joined: 30-June 11



Just combining ADezii's comments
CODE
Function EnumerateCCTitles(sDocFile As String)
    Dim oApp                  As Object
    Dim oDoc                  As Object
    Dim oTBx                  As Object
    Dim CC                    As Object
    Dim strTemp               As String
    Dim i                     As Long
    Const msoTextBox = 17

    On Error Resume Next
    Set oApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then    'Word isn't running so start it
        Set oApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set oDoc = oApp.Documents.Open(sDocFile)
    oApp.Visible = True
    Debug.Print "Index No", "Id", "Title", "Text"
    For Each oTBx In oDoc.Shapes
        If oTBx.Type = msoTextBox Then
            For Each CC In oTBx.TextFrame.TextRange.ContentControls
                i = i + 1
                Debug.Print i, CC.ID, CC.Title, CC.Range.Text
            Next
        End If
    Next oTBx
End Function


So once you identify the CC Id (assuming the documents are standardized!), then you can more easily bind to them for data extraction and simply use (assuming the id = 3772645804 in the example below)
CODE
ActiveDocument.ContentControls(3772645804).Title

Or in the case of the above
CODE
oDoc.ContentControls(3772645804).Title

CODE
oDoc.ContentControls(3772645804).Range.Text

CODE
oDoc.ContentControls(3772645804).Checked

--------------------
Daniel Pineault (2010-2018 Microsoft MVP)
Professional Help: http://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: http://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
ADezii
post May 14 2019, 10:23 AM
Post#4



Posts: 2,373
Joined: 4-February 07
From: USA, Florida, Delray Beach


To further complicate matters, a ContextControl exposes a Type Property which can uniquely identify what Type of Control it is:
Attached File(s)
Attached File  Type.JPG ( 89.33K )Number of downloads: 5
 
Go to the top of the page
 
MadPiet
post May 19 2019, 11:27 PM
Post#5



Posts: 3,127
Joined: 27-February 09



Okay, thanks... I'll give it a bash. I was hoping I could refer to each control by a unique name, but that appears to be impossible as two ContentControls can have the same name. So no referring to them by name and getting anywhere. Drat!

Glad I don't have to read those 100K Word files! YUCK.
Go to the top of the page
 
DanielPineault
post May 20 2019, 06:19 AM
Post#6


UtterAccess VIP
Posts: 6,653
Joined: 30-June 11



It depends on the document. If the documents you are working with are standardized (from one template), and you are confident that there is only one CC with a given name, then you can use it to bind to it, otherwise ...

--------------------
Daniel Pineault (2010-2018 Microsoft MVP)
Professional Help: http://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: http://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
MadPiet
post May 20 2019, 08:15 PM
Post#7



Posts: 3,127
Joined: 27-February 09



Okay, thanks. I'll test out your code and see what happens. Might need it some day.

even if I didn't harvest all the data from the files, I could at least loop through the ContentControls or something and write the names of each to a table... then at least I could figure out programmatically what had to be mapped where. Then it's as simple as (ContentControlName*, MapToColumnName) table. Would take the code a little longer to run, but still infinitely faster than reading all that by hand.
Go to the top of the page
 

Posts in this topic



Custom Search


RSSSearch   Top   Lo-Fi    18th June 2019 - 01:48 PM