PDA

View Full Version : Problems with Formfield controls



AntM79
08-11-2015, 01:02 AM
Hello all,

I have a Word document that is generated from Crystal reports, inserting text objects in Crystal seems to generate Frames in Word. This document has to have areas that are locked for editing. Those areas have been marked by ~and an at sign for frames that need to be converted to Check box form fields and ~~ for Frames that need to be text formfield controls. The idea and code I wrote works fine for converting the various frames to either text or check form fields but the problem I'm having is that if there is text already in the Frame text box that needs to be brought over into the form field it gets duplicated.

"~~23456789" to this "23456789 23456789" the formfield seems to be inserted within the Frame and the duplicate entry or text is uneditable. I need the duplicate removed!

I have tried a few things but can't seem to find a way around it, any help would be greatly appreciated! I have attached a word document with some examples of the check boxes and text boxes that need converting along with the code in module1. The ones that are causing problems are the type in the example stated here. hope that makes sense.

Thanks in advance for any help

gmaxey
08-11-2015, 03:20 AM
AuntieM,

Try:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
Dim oRng As Range
Dim oFrm As Frame
Dim oFF As formField
Dim strText As String
If Documents.Count > 0 Then
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect "test"
End If
For lngIndex = ActiveDocument.Frames.Count To 1 Step -1
Set oFrm = ActiveDocument.Frames(lngIndex)
Select Case Left(oFrm.Range.Text, 2)
Case "~~"
oFrm.Range.Select
Set oRng = Selection.Range
strText = Mid(oRng.Text, 3, Len(oRng.Text) - 2)
Set oFF = ActiveDocument.FormFields.Add(oRng, wdFieldFormTextInput)
oFF.Result = strText
Case "~@"
oFrm.Range.Select
Set oRng = Selection.Range
Set oFF = ActiveDocument.FormFields.Add(oRng, wdFieldFormCheckBox)
oFF.CheckBox.Value = False
End Select
Next lngIndex
ActiveDocument.Protect wdAllowOnlyFormFields, True, "test"
Else
MsgBox "There is no document open to process."
End If
lbl_Exit:
Exit Sub
End Sub

AntM79
08-11-2015, 04:16 AM
Hi Greg,

Thanks a lot, I just tried the macro and it works fine, I was having so much trouble swapping Frames for form fields :-)

Is there a way to control the tab order of the formfields once the document is locked?

Thanks again,
Antm79

gmaxey
08-11-2015, 04:31 AM
See: http://word.mvps.org/FAQs/TblsFldsFms/SetTabOrder.htm