Link to home
Start Free TrialLog in
Avatar of srlapides
srlapides

asked on

Problem with Selection.Information(wdVerticalPositionRelativeToPage) in MS-Word.

I have a VB application which is generating a Word document.  It uses Selection.Information(wdVerticalPositionRelativeToPage) to assist in it's processing.  It works fine when Word is visible and not minimized, but when we minimize Word (for performance reasons - our benchmarks indicate it does generate faster), the information returned is not always correct, causing erroneous processing.  Can you suggest a workaround.  BTW, I did try the Microsoft knowledgebase, but didn't find anything.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Try using a range object instead of the Selection object. (Always a good idea).
If you are looking to speed up your macro processing I typically Turn Off Screen Updating:

        Application.ScreenUpdating = False

Then turn it back on at the end of processing:

    'Manually Refreshes the Screen
        Application.ScreenRefresh

    'Restarts Screen Refreshing
        Application.ScreenUpdating = True

For more information try this article too:

How to speed up Word Automation by hiding the application
http://word.mvps.org/FAQs/InterDev/MakeAppInvisible.htm

NOTE:  Do not Minimize the Screen using the ScreenRefresh as it will not benefit further
Avatar of srlapides
srlapides

ASKER

I got the same erroneous result using the Range object.
What sort of inaccuracies are they?

Perhaps we should be sure that your Word installation is working correctly, so try these troubleshooting steps.

http://www.officearticles.com/word/steps_to_troubleshooting_microsoft_word.htm
What happens is that the Selection (or Range).Information is incorrect.  We use wdVerticalPositionRelativeToPage to determine when to take a page break, so we can regenerate headers and subheaders.  I've used debug.print to see what's happening - it returns a high enough value that our report starts taking page breaks every line.  To repeat, this happens only when Word is minimized.  

Here's an excerpt from another entry in your Knowledgebase with the same observation (see the last line):

-----------------------------------------------------------------------------------------------------------------------------

Question: I have a big problem with this functionality.
I need to use it ,because ,I create a lot of tables(dinamicly) in Word Document (in VB code)
And I check :
--------------------------------------------------------------------------------------------
If (objDoc.ActiveWindow.Selection.Information(wdVerticalPositionRelativeToPage) > 410) Then
    objDoc.ActiveWindow.Selection.InsertBreak Type:=wdPageBreak
End If
-------------------------------------------------------------------------------------------
It's mean I want to  start a new table on the new page if it's remain a little piece on current page.

But when it's become 488(i Don't know how,because it's already a new page ),after that it's not change never.And always equal to 488 .How can I solve this ,that this functionality will work correctly.
May be somebody had same  problem.
Or maybe somebody has a new solution .Please help me ,it's very urgent for me !!!

p.s. Document must be unvisible .I checked if the Document is visible ,it's work right ?!?!?!?!?
Perhaps a frequent Repaginate call will help with the accuracy (though not with the performance)

Note that there is built-in paragraph and other formatting functionality such as Widow-control and Keep Together/With Next that might be able to do that sort of job automatically.
The long and short of it is that this is a long standing Microsoft bug that we're all trying to work around.  I don't like turning off ScreenUpdating (I admit it's a performance improvement to do so) but if there's a crash with no error recovery, then the Word window is empty space.  And you get the same effect by selecting it in the taskbar list while it's generating.  Pretty ugly.

I had already tried RePaginate but that didn't help either.  But the code below does seem to work.  We offer the user an option to do alternate row shading for readability - when we do that, we never see the problem.  When alternate shading is not selected, we now still go though the mechanics of doing it - that seems to work, though it is a performance hit.  I was hoping for some other sequence of instructions for the no shading environment that was more efficient.  Here's the code we're using:

    With MyWP.Selection
        .EndKey Unit:=wdLine, Extend:=wdExtend
        With .ParagraphFormat.Shading
            LineCount = LineCount + 1
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorAutomatic
            If Not NoShading And LineCount Mod 2 = 1 Then
                .Texture = wdTexture10Percent
            Else
                .Texture = wdTextureNone
            End If
        End With
        .Collapse 0
    End With
    Return
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial