none
Having trouble using a macro in word 2007 to fill in content controls

    Question

  • I created a macro in word 2007, inside is a form with a button ("Apply") that is supposeed to fill in a content control ("Address"). I thought that by using this code I would be able to fill in the content control in the form but I am having some trouble and can not find any good articles or forums pertaining to this type of coding. Any help will be greatly appreciated.

    Regards

    Maurice5001

    Below is the code:

    Dim Address As Word.ContentControl

    Set Address = ActiveDocument.SelectContentControlsByTitle("Address")

    Address = txtAddress.Text

    Tuesday, August 17, 2010 3:43 PM

Answers

  • Assuming that you are obtaining the address from a userform field called TextBox1 and that userform has a command button to update the content control in the document, then the following user form code code associated with the command button (here CommandButton1) will do that

    Option Explicit
    Private sAddress As String
    Private oCC As ContentControl
    Private Sub CommandButton1_Click()
    sAddress = Me.TextBox1.Value
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = "Address" Then
            oCC.Range.Text = Me.TextBox1.Value
        End If
    Next oCC
    Unload Me
    End Sub

    <Maurice5001> wrote in message news:e4c61432-6ee5-47f7-a077-26f5379326f6@communitybridge.codeplex.com...

    thank you for your help. I just tried it an it is still giving me an error on the line below:

    Set Address = ActiveDocument.SelectContentControlsByTitle("Address")

    The error is : object variable or with block variable not set.

    I am clueless because in the line of code above I am clearly setting the variable.


    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Wednesday, August 18, 2010 1:54 PM
  • Hi Maurice

    <uuf> I'm sorry, I obviously wasn't thinking things all the way through!

    When you use SelectContentControlsByTitle (or by Tag), this returns a collection, not a single content control. So you need to do something more like this:

    Dim ccs as Word.ContentControls
    Set ccs = ActiveDocument.SelectContentControlsByTitle("Address")
    Set Address = ccs(1) 'Assuming it's the only one, this is safe
    Address.Range.Text = "the text"


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 18, 2010 3:12 PM
    Moderator

All replies

  • Hi Maurice

    See if something like this works:

    Address.Range.Text = txtAddress.Text


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 18, 2010 5:39 AM
    Moderator
  • thank you for your help. I just tried it an it is still giving me an error on the line below:

    Set Address = ActiveDocument.SelectContentControlsByTitle("Address")

    The error is : object variable or with block variable not set.

    I am clueless because in the line of code above I am clearly setting the variable. 

    Wednesday, August 18, 2010 12:50 PM
  • Assuming that you are obtaining the address from a userform field called TextBox1 and that userform has a command button to update the content control in the document, then the following user form code code associated with the command button (here CommandButton1) will do that

    Option Explicit
    Private sAddress As String
    Private oCC As ContentControl
    Private Sub CommandButton1_Click()
    sAddress = Me.TextBox1.Value
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = "Address" Then
            oCC.Range.Text = Me.TextBox1.Value
        End If
    Next oCC
    Unload Me
    End Sub

    <Maurice5001> wrote in message news:e4c61432-6ee5-47f7-a077-26f5379326f6@communitybridge.codeplex.com...

    thank you for your help. I just tried it an it is still giving me an error on the line below:

    Set Address = ActiveDocument.SelectContentControlsByTitle("Address")

    The error is : object variable or with block variable not set.

    I am clueless because in the line of code above I am clearly setting the variable.


    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Wednesday, August 18, 2010 1:54 PM
  • Hi Maurice

    <uuf> I'm sorry, I obviously wasn't thinking things all the way through!

    When you use SelectContentControlsByTitle (or by Tag), this returns a collection, not a single content control. So you need to do something more like this:

    Dim ccs as Word.ContentControls
    Set ccs = ActiveDocument.SelectContentControlsByTitle("Address")
    Set Address = ccs(1) 'Assuming it's the only one, this is safe
    Address.Range.Text = "the text"


    Cindy Meister, VSTO/Word MVP
    Wednesday, August 18, 2010 3:12 PM
    Moderator
  • Hi Maurice,

    How is this issue going on in your side? Have you solved the problem?

    Best Regards,
    Amy Li
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, August 23, 2010 8:53 AM
    Moderator