PDA

View Full Version : Code not working HELP



Brett Desper
04-29-2010, 07:33 AM
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?

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

Brett Desper
04-29-2010, 07:38 AM
Never Mind, I think I found the answer - I needed to switch the end if and exit for in the code.

lucas
04-29-2010, 07:49 AM
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:


ActiveDocument.FormFields("Text1").Result = "my text"

Paul_Hossler
05-01-2010, 02:11 PM
hi lucas

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

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

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


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


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



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

Brett Desper
05-01-2010, 02:41 PM
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.

fumei
05-03-2010, 08:36 AM
" in 2007 ContentControls are not indexable by name"

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

Paul_Hossler
05-03-2010, 01:43 PM
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

fumei
05-03-2010, 01:54 PM
Hey Paul. Have you checked out the userform Kim75 uploaded ("Word userform, processing time slow")? Interesting. Eye-candy...but interesting.