Can't run macro containing for each

L

Louverril

This code below was the solution to an earlier problem of mine but I have
found that I cannot ruin and for each .. next code.

Not matter what the detail of the objCC property in the code below - could
be placeholdertext or anything using a "For Each objCC In
ActiveDocument.ContentControls" and running is from the macro run button
hangs Word. The first control is updated and then it sticks and I have to use
task manager to shut word down.

I CAN run it by setting a breakpoint in the vb editor and stepping through
but running it without the breakpoint does not work.

Sub SetInitialShading()
Dim objCC As ContentControl
For Each objCC In ActiveDocument.ContentControls
objCC.Range.Shading.BackgroundPatternColor = wdColorLightYellow
Next
End Sub

Any ideas

Thanks Lou
 
G

Greg Maxey

Lou,

I can not duplicate your error. Try:

Sub SetInitialShading()
Dim objCCs As ContentControls
Dim i As Long
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightYellow
Next i
End Sub
 
L

Louverril

Greg thanks so much for your help.

I had the same problem with the code below - BUT I have workd out that I am
only having problems if this code:-

"Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl,
Cancel As Boolean)
If oCC.Range.Text = oCC.PlaceholderText Then
oCC.Range.Shading.BackgroundPatternColor = wdColorLightYellow
Else
oCC.Range.Shading.BackgroundPatternColor = wdColorLightGreen
End If
End Sub"

is in the ThisDocument section under Microsoft Word Objects. If it's not
present all the code works fine!

But I have to put the code above in the ThisDocument section otherwise is
does not run when you enter a content control?

I am not sure but is the coount or for each macro causing Word to try and
run the above macro and getting confused?

Thanks so much for your help so far I have learned a lot - please can you
shed any light on the above?

Lou
 
G

Greg Maxey

Lou,

Ok yes I was able to reproduce the problem. I don't know why it occurs, but
try:

Sub SetInitialShading()
Dim objCCs As ContentControls
Dim i As Long
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
DoEvents
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightYellow
Next i
End Sub
 
L

Louverril

Thanks Greg but as mentioned I get the same result with this code.

Best wishes Lou
 
J

Jules

Works on my content controls - thanks Greg.
Greg Maxey said:
Lou,

I can not duplicate your error. Try:

Sub SetInitialShading()
Dim objCCs As ContentControls
Dim i As Long
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightYellow
Next i
End Sub
 
L

Louverril

Hello Jules - yes the code works but does it work if you have the code below
in the ThisDocumnet section?


Cheers

Lou
I had the same problem with the code below - BUT I have workd out
that I am only having problems if this code:-

"Private Sub Document_ContentControlOnExit(ByVal oCC As
ContentControl, Cancel As Boolean)
If oCC.Range.Text = oCC.PlaceholderText Then
oCC.Range.Shading.BackgroundPatternColor = wdColorLightYellow
Else
oCC.Range.Shading.BackgroundPatternColor = wdColorLightGreen
End If
End Sub"
 
G

Greg Maxey

Lou,

You did add the "DoEvents" statement as shown in that code correct? Without
that statement, I can replicate your problem. With that statement I have no
problem.
 
L

Louverril

Greg,

1. I cleared all the macros in the normal template and closed all other
documents. I then created a new document and added the following code -
identical to your suggestions:

2. In the ThisDocument section I put this:

Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl,
Cancel As Boolean)
If oCC.Range.Text = oCC.PlaceholderText Then
oCC.Range.Shading.BackgroundPatternColor = wdColorLightYellow
Else
oCC.Range.Shading.BackgroundPatternColor = wdColorLightGreen
End If
End Sub

3. and in a new module “module1†I put this:

Sub SetInitialShading()
Dim objCCs As ContentControls
Dim i As Long
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
DoEvents
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightYellow
Next i
End Sub

4. I then saved the document as testtext.dotm.
5. I then added one rich text contentcontrol from the Developer tab controls
group.
6. I went to the macros button and selected the only macro showing there
“SetInitialShading†and selected run.
7. The macro ran no problem (success! But wait a minute...)
8. I then added a second content control – as above and ran the macro; again
as above.

Word hung and I had to use task manager. But wait...

Then I realised I have probably - as someone new to Word macros (used to
Access) and in search of a good book - made what is to you an error so
obvoius you didn't think anyone could be so stupid.

When I tried closing the .dotm and opening a document based on it, the
setInitialShading macro the macro ran fine..... But

If you then clicked into one of the rich text content controls the curser
went wild as if it was in a LOOP until you click outside the control.

Are you sure the setinitalshading macro does not set the other macro into a
loop - because of the way it "autoexecutes" when the control is "active"?

Thanks very very much for your patient help.

Lou
 
L

Louverril

Hello Greg,

I have now set up the template with the onExit macro being commented out so
all the content controls show with a clear background.

I then uncommented out the onexit code, saved it and opened up a new
document based on it. Which better reflects what will happen in the real
world.

I then ran the setinitialshading macro before I entered any of the content
controls (will look at putting it in an autoexec). This worked.

I then editted a couple of the controls - the background colours changed
fine with no "looping". So everything worked.

But that leaves two big problems:

I cannot clear the shading for printing - if I try to change it to white by
running a similar macro to setinitialshading. The whole thing hangs.

Also when you; reopen the edited document and try to run setinitialshading -
it hangs.

The problem seems to be the If statement in

Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl,
Cancel As Boolean)
If oCC.Range.Text = oCC.PlaceholderText Then
oCC.Range.Shading.BackgroundPatternColor = wdColorLightYellow
Else
oCC.Range.Shading.BackgroundPatternColor = wdColorLightGreen
End If
End Sub

Like this below it works - but is not as much use as although the user may
have click into the content control the text has not changed.

Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl,
Cancel As Boolean)
oCC.Range.Shading.BackgroundPatternColor = wdColorLightGreen
End Sub

Thanks again for your patience.

Lou

Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl,
Cancel As Boolean)
oCC.Range.Shading.BackgroundPatternColor = wdColorLightYellow
End Sub
 
G

Greg Maxey

Lou,

I do know for certain that there is what I consider a bug in the Content
Control entry/exit events and I reported it to MS several months ago.

You can see a demonstration of the bug with the following procedure:

Open a new document and create a couple of controls with unique titles.
Enter the following code and click or tab from control to control. The
messages report as expected.

Private Sub Document_ContentControlOnEnter(ByVal CC As ContentControl)
MsgBox CC.Title & " On entry event fired."
End Sub
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel
As Boolean)
MsgBox CC.Title & " On exit event fired."
End Sub

Now save, close, and reopen the document. Click or tab from control to
control. You will notice that "usually" you see three events OnExit,
OnEntry and another OnExit. The third onExit is unexpected, I think it is a
bug, and it can have disasterous effects on what you think your code should
be doing.

This is probably what is contributing to your current problem.

What if you simply take out the SetIntialShading code? The OnExit event
should take care of formatting new controls as they are added and you move
to the next control.
 
L

Louverril

Thanks for this Greg.

Swear words about the bug.

I have recreated it and can confirm that I get two on exits in some cases. I
created a new document and added three rich text controls. I clicked from one
to the other.

Critically (for my problem) I do NOTrecieve two on exit msgboxes on the
first control in the document but I do as I click away from the others. This
matches the behaviour I saw in the tests we have been going through where the
first control in any doument turned the correct colour but after that Word
hung.

Your suggestion was a good one but I need some way or clearing the colour
changes as well as setting then so the document can be printed without the
"highlighting". The only way I know to clear the background colour is to use
the same code as you gave for the setinitialshading but use white - and I
then have the same issues.

So I thought what I could do is not use the onexit code and put an if
statement in the setinitialshading so that if the document was closed when it
was reopened later anything that was not placeholder text would be green not
yellow. And any then freshly editied text will have no background.

So I tried this:

Sub AutoExec()
Dim objCCs As ContentControls
Dim i As Long
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
DoEvents
If objCCs(i).Range.Text = objCCs(i).PlaceholderText Then
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightYellow
Else
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightGreen
End If
Next i
End Sub

Which works fine but I cannot get it to run automatically when the document
is opened. Why won't it run ? The autoexec won't run at all - tried putting a
msgbox in.

Thanks Lou
 
L

Louverril

Greg - figured out the Autoexec.

Need to put the code in Sub Document_New()

I'm on a fast and steep learing curve!

Lou
 
G

Greg Maxey

Lou,

You are also on a slipery sloop when it comes to ContentControls. I don't
know how many bugs there are with them (discovered and still lurking).

I think this issue with shading is another one. Try this test.

Create a new blank document and add a single line of text.

Then go down a few lines and adds some CCs.

Run these three macros:

Sub SetInitialShading()
Dim objCCs As ContentControls
Dim i As Long
ActiveDocument.Paragraphs(1).Range.Shading.BackgroundPatternColor =
wdColorLightYellow
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
DoEvents
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightYellow
Next i
End Sub

Sub SetNewShading()
Dim objCCs As ContentControls
Dim i As Long
ActiveDocument.Paragraphs(1).Range.Shading.BackgroundPatternColor =
wdColorLightGreen
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
DoEvents
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorLightGreen
Next i
End Sub

Sub SetFinalshading()
Dim objCCs As ContentControls
Dim i As Long
ActiveDocument.Paragraphs(1).Range.Shading.BackgroundPatternColor =
wdColorAutomatic
Set objCCs = ActiveDocument.ContentControls
For i = 1 To objCCs.Count
DoEvents
objCCs(i).Range.Shading.BackgroundPatternColor = wdColorAutomatic
Next i
End Sub

I have no idea why SetFinalShading does not remove the shading from the
CC.Ranges. This seems like a bug to me and I will pass it on to my MVP
lead.
 
L

Louverril

Greg,

I repeated this.

The set final shading macro only changed the first paragraph back to a clear
background.

I had experienced some issues using automatic during trying to get a
solution to my problem. If you change your setfinalshading macro to white
rather than automatic it works OK (I know this is not the same as automatic
but its strange that it works when auto won't).

Also as I noticed in my struggles (!) if a control has text entered it
behaves differently. if you do as per your instructions but enter some text
in one of the controls you enter the setfinalshading will work for that
control.

You are right about content controls being problematic. I have spent twice
as long on this project as i had expected - even though I had expected to
spend a lot of time because anything but basic word macros were something to
learn for me. I have just had to recreate all the content controle in a
template - I don't not why but the code we have been discussing would not
work woith them. I recreated them to be exaclty the same (as far as all the
prperty info could tell me) and they worked. I don't know whether it was
because I started from an existing document created originally with different
styles and fonts - that's my only guess and too vague to test - anyway
recreating them worked. Lesson learned - create all content controls from
scratch if you can.

Thanks for your help on this. And I wish you well with getting a good
response from Microsfot on this. The content controls are a great idea and it
wouls be a shame for them not to be well used.

Lou
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top