Consulting

Results 1 to 8 of 8

Thread: Identify specific Content Controls in multiple templates

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Identify specific Content Controls in multiple templates

    Guys,

    I am creating a template add-in in Word 2010 which modifies the Ribbon UI and allows the user to insert various Content Control with predefined properties, such as .Title, .Tag etc.

    When the user is navigating through the document, the add-in also has to distinguish between different CCs (i.e. CCs added with the predefined properties or CCs inserted by the user in the normal way). To do this, I am using the CC OnEnter & OnExit events. When the add-in has identified an appropriate CC through its OnEnter event, it should set a public variable equal to the currently selected CC. This feature should work throughout all of the user's templates, which will be stored in a common folder.

    Am I right in thinking that in order to identify and set a public variable equal to the selected CC, each of the user's templates will require code for the CC OnEnter & OnExit events stored in the templates ThisDocument module?

    Is this correct or is there an easier way to accomplish this? Currently I have it working as described above, for a few templates.

    Cheers,
    Joe

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Joe,

    I haven' tested it but unless you wanted to create some sort of event (e.g, WindowsSelection_Change) in your add-in to detect entering a CC then yes would need at least one line of code in the ThisDocument CCOnEnter event.

    You could set a reference to your project (Let's call it DemoAddin.dotm) in in each template and use something like this:

    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
      DemoAddIn.mod_DemoAddIn.DoSomething ContentControl
    End Sub
    
    In your project include a module naed mod_DemoAddIn with:
    
    
    
    Sub DoSomething(oCC As ContentControl)
      MsgBox oCC.Title
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Thanks for that Greg. I understand what you are saying and I'm going to go away and have a think.

    The question now is less about targeting the CC and more about making sure that all the user templates have the appropriate reference and code in the ThisDocument module to allow targeting of the CC. I would like the user to be able to add templates to their collection without having to deal with any references or VBA code. Am I right in saying that this is taking me down the extensibility route? Programmatically adding a reference and copying code into the templates?

    Cheers,
    Joe

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Joe,

    I honestly don't know where the extensibility route leads. What you appear to be wanting to do isn't something that I recall doing. I know you can copy code from an add-in to a anther open file. What you can't do is force the user to allow you access the VB project object module. Look to the Trust Center settings. If that box is unchecked I'm afraid you are finis ;-(
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Just a thought. As the issue mainly relates to the probability of users entering CCs without using the tools you are creating, why not simply remove the built in tools from the ribbon from your template and replace them with those accessing code from your template? Thus you have far more control over the titling and tagging of the controls you add. If your template is used as an add-in there should be no need to complicate matters by writing code to users' templates, which will not in any case be possible (or in a best case not easily) should the VBA projects those templates be locked.

    As for writing code to the ThisDocument module of the documents created using functions from your template, you can do that from code stored in a Word document, or text file(s). Text is quicker but you cannot protect it from user interference so readily.

    I did something like this at http://www.gmayor.com/interactive_multichoice.html recently, which employs code variations depending on the choice of options used. The following demonstrates writing code from a text file to an existing module in a document such as ThisDocument. Writing from a document is no more difficult and you can use bookmarked ranges to hold the different code options and write the range texts to the prepared documents.

    Public Function AddVBACodeFromTXT(oDoc As Document, strSource As String, strModule As String)
    'Graham Mayor - http://www.gmayor.com
    Dim strLines As String
    Dim i As Long, j As Long
    Dim strCode As String
    Dim iFile As Integer: iFile = FreeFile
    
        Open strSource For Input As #iFile
        strCode = Input(LOF(iFile), iFile)
        Close #iFile
    
        i = oDoc.VBProject.VBComponents(strModule).CodeModule.CountOfLines
        oDoc.VBProject.VBComponents(strModule).CodeModule.DeleteLines 1, i
        oDoc.VBProject.VBComponents(strModule).CodeModule.AddFromString strCode
    lbl_Exit:
        Exit Function
    End Function
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Greg/Graham, thanks for that.

    I will have a look through the link you provided Graham. If I understand you correctly, you are talking about inserting stored code (stored in text or a document) into the document which is created based on the template, as opposed to the template itself?

  7. #7
    Merry Christmas!

    Basically a document can only use code from the template it was created from, if the document can 'see' the template. If you send such a document to a third party, that third party doesn't normally have access to the template and therefore neither does the document. If you want that code to work in the absence of the template, then the code must be stored in the document and the document saved as macro enabled (you still have the issue of ensuring that the user runs the macro, which cannot be enforced) but at least the user is able to employ the macros.

    This was the essential dilemma in the link I posted. Users needed to fill in forms but in order to do so, they required the macros they didn't have. The add-in I produced, which is an aid to creating a particular type of form, copies macro code from bookmarked sections of a Word document to the document, that allows the document to be completed. As the created documents may have different configurations, the code copied varies.

    I used just one Word document to contain the code, password protected to inhibit the person using the add-in from messing it up. The code in the documents is unprotected.

    I have also used, in other projects, code similar to that I posted, which uses a text file to hold the code. The point is that it is just a means to an end. How you achieve it depends on what it is you are trying to do, and who the end users are.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Thanks Graham and Merry Christmas.

    I think I have enough information on the subject to go away and try different solutions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •