#1
|
|||
|
|||
VBA Code to Insert Legacy Text Field
Need help with VBA code that will insert a Legacy text field without a bookmark ID and with the Text Format: First Capital. I already have the code to un-protect the form and then turn protection back on after the field has been added so I can add that.
I created a macro (below) but it's not working. It didn't record the steps I clicked to remove the bookmark and change the format to first capital and seems to have added some spaces. Any help would be greatly appreciated. Margaret HTML Code:
Sub test() ' ' test Macro Selection.EndKey Unit:=wdLine Selection.TypeParagraph Selection.FormFields.Add Range:=Selection.Range, Type:= _ wdFieldFormTextInput Selection.Font.Bold = wdToggle Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeText Text:=":" Selection.Font.Bold = wdToggle Selection.TypeText Text:=" " Selection.FormFields.Add Range:=Selection.Range, Type:= _ wdFieldFormTextInput Selection.HomeKey Unit:=wdLine End Sub |
#2
|
||||
|
||||
Try this.
Code:
Sub test() Selection.EndKey Unit:=wdLine Selection.TypeParagraph Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput Selection.Font.Bold = True Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeText Text:=":" Selection.Font.Bold = False Selection.TypeText Text:=" " 'this is where the spaces were added Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput Selection.HomeKey Unit:=wdLine Dim aFld As FormField For Each aFld In Selection.Paragraphs(1).Range.FormFields aFld.TextInput.EditType Type:=wdRegularText, Default:="", Format:="First capital" Next aFld Do While Selection.Paragraphs(1).Range.Bookmarks.Count > 0 Selection.Paragraphs(1).Range.Bookmarks(1).Delete Loop End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
||||
|
||||
Why the bookmark concern? Its presence shouldn't be of any consequence.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Thank you Andrew. This is exactly what I've been trying to develop myself with no luck. I learn something every time an expert like you takes the time to assist me.
Have a great evening. Margaret |
#5
|
||||
|
||||
Margaret: I notice that, in your other thread (https://www.msofficeforums.com/word-...t-control.html), you're working with content controls.
Do be aware that content controls and formfields weren't designed to work together and can lead to problems if you use them in the same document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thanks Paul. I haven't combined legacy and content controls in the same document. Two separate projects.
|
#7
|
|||
|
|||
Andrew,
Can the code include default text for each of the fields? "Category" for the first field and "Detail" for the second. |
#8
|
||||
|
||||
It is easy enough to include the default text, but I personally would do things slight differently embracing ranges.
Code:
Option Explicit Sub CreateTextField() Dim oRng As Range Dim aFld As FormField Set oRng = Selection.Range With oRng .End = .Paragraphs(1).Range.End + 1 .Collapse 0 .Text = vbCr .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Category", Format:="First capital" .End = aFld.Range.End .Font.Bold = True .Collapse 0 .Text = ": " .Font.Bold = False .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Detail", Format:="First capital" .End = aFld.Range.End .Collapse 0 'why do you need this? The bookmarks shouldn't be an issue 'Do While .Paragraphs(1).Range.Bookmarks.Count > 0 ' .Paragraphs(1).Range.Bookmarks(1).Delete 'Loop .Select End With lbl_Exit: Set oRng = Nothing Set aFld = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
|||
|
|||
Graham,
It works great when I insert it in a blank document and run it but when I replace my current code in between the code to unlock and lock the form it's not working. Code:
Sub CreateTextField() ActiveDocument.Unprotect Dim oRng As Range Dim aFld As FormField Set oRng = Selection.Range With oRng .End = .Paragraphs(1).Range.End + 1 .Collapse 0 .Text = vbCr .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Category", Format:="First capital" .End = aFld.Range.End .Font.Bold = True .Collapse 0 .Text = ": " .Font.Bold = False .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Detail", Format:="First capital" .End = aFld.Range.End .Collapse 0 'why do you need this? The bookmarks shouldn't be an issue 'Do While .Paragraphs(1).Range.Bookmarks.Count > 0 ' .Paragraphs(1).Range.Bookmarks(1).Delete 'Loop .Select End With lbl_Exit: Set oRng = Nothing Set aFld = Nothing Exit Sub ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End Sub |
#10
|
|||
|
|||
What is not working?
Code:
Sub CreateTextField() 'With the exception of this minor change you code seems to work fine. If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Dim oRng As Range Dim aFld As FormField Set oRng = Selection.Range With oRng .End = .Paragraphs(1).Range.End + 1 .Collapse 0 .Text = vbCr .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Category", Format:="First capital" .End = aFld.Range.End .Font.Bold = True .Collapse 0 .Text = ": " .Font.Bold = False .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Detail", Format:="First capital" .End = aFld.Range.End .Collapse 0 'why do you need this? The bookmarks shouldn't be an issue 'Do While .Paragraphs(1).Range.Bookmarks.Count > 0 ' .Paragraphs(1).Range.Bookmarks(1).Delete 'Loop .Select End With lbl_Exit: Set oRng = Nothing Set aFld = Nothing Exit Sub ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End Sub |
#11
|
||||
|
||||
It might work even better if you move the line
Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#12
|
|||
|
|||
I think it's positional. If my cursor is on the first line of fields and I want to insert another set of fields below the first line (first example below)
Category: Detail Category: Detail Category: Detail This is what is does after I run the macro. Category: Detail : Detail Category: Detail If I'm on the last row of fields and run the macro it works but it's picking up the paragraph spacing for the field below these fields. The Category and Details fields have 3 pt paragraph spacing after. The field below the Category and Details fields has a 24 pt paragraph spacing before it. Category: Detail Category: Detail Category: Detail Category: Detail |
#13
|
||||
|
||||
The macro purports to provide the functionality that your original macro failed to provide. However have not seen your document and only have a vague idea of what you are trying to do based on that original macro.
The issues that you mention can be addressed, by setting the range and its values appropriately, but unless we know exactly what you have and what you want to get from it, we are simply shooting in the dark.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#14
|
|||
|
|||
My apologies if I'm not providing enough information. I would like the macro to insert a new set of Category and Details fields below the current cursor position with the same paragraph space as the Category and Details fields which is 3 pt after the paragraph. So, if I'm on any of the Category and Details fields it should work fine by inserting a paragraph return and then inserting the fields; however, if I'm on the last line of Category and Details fields and hit return, it's picking up the paragraph spacing from the Notes & Details field which has 24 pt. before the paragraph.
Category: Detail Category: Detail Category: Detail Category: Detail Category: Detail Category: Detail Category: Detail Category: Detail Notes: Detail I hope this clears it up. Margaret |
#15
|
||||
|
||||
OK - That helps
Try the following Code:
Option Explicit Sub CreateTextField() Dim oRng As Range Dim aFld As FormField If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Selection.Paragraphs(1).Range.InsertParagraphAfter Set oRng = Selection.Paragraphs(1).Range.Next.Paragraphs(1).Range With oRng .ParagraphFormat.SpaceBefore = 3 .Collapse 1 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Category", Format:="First capital" .End = aFld.Range.End .Font.Bold = True .Collapse 0 .Text = ": " .Font.Bold = False .Collapse 0 Set aFld = .FormFields.Add(Range:=oRng, Type:=wdFieldFormTextInput) aFld.TextInput.EditType Type:=wdRegularText, Default:="Detail", Format:="First capital" .End = aFld.Range.End .Collapse 0 'why do you need this? The bookmarks shouldn't be an issue 'Do While .Paragraphs(1).Range.Bookmarks.Count > 0 ' .Paragraphs(1).Range.Bookmarks(1).Delete 'Loop .Select End With lbl_Exit: ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True Set oRng = Nothing Set aFld = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Insert text form field help | sj80 | Word | 1 | 01-26-2016 10:50 PM |
Variable text field code based on occurrences on each page | Cosmo | Word | 2 | 12-29-2015 11:54 AM |
Grammar check into legacy forms > Text form field. | Eduardo Care | Word | 2 | 09-09-2015 03:11 PM |
legacy Form Field | sunrise06 | Word | 9 | 05-02-2015 06:48 PM |
Filling in legacy field causes rest of line to move to next tab stop | rtrdom | Word | 5 | 12-14-2013 05:48 PM |