Macro runs different in small doc vs. large

M

Mark Tangard

Hi gang. The code below is intended to remove all "optional hyphens" in
a document that aren't being "used" (that is, aren't at a line break
doing their job). It compares the vertical position of each found
hyphen with the vertical position of the character following it and
deletes the hyphen if those 2 positions are different.

It works in a small file (a page or so), and it works in a large file if
run step by step from the VBE (which of course, is too slow to provide
any benefit.). It *doesn't* work if run from the document window on a
document of any appreciable size. In that case, *all* the soft hyphens
are removed.

I've tried this code with ^31 (ascii for soft hyphen) where "^-" is
shown below (for .Text), and have tried it with and without codes
displayed. No change in either case. Any ideas?

BTW, I haven't gone batty, there's actually a reason for needing this to
happen....

Thanks for any clues.

--
Mark Tangard, Microsoft Word MVP
"Life is nothing if you're not obsessed." --John Waters


Sub DP_ZapInactiveSoftHyphens()
If Documents.Count = 0 Then Exit Sub
Dim i As Long, r As Range, r1 As Range, r2 As Range
Set r = Selection.Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^-"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWildcards = False
Do While .Execute
Set r = Selection.Range
r.MoveEnd wdCharacter, 1
Set r1 = ActiveDocument.Range(r.Start, r.Start)
Set r2 = ActiveDocument.Range(r.End, r.End)
r.MoveEnd wdCharacter, -1
If r1.Information(wdVerticalPositionRelativeToPage) _
= r2.Information(wdVerticalPositionRelativeToPage) Then
r.Characters(1).Delete
End If
Set r1 = Nothing
Set r2 = Nothing
Loop
End With
MsgBox "Done."
End Sub
 
M

Mark Tangard

One important addition: It fails to do its job ONLY in paragraph that
are wrapped around a frame or textbox.

I guess I need a property that indicates whether there's an obstacle
pushing the text aside. Is there such a thing? I don't see one....

TIA again
Mark Tangard
 
J

Jezebel

Nothing springs to mind as a cause of the problem, but in searching for a
work-around, I would try:

a) Use StoryRange objects rather than the Selection object.

b) Make Word invisible while the code is running

c) Work backwards: start at the end of the document and search upwards.
 
W

Word Heretic

G'day Mark Tangard <Mark@NoMailPlease_Tangard.com>,

You HAVE gone batty - completely BONKERS.

IF you are trying to remove soft hyphens unless they are actually in
use as a line break, try this, its just a cleaned version of yours. It
doesn't interfere with the selection or the GUI FnR settings, uses one
less range and is a bit more legible :)


Option Explicit

Sub DP_ZapInactiveSoftHyphens()
'Twiddles by the Word Heretic for Mark Tangard

'Dec
Dim SoftHyphen As Range
Dim CharAfter As Range
Dim Source As Document
Dim SoftEnd As Long


'Init
If Documents.Count > 1 Then

Set Source = ActiveDocument
Set SoftHyphen = Source.Content
SoftHyphen.Collapse
With SoftHyphen.Find
.Text = "^-"
.Wrap = wdFindStop
End With


'main

While SoftHyphen.Find.Execute(Replace:=wdReplaceNone)

'SoftHyphen is always on the 'top' line.

SoftEnd = SoftHyphen.End
Set CharAfter = Source.Range(SoftEnd, SoftEnd + 1)

If SoftHyphen.Information(wdVerticalPositionRelativeToPage) _
= CharAfter.Information(wdVerticalPositionRelativeToPage) Then

SoftHyphen.Delete

Else

SoftHyphen.Collapse wdCollapseEnd

End If 'same line
Wend

Application.StatusBar = "Soft hyphenation removed unless across
line break"

Set CharBefore = Nothing
Set CharAfter = Nothing
Set SoftHyphen = Nothing
Set Source = Nothing
End If 'No doc
End Sub




Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Mark Tangard reckoned:
Hi gang. The code below is intended to remove all "optional hyphens" in
a document that aren't being "used" (that is, aren't at a line break
doing their job). It compares the vertical position of each found
hyphen with the vertical position of the character following it and
deletes the hyphen if those 2 positions are different.

It works in a small file (a page or so), and it works in a large file if
run step by step from the VBE (which of course, is too slow to provide
any benefit.). It *doesn't* work if run from the document window on a
document of any appreciable size. In that case, *all* the soft hyphens
are removed.

I've tried this code with ^31 (ascii for soft hyphen) where "^-" is
shown below (for .Text), and have tried it with and without codes
displayed. No change in either case. Any ideas?

BTW, I haven't gone batty, there's actually a reason for needing this to
happen....

Thanks for any clues.

Sub DP_ZapInactiveSoftHyphens()
If Documents.Count = 0 Then Exit Sub
Dim i As Long, r As Range, r1 As Range, r2 As Range
Set r = Selection.Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^-"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWildcards = False
Do While .Execute
Set r = Selection.Range
r.MoveEnd wdCharacter, 1
Set r1 = ActiveDocument.Range(r.Start, r.Start)
Set r2 = ActiveDocument.Range(r.End, r.End)
r.MoveEnd wdCharacter, -1
If r1.Information(wdVerticalPositionRelativeToPage) _
= r2.Information(wdVerticalPositionRelativeToPage) Then
r.Characters(1).Delete
End If
Set r1 = Nothing
Set r2 = Nothing
Loop
End With
MsgBox "Done."
End Sub
 
P

Peter Hewett

Hi Steve

What are you coming down with? That's the biggest chunk of code I've ever
seen you post <bg> Even if it's twiddled!

Peter
 
W

Word Heretic

G'day Peter Hewett <[email protected]>,

LOL! This lot has been publicly posted a few times. I _was_ going to
turn it into a tool, then realised its a DAMN sight more useful as a
customizable set of macros - writing a GUI to give this lot their
flexibility will be a nightmare. So I just released several years of
research for zippo in the hope that folks will keep their hair around
list numbering :)

Enjoy!

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Peter Hewett reckoned:
 
M

Mark Tangard

Steve,

Quite nice. Thanks. Everyone here at the asylum thanks you too. I'm
*so* glad that nobody flat-out asked why this was needed, but I should
perhaps mention it originated not from anyone here in the compound but
from the demands of an eccentric client in our beloved state government.

No, not Arnold.

Must confess, I don't see why mine didn't work, unless there's a point
of overusing the selection beyond which Word just vomits. My earliest
attempts were done entirely with ranges but came out even worse.

Thanks again,

MT
 
W

Word Heretic

G'day Mark Tangard <Mark@NoMailPlease_Tangard.com>,

Ah. State Gov. Yes, my main client is the Firies over here (State
Gov). I know exactedly what you talk about poor bastard. I am glad I
could help someone who helps others as oft as you do with nothing more
than a

<shameless self-promotion / aggressive marketing on again>

simple optimisation based on my Word VBA Beginner's Spellbook and
naught else :) I swear, its worth its weight in gold. (30 pages dont
weigh that much eh? :) )

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Mark Tangard reckoned:
 
W

Word Heretic

G'day Peter Hewett <[email protected]>,

Don't worry about my prev reply, I got muddled up inbetween 1k posts.
I am happy to do so much for someone like Mark only because I see him
YEAR IN AND YEAR OUT helping the ass off folks. Most others my
rewrites are commercial only. Obv one can see the results immediately
so this is why its not for free anymore...

Beer is good ... need more beer, donations geelfully accepted. Mark
your donations c/o The Word Heretic's Liver Fund ... blah blah blah
:)

Life is a good thing

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Peter Hewett reckoned:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top