Discussion:
Inserting formatted text into a Text Field.
(too old to reply)
Jumping Sheep
2006-02-01 14:16:27 UTC
Permalink
Hi

I am wanting to provide a means of copying formatted text from one
document into the text field of a protected document. By formatted
text I mean text that could include a tab stop.

Here is my code:
Public Sub SetTextInFF()
If Selection.FormFields.Count = 1 Then
'No textbox but a check- or listbox
MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count >
0 Then
'MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Range.Text

Dim Ad As Document
Set Ad = ActiveDocument

Dim bm As Bookmark
Set bm = Selection.Bookmarks(Selection.Bookmarks.Count)


Dim doc As Document
Set doc = Documents.Open("C:\TextEditFF.doc", _
Visible = False)

doc.SelectAllEditableRanges
Dim r1 As Range
Set r1 = doc.Range


Ad.Activate
ActiveDocument.Unprotect
bm.Range.FormFields(1).FormattedText = r1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True


doc.Close
End If
End Sub

This does copy the text across (with tab stop) but unfortunately blots
out the text field. Other variations have ended up with the formatted
text within the bookmark but not the form field. If I just assign to
the text field result the formatting is lost.

Anybody seen a solution to this?

Out of interest, I am able to extract formatted text from a text field
and paste it into another document. Quite messy though.
Cindy M -WordMVP-
2006-02-04 17:45:58 UTC
Permalink
Hi Jumping,

Which version of Word is this? It looks like a fairly old one (from the
way it's letting you use .FormattedText), and older versions of Word can
be a bit "finicky". I wouldn't want to spend time testing in Word 2003,
then come up with something that won't work on an older version...

FWIW, I do have a couple of comments. Not sure they'll make any
difference, though...

1. You have assigned the ActiveDocument to the object variable AD. No
need to activate it, then unprotect ActiveDocument. Just stick with AD:
AD.Unprotect

2. You might try assigning bm.Range to a Range variable (Dim rng as
Word.Range, Set rng = bm.Range), then use that.
-Try rng.Collapse wdCollapseStart, and wdCollapseEnd before picking
up .FormattedRange. That may retain the form field.

- Try rng.Select (put a break point after this line so the code
stops) and look where that puts the selection.

- Try moving the start of the range forward one (or two) characters,
or the end of the range back. That might position the target within the
form field, so that it's not lost.
Post by Jumping Sheep
I am wanting to provide a means of copying formatted text from one
document into the text field of a protected document. By formatted
text I mean text that could include a tab stop.
Public Sub SetTextInFF()
If Selection.FormFields.Count = 1 Then
'No textbox but a check- or listbox
MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count >
0 Then
'MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Range.Text
Dim Ad As Document
Set Ad = ActiveDocument
Dim bm As Bookmark
Set bm = Selection.Bookmarks(Selection.Bookmarks.Count)
Dim doc As Document
Set doc = Documents.Open("C:\TextEditFF.doc", _
Visible = False)
doc.SelectAllEditableRanges
Dim r1 As Range
Set r1 = doc.Range
Ad.Activate
ActiveDocument.Unprotect
bm.Range.FormFields(1).FormattedText = r1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
doc.Close
End If
End Sub
This does copy the text across (with tab stop) but unfortunately blots
out the text field. Other variations have ended up with the formatted
text within the bookmark but not the form field. If I just assign to
the text field result the formatting is lost.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)

Loading...