Re: Range Madness and Other Frustrations
- From: "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 24 Oct 2006 19:50:28 -0400
Dian,
Thanks. Your complete abandonment of range is a bit too radical for me, but
I was able to put together a composite that seems to work really well:
Sub OnExitQuestion()
Dim oRng As Word.Range
Dim oFF As FormFields
With ActiveDocument
If .ProtectionType <> wdNoProtection Then .Unprotect
Set oFF = .FormFields
Select Case oFF("PriAnswer").Result
Case Is = "To get to the other side"
'If the secondary question and formfield already exists then skip
creation. If not, create the secondary question and redefine the bookmark
If Not .Bookmarks.Exists("SecAnswer") Then
Selection.GoTo What:=wdGoToBookmark, Name:="FUQ"
.AttachedTemplate.AutoTextEntries("FUQ").Insert Selection.Range, True
Set oRng = Selection.Range
oRng.Start = .Bookmarks("FUQ").Start
.Bookmarks.Add "FUQ", oRng
End If
.Bookmarks("SecAnswer").Range.FormFields(1).Select
Case Else
Set oRng = .Bookmarks("FUQ").Range
oRng.Text = ""
.Bookmarks.Add "FUQ", oRng
End Select
If .ProtectionType <> wdAllowOnlyFormFields Then
.Protect wdAllowOnlyFormFields, True
End If
End With
End Sub
If anyone else is ready this then I am still interested in explanations for
the seemingly wierd behaviour with decribed earlier.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Dian D. Chapman, MVP wrote:
You're right...ranges are a PITA. I avoid them at all cost. ;-)
I had to do a LOT of AutoText and File inserts in this project:
http://www.mousetrax.com/Consulting_Solutions.html#reviews
So, although I have all separate subs for InsertAutoText, Rewrap and
GoToBookmark in the modMain for this...I've pulled the code out into
static code below so it can be run in one shot and I commented out the
other stuff in the sub that doesn't apply here.
Rather than messing with ranges, I went the weenie route<g>. Since you
KNOW the cursor will end up at the end of the insert, you can move one
char back to get to the end of the previous line. Then you select back
to the original bookmark. Now you have the current insert selected.
Then just delete the original bookmark and, since you've passed that
name into a variable, you still have it within the procedure, so you
can readd it now that you have the entire area selected. This rewraps
the content with the same bookmark so the info can be changed if
needed in an update (the project was an employee review that would
most likely be often updated).
And if you don't NEED the bookmark cos' the info is NO, you can just
delete it...or do what I did...style that bookmark para with a bogus
1pt font so no one but you will even know it's there cos' it's too
tiny to see. ;-)
Jay or Bill could probably come up with something slicker...but this
worked great for me for the MANY inserts and updates that were needed
for the several pages of review data needed in the docs.
Sub InsertAndWrapAutoText(strBookmark As String, _
strAutoText As String)
'dian@xxxxxxxxxxxxx
'PURPOSE: inserts autotext content into bookmarked location
'rewraps w/same bookmark
'**********************
'On Error GoTo Handler
'unlock doc
'modMain.ToggleFormLock
'go to bookmark location
Selection.GoTo What:=wdGoToBookmark, Name:=strBookmark
'insert autotext
ActiveDocument.AttachedTemplate.AutoTextEntries(strAutoText) _
.Insert Where:=Selection.Range, RichText:=True
'move cursor back one char to end of this last insertion point
Selection.MoveLeft unit:=wdCharacter, Count:=1
'turn on EXT (selection function)
Selection.Extend
'move back up to starting bookmark, which selects this insert
Selection.GoTo What:=wdGoToBookmark, Name:=strBookmark
'delete original single bookmark
ActiveDocument.Bookmarks(strBookmark).Delete
'rewrap w/double bookmark
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=strBookmark
End With
'relock doc
'modMain.ToggleFormLock
'GoTo done
'Handler:
' gstrErrorString = "InsertAndWrapAutoText:modMain"
' modErrorHandler.ErrorLog gstrErrorString
'done:
End Sub
Dian D. Chapman
Technical Consultant, Microsoft MVP
MOS Certified, Editor/TechTrax
Free MS Tutorials: http://www.mousetrax.com/techtrax
Free Word eBook: http://www.mousetrax.com/books.html
Optimize your business docs: http://www.mousetrax.com/consulting
Learn VBA the easy way: http://www.mousetrax.com/techcourses.html
.
- Follow-Ups:
- Re: Range Madness and Other Frustrations
- From: Dian D. Chapman, MVP
- Re: Range Madness and Other Frustrations
- References:
- Range Madness and Other Frustrations
- From: Greg Maxey
- Re: Range Madness and Other Frustrations
- From: Dian D. Chapman, MVP
- Range Madness and Other Frustrations
- Prev by Date: Re: Forms / Send to
- Next by Date: *Another* MoveDown bug in Word?
- Previous by thread: Re: Range Madness and Other Frustrations
- Next by thread: Re: Range Madness and Other Frustrations
- Index(es):
Relevant Pages
|