Consulting

Results 1 to 6 of 6

Thread: Using VBA forms to insert data into MS Word 2007 Content Controls

  1. #1

    Using VBA forms to insert data into MS Word 2007 Content Controls

    Hello All, I need urgent help pls! I am new to working with VBA + Word 2007.
    I am currently working with Word 2007 template document. On the template, I created few text fields using Content Controls(i.e. rich text control) instead.
    I labelled the controls as Name, Title etc.
    I have also created a simple VBA Userform with text fields i.e. Name, Title.
    Now, rather than entering text/data directly into the template document when it is loaded, I want to use the VBA userform to enter the data so that the data gets transferred to the text areas on the document when a command button is clicked.

    This is the requirement from my place of work and i need help pls. I have tried numerous suggestions from msdn site, but to no avail. I'm always getting run time errors e.g. "run-time error 5941: the requested member of the collection does not exist"

    Private Sub CommandButton1_Click()
    ActiveDocument.ContentControls("Title").Range.Text = TextBox1.Text
    End Sub

    Any help or code would be greatly appreciated, thanks.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Brownchoc,

    If all you're doing is pushing data onto the document, why not simply use bookmarks and update their content? Doing so makes it much easier to port the document to other Word versions (even with the compatability patch, Word 97-2003 versions can't do anything with content controls [ie they're treated as plain text]).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    May 2011
    Posts
    10
    Location
    Hi,

    I am fairly new to programming and I want to insert data from excel into bookmarks in word but I do not understand much of the code and do not know how to edit it to my own ends. Can you help?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Please don't hijack existing threads. You've provided no information that anyone could use to help you. Populating Content Controls from a userform has nothing to do with populating Word bookmarks from Excel.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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

    Write UF value to CC range

    Since MS (rather stupidly in my opinion) allows multiple CCs with the title "Name" or "Title" or "Whatever" you have to explicitly identify the one you want to work with. One way is to use the Item index:

    Private Sub CommandButton1_Click()
    With ActiveDocument
    .SelectContentControlsByTitle("Name").Item(1).Range.Text = Me.TextBox1
    .SelectContentControlsByTitle("Title").Item(1).Range.Text = Me.TextBox2
    End With
    End Sub



    Quote Originally Posted by brownchoc
    Hello All, I need urgent help pls! I am new to working with VBA + Word 2007.
    I am currently working with Word 2007 template document. On the template, I created few text fields using Content Controls(i.e. rich text control) instead.
    I labelled the controls as Name, Title etc.
    I have also created a simple VBA Userform with text fields i.e. Name, Title.
    Now, rather than entering text/data directly into the template document when it is loaded, I want to use the VBA userform to enter the data so that the data gets transferred to the text areas on the document when a command button is clicked.

    This is the requirement from my place of work and i need help pls. I have tried numerous suggestions from msdn site, but to no avail. I'm always getting run time errors e.g. "run-time error 5941: the requested member of the collection does not exist"

    Private Sub CommandButton1_Click()
    ActiveDocument.ContentControls("Title").Range.Text = TextBox1.Text
    End Sub

    Any help or code would be greatly appreciated, thanks.
    Greg

    Visit my website: http://gregmaxey.com

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

    Follow-up

    When a CC is created it is assigned a unique ID. Probably the most robust means of referencing CCs is to use the ID. You can get the ID with the Document_ContentControlOnExit event:

    [VBA]Option Explicit
    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    MsgBox ContentControl.ID
    End Sub

    Sub ScratchMacro()
    'A quick macro scratch pad created by Greg Maxey
    'Use whatever the msgbox returned as the number here:
    ActiveDocument.ContentControls("3060960396").Range.Text = "I am CC ID 3060960396"
    End Sub
    [/VBA]


    Quote Originally Posted by brownchoc
    Hello All, I need urgent help pls! I am new to working with VBA + Word 2007.
    I am currently working with Word 2007 template document. On the template, I created few text fields using Content Controls(i.e. rich text control) instead.
    I labelled the controls as Name, Title etc.
    I have also created a simple VBA Userform with text fields i.e. Name, Title.
    Now, rather than entering text/data directly into the template document when it is loaded, I want to use the VBA userform to enter the data so that the data gets transferred to the text areas on the document when a command button is clicked.

    This is the requirement from my place of work and i need help pls. I have tried numerous suggestions from msdn site, but to no avail. I'm always getting run time errors e.g. "run-time error 5941: the requested member of the collection does not exist"

    Private Sub CommandButton1_Click()
    ActiveDocument.ContentControls("Title").Range.Text = TextBox1.Text
    End Sub

    Any help or code would be greatly appreciated, thanks.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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