PDA

View Full Version : Solved: Check whether first character in a paragraph is a checkbox?



DaVinney
05-16-2011, 11:00 AM
As my routine processes paragraphs using a 'For Each oPara In oRange.Paragraphs' loop, is there a way to detect if the very first character is a formfield checkbox?

gmaxey
05-16-2011, 03:27 PM
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oPar As Paragraph
Dim oFF As FormField
For Each oPar In ActiveDocument.Range.Paragraphs
'This seems to work:
If Asc(oPar.Range.Characters(1)) = 21 Then MsgBox "Bingo"
'This may be more accurate:
If oPar.Range.FormFields.Count > 0 Then
If oPar.Range.FormFields(1).Type = wdFieldFormCheckBox Then
If oPar.Range.FormFields(1).Range.Start = oPar.Range.Start Then
MsgBox "Bingo"
End If
End If
End If
Next oPar
End Sub

DaVinney
05-16-2011, 04:13 PM
Thanks for the code, Greg :hi:

In the meantime while I was waiting for a reply, I arrived at this (snippet) that also seems to work:

Dim oRange As Range
For Each oPara in MyRange.Paragraphs
Set oRange = oPara.Range

oRange.End = oRange.Start + 1

If oRange.FormFields.Count = 1 Then
If oRange.FormFields(1).Type = wdFieldFormCheckBox Then
'Insert the process here
End If
End If
Next

I take it you didn't need variable oFF afterall.

gmaxey
05-16-2011, 05:55 PM
... didn't ned the variable oFF after all.

Right. I declared it the got off on another tangent ;-).

Frosty
05-17-2011, 10:46 AM
DaVinney: I'm pretty sure that you can get erroneous returns with your code vs. Greg's 2nd method (the ASC thing has risks too, I *think*).

The problem with fields and ranges in general is that the range will actually change, depending on whether (for the purposes of simplicity) you are describing the result of the field (in this case, a checkbox), or the field itself (what's shown if you reveal the field codes). And what you think you're describing is not always the case. There are a few too many different settings to check to make sure your code would work at the time you run it.

So, imho, better to test the .Start of the range of the first field in the paragraph (Greg's methodology) rather than the entirety of the range of the field (which might be 1 character for a checkbox, but it might also be 13+ characters for the whole {FORMFIELD yada yada} stuff "behind the scenes"

Make sense?

DaVinney
05-17-2011, 06:34 PM
Frosty, yeah I see your point why Greg's method is more reliable :think:

That's the one I initially opted to go with, actually.


Thanks for the help guys.


Would also appreciate your help with another item (new thread) regarding skipping over tables.