Consulting

Results 1 to 8 of 8

Thread: Code not working HELP

  1. #1

    Code not working HELP

    I have the following code inserted into a (much) larger macro. For some reason it will not change the content control field in the word document I am trying to edit. The spelling matches the field in the doc exactly (including the cap in the wrong place.) etc. Other (similar) code just below it seems to be working just fine. Where do I start to find the problem here?

    [vba]For i = 1 to wdDoc.ContentControls.Count
    If wdDoc.ContentControls.(i).Title = "coverPages" then
    wdDoc.ContentControls (i).range.text = "4"
    End If
    Exit For
    Next
    [/vba]

  2. #2
    Never Mind, I think I found the answer - I needed to switch the end if and exit for in the code.

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I don't have 2007 but I don't see why you should have to loop through all the content controls to do what you are doing.

    For instance in a 2003 formfield you just call the formfield and tell it what text to have in it like this:

    [VBA]
    ActiveDocument.FormFields("Text1").Result = "my text"
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    hi lucas

    ref: http://www.vbaexpress.com/forum/showthread.php?t=31499

    For some reason, in 2007 ContentControls are not indexable by name

    That would make things too easy, and where's the challenge in that ?


    In this example, only the uncommented line works, and it's too difficult to keep track of the numbers

    [VBA]
    Sub drv()

    ' error 5941 - requested member of collection does not exist
    ' ActiveDocument.ContentControls("generic").Range.Text = "sample"


    ActiveDocument.ContentControls(1).Range.Text = "sample"

    ' Compile error: Method or data member not found
    ' ActiveDocument.ContentControls(1).Name = "generic"


    End Sub
    [/VBA]


    Only I've found is to iterate through the collection by .Index looking for the .Title property that I want

    Still open to ideas

    Paul

  5. #5
    That is the only way I have gotten it to work. I defined the search through the content control titles as a function and then just call that when I need to find something. It is still a bit of a pain, but that's the way it works.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    " in 2007 ContentControls are not indexable by name"

    Yet another reason to dislike it. What a bloody stupid thing.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    It does make it hard to talk to them via VBA, since you have to walk through the collection looking for the .Title you want

    But on the other hand, a doc can have multiple Content Controls with the same .Title, and when you update one (KB or VBA), all the others with the same .Title also get updated.

    I guess you could acheive the same with bookmarks and REF fields

    Paul

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hey Paul. Have you checked out the userform Kim75 uploaded ("Word userform, processing time slow")? Interesting. Eye-candy...but interesting.

Posting Permissions

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