PDA

View Full Version : Is Cursor in a field positioned



thomas-steth
11-22-2007, 03:58 AM
Hi,

I have a word document with several fields inserted (all PRINT fields) like

{PRINT "This is a test" \* MERGEFORMAT}

The document is viewed that the fields are visible.
How can I detect within a macro if the cursor is positioned or even expended within that field without the field is fully marked.

Thank you for your help.
Thomas

fionabolt
11-22-2007, 04:51 AM
I think this will do what you want:

Sub IsThisAField()
With Selection
If .FormFields.Count = 1 Then
' CheckBox or DropDown
MsgBox .FormFields(1).Name
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
MsgBox .Bookmarks(1).Name
End If
End With
End Sub

thomas-steth
11-22-2007, 05:35 AM
Thank you for your help, but you hit the not moving in nail twice. Your example is correct if the selection marks he whole field. If it marks only a part within the field, the return of the Selections.Fields.Count is 0.

The second hit is getting the correct field number in the flields list without having supplied a unique bookmark to each field.

But the secodn oneis for my applicaton not so important, the first ons is a real problem.
Thomas

fumei
11-22-2007, 10:09 AM
1. I don't know how .FormFields.Count would work, as a PRINT is not a formfield. .Fields.Count would work.

2. If the Selection does not include the whole field, yes, the count = 0. So, select the field. This would be easy IF the field is a single paragraph.

{PRINT "This is a test" \* MERGEFORMAT}

By itself.

If the Selection in the field (but NOT selecting the whole field), then:

Selection.Paragraphs(1).Range.Select

will select the whole paragraph, and therefore the whole field, and the .Count = 1.

More importantly though, is why do you need this?

"How can I detect within a macro if the cursor is positioned or even expended within that field without the field is fully marked."

Why do you need to?

thomas-steth
11-22-2007, 12:37 PM
Dear Fumei,

I did not start to talk about FormFields it was the person who tried to help first. I always talked about fields.

Why do I need that, We have a PCL 5 (HP Laser printer) based Output Management system. Normally the data is already positioned and our SW archives, eMails, faxes, reprints, generate copies and can handle with a high level command line all printer functions like stapling, paper tray in and out handlungm watermarks etc. as well as overlaying forms.

Those forms are normally inserted like you put an overhead foil on preprinted paper. Our software gains varibales, and puting those variables within those overlayed forms, our system will replace them by its conten. So it is something like a merge engine.

Due to the way how windows printes, is not really possible (anymore) to find some text (e.g. variabe names) in data streams. So we use the MSWord PRINT FIELD to sned data direct to the printer (Print file). However in that case we do noz only send the variable we must also send all informations like text size, style etc. thsi si the content of the print field.

Our new developed macro now can generate such fields and change them. So the client can now teh first time use our Software to really merge variable data into any form, simply generated by MS Word.

When our macro is started it coul be, that teh curser is in middle of such a field, and it could be, that it is even not a print field form our macro, but any print field or any other field.

The problem is, if by error now the cleint adds a new field, so there is then a field in a field. Or part of the field is erased by the macro etc.

This is why we have the intention on macro start, to find out if the cursor is in a field or not.

I searched more or less a half a day for a solution. But could not find any VBA statement helping me out.

Actually I found and programmed a solution now after I have placed the thread: But I am not happy with it: Takes somehow too much time and too much positioning aroound.

Actully what is does is to save the original cursoer start and end position, mark the closest print field, It there is only one jump direct to it, there are more then one jump to the next one (or previouse one if it was behind or in the last one.) and then return to the next closest one in the oposite direction.

So at the end I check ifthe original selection was within the closest field. And this can only be, if the cursor was already in that field.

But there must be an easier way... Any way :friends: and here is my code now
Dim OriginalSelectionStart
Dim OriginalSelectionEnd
' Save the original selection range
OriginalSelectionStart = Selection.Start
OriginalSelectionEnd = Selection.End
Start1:
AddVar.Caption = "Add"
If ListVariableType.ListIndex = 2 Then
' Word cursor is in an unknown area, maybe we need to add a new pritn field
If Selection.Fields.Count > 0 Then
' At least there is a field in teh actual selection, so we can propably replace it
AddVar.Enabled = True
Else
' no field in the selection, so there is eithe rnone, or the cursor is within a [print] field
If ActiveDocument.Fields.Count > 0 Then
' ok but there are fields in that document
If ActiveDocument.Fields.Count = 1 Then
' only one field, now select it
ActiveDocument.Fields(1).Select
Else
' several fields, so we try to select the next closet one
Selection.NextField
If (OriginalSelectionEnd = Selection.End) And (OriginalSelectionEnd = Selection.Start) Then
' this can be true, if cursor is positined in the last field,
' As there are several fiels we can then try to jup to the last field
Selection.PreviousField
' and then back to the closest field
Selection.NextField
Else
' Simply go now back to the field before
Selection.PreviousField
End If
End If
' at that point we have selected the closest field and actually the next if command must be always true
If Selection.Fields.Count > 0 Then
' check if the Original Cursor setting was within that field
If (OriginalSelectionStart <= Selection.End) And (OriginalSelectionEnd >= Selection.Start) Then
' if yes, then restart and find what we look for and exit with Replace enabled true
GoTo Start1
End If
End If
' no, the cursoer is no definitive outside a field, so reset teh original selection
Selection.Start = OriginalSelectionStart
Selection.End = OriginalSelectionEnd
End If
' nothing found, Make all btton to disable
AddVar.Enabled = False
End If

If Selection.Start <> Selection.End Then
AddVar.Caption = "Replace"
End If
Else
If OriginalSelectionStart = OriginalSelectionEnd Then
AddVar.Enabled = True
End If
End If

TonyJollans
11-24-2007, 06:50 AM
I'm not following all your code but I think this will be a bit simpler, although I haven't tested it and there may be situations where it could fail.

Firstly ..
Selection.PreviousField
Selection.NextField
should select the field the cursor was in, if it was in one. It may be that this is a subset or a superset of the Selection, but the two can not partially overlap, so ..
Dim Sel As Range, SelF As Range
Set Sel = Selection.Range
Selection.PreviousField
Selection.NextField
Set SelF = Selection.Range
Sel.Select ' restore if you want
should give you two ranges that you can check ..
If Sel.InRange(SelF) Or SelF.InRange(Sel) Then
MsgBox "In Field"
Else
Msgbox "Not"
End If
If you actually want the field then you will have to expand the logic a little.

thomas-steth
11-24-2007, 07:10 AM
Tony,

pretty much the same thing,but with simpler code. thank you. But there is one exeptions, which will also fail in your code:
when the cursor
cursor stands in the first field, then the previous will not do anything, and the next jumps right into the next field. actually i was not aware that you can can compare ranges, so this makes things easier. he same note is for the inrange method.

Thank you very much.

Thomas

TonyJollans
11-24-2007, 08:44 AM
Yes, you are right on both counts - it is pretty similar to what you had and, yes, it will fail if in the first field - I did say I hadn't tested it :)