Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-07-2016, 04:09 PM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 06-07-2016, 06:14 PM
Guessed's Avatar
Guessed Guessed is online now VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 06-07-2016, 06:23 PM
macropod's Avatar
macropod macropod is offline VBA Code to Insert Legacy Text Field Windows 7 64bit VBA Code to Insert Legacy Text Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by PSSMargaret View Post
Need help with VBA code that will insert a Legacy text field without a bookmark ID
Why the bookmark concern? Its presence shouldn't be of any consequence.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 06-07-2016, 07:11 PM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 06-07-2016, 08:11 PM
macropod's Avatar
macropod macropod is offline VBA Code to Insert Legacy Text Field Windows 7 64bit VBA Code to Insert Legacy Text Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 06-08-2016, 03:50 AM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default

Thanks Paul. I haven't combined legacy and content controls in the same document. Two separate projects.
Reply With Quote
  #7  
Old 06-08-2016, 03:51 AM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default

Andrew,

Can the code include default text for each of the fields? "Category" for the first field and "Detail" for the second.
Reply With Quote
  #8  
Old 06-08-2016, 05:44 AM
gmayor's Avatar
gmayor gmayor is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #9  
Old 06-08-2016, 07:06 AM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 06-08-2016, 10:08 AM
gmaxey gmaxey is offline VBA Code to Insert Legacy Text Field Windows 7 32bit VBA Code to Insert Legacy Text Field Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 06-09-2016, 01:41 AM
gmayor's Avatar
gmayor gmayor is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It might work even better if you move the line
Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
to a position before Exit Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #12  
Old 06-09-2016, 04:38 AM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 06-09-2016, 04:57 AM
gmayor's Avatar
gmayor gmayor is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #14  
Old 06-09-2016, 05:06 AM
PSSMargaret PSSMargaret is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2010 64bit
Novice
VBA Code to Insert Legacy Text Field
 
Join Date: May 2016
Posts: 28
PSSMargaret is on a distinguished road
Default

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
Reply With Quote
  #15  
Old 06-09-2016, 05:45 AM
gmayor's Avatar
gmayor gmayor is offline VBA Code to Insert Legacy Text Field Windows 10 VBA Code to Insert Legacy Text Field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to Insert Legacy Text Field Insert text form field help sj80 Word 1 01-26-2016 10:50 PM
VBA Code to Insert Legacy Text Field Variable text field code based on occurrences on each page Cosmo Word 2 12-29-2015 11:54 AM
VBA Code to Insert Legacy Text Field Grammar check into legacy forms > Text form field. Eduardo Care Word 2 09-09-2015 03:11 PM
VBA Code to Insert Legacy Text Field 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:44 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft