Working with Loops

L

LEU

Below is a section of my macro. Is there a way to do a loop?

Dim i as Long
Dim WC35 As String, WC36 As String, WC37 As String

Set oFF = ActiveDocument.FormFields

If oFF("Check35").CheckBox.Value = False And Me.Controls("Ck35").Value =
False Then
WC35 = False
ElseIf oFF("Check35").CheckBox.Value = False And Me.Controls("Ck35").Value =
True Then
WC35 = False
ElseIf oFF("Check35").CheckBox.Value = True And Me.Controls("Ck35").Value =
False Then
WC35 = True
ElseIf oFF("Check35").CheckBox.Value = True And Me.Controls("Ck35").Value =
True Then
WC35 = False
End If

If oFF("Check36").CheckBox.Value = False And Me.Controls("Ck36").Value =
False Then
WC36 = False
ElseIf oFF("Check36").CheckBox.Value = False And Me.Controls("Ck36").Value =
True Then
WC36 = False
ElseIf oFF("Check36").CheckBox.Value = True And Me.Controls("Ck36").Value =
False Then
WC36 = True
ElseIf oFF("Check36").CheckBox.Value = True And Me.Controls("Ck36").Value =
True Then
WC36 = False
End If

If oFF("Check37").CheckBox.Value = False And Me.Controls("Ck37").Value =
False Then
WC37 = False
ElseIf oFF("Check37").CheckBox.Value = False And Me.Controls("Ck37").Value =
True Then
WC37 = False
ElseIf oFF("Check37").CheckBox.Value = True And Me.Controls("Ck37").Value =
False Then
WC37 = True
ElseIf oFF("Check37").CheckBox.Value = True And Me.Controls("Ck37").Value =
True Then
WC37 = False
End If
Set oFF = Nothing



I tried the following but the debugger kept giving me an error at ‘(“WC†&
i)’.

Set oFF = ActiveDocument.FormFields
For i = 35 To 37
If oFF("Check" & i).CheckBox.Value = False And Me.Controls("Ck" & i).Value =
False Then
(“WC†& i) = False
ElseIf oFF("Check" & i).CheckBox.Value = False And Me.Controls("Ck" &
i).Value = True Then
(“WC†& i) = False
ElseIf oFF("Check" & i).CheckBox.Value = True And Me.Controls("Ck" &
i).Value = False Then
(“WC†& i) = True
ElseIf oFF("Check" & i).CheckBox.Value = True And Me.Controls("Ck" &
i).Value = True Then
(“WC†& i) = False
End If
Next i
Set oFF = Nothing
 
H

Helmut Weber

Hi LEU,

try CStr(i): (“WC” & Cstr(i))



--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

.... also:

compare:
try CStr(i): (“WC” & Cstr(i)) vs
try CStr(i): ("WC" & Cstr(i))


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
L

LEU

Hi Helmut,

These both look the same. I plugged it into my macro as follows and I get an
error 'Syntax error'.

Set oFF = ActiveDocument.FormFields
For i = 35 To 37
If oFF("Checki").CheckBox.Value = False And Me.Controls("Cki").Value = False
Then
CStr(i): (“WC†& Cstr(i)) = false
ElseIf oFF("Checki").CheckBox.Value = False And Me.Controls("Cki").Value =
True Then
CStr(i): (“WC†& Cstr(i)) = false
ElseIf oFF("Checki").CheckBox.Value = True And Me.Controls("Cki").Value =
False Then
CStr(i): (“WC†& Cstr(i)) = true
ElseIf oFF("Checki").CheckBox.Value = True And Me.Controls("Cki").Value =
True Then
CStr(i): (“WC†& Cstr(i)) = false
End If
Next i
 
L

LEU

It really looks like this:

Set oFF = ActiveDocument.FormFields
For i = 35 To 37
If oFF("Check" & i).CheckBox.Value = False _
And Me.Controls("Ck" & i).Value = False Then
CStr(i): (“WC†& Cstr(i)) = false
ElseIf oFF("Check" & i).CheckBox.Value = False _
And Me.Controls("Ck" & i).Value = True Then
CStr(i): (“WC†& Cstr(i)) = false
ElseIf oFF("Check" & i).CheckBox.Value = True _
And Me.Controls("Ck" & i).Value = False Then
CStr(i): (“WC†& Cstr(i)) = true
ElseIf oFF("Check" & i).CheckBox.Value = True _
And Me.Controls("Ck" & i).Value = True Then
CStr(i): (“WC†& Cstr(i)) = false
End If
Next i
 
H

Helmut Weber

Hi LEU,

sorry, you are completely off the track.
If oFF("Checki").CheckBox.Value = False And Me.Controls("Cki").Value = False
Then
CStr(i): (“WC” & Cstr(i)) = false
ElseIf oFF("Checki").CheckBox.Value = False And Me.Controls("Cki").Value =

"Checki" doesn't make sense.
"Check" & Cstr(i) '!
"Cki" doesn't make sense either.
"Ck" & Cstr(i) '!

You might be better off with a "select case" command.




--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
L

LEU

What I tried really looks like this:

Set oFF = ActiveDocument.FormFields
For i = 35 To 37
If oFF("Check" & i).CheckBox.Value = False _
And Me.Controls("Ck" & i).Value = False Then
CStr(i): (“WC†& Cstr(i)) = false
ElseIf oFF("Check" & i).CheckBox.Value = False _
And Me.Controls("Ck" & i).Value = True Then
CStr(i): (“WC†& Cstr(i)) = false
ElseIf oFF("Check" & i).CheckBox.Value = True _
And Me.Controls("Ck" & i).Value = False Then
CStr(i): (“WC†& Cstr(i)) = true
ElseIf oFF("Check" & i).CheckBox.Value = True _
And Me.Controls("Ck" & i).Value = True Then
CStr(i): (“WC†& Cstr(i)) = false
End If
Next i

The ("Ck" & i) are the check boxes in my form.

LEU
 
H

Helmut Weber

Hi LEU,

apart from other issues,
If oFF("Check" & i).CheckBox.Value = False
^ ^
Check is enclosed in straight quotes chr(34), that is ok!
CStr(i): (“WC” & Cstr(i)) = false
^ ^
WC is preceded by chr(147) and followed by chr(148), that is not ok!

Furthermore don't try to assign a boolean value to a string,
like (“WC” & Cstr(i)) = false

Use a nonproportional font such as CourierNew
to display this message.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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