none
CTRL+SHIFT+UP ARROW and Selection.MoveUp Unit:=wdParagraph Inconsistencies (aka Range vs. Selection)

    Question

  • In my ongoing attempt to chase down all the changes needed to move a fairly complex project from Word 2003 to 2013, I have come across something I find hard to accept.

    As part of a spacing calculation and adjustment, my code selected two photo captions (paragraphs) and reduced the font size by 0.5 points. The end of the second paragraph is selected (in calculating clearance to a photo below); therefore, the paragraph selection was:

    Selection.MoveUp Unit:=wdParagraph, Count:=2, Extend:=wdExtend

    With ShowAll off, I realized only the bottom paragraph was being selected and having the font adjusted. I broke the execution and found the same behavior using Ctrl+Shift+Up Arrow. With ShowAll on, the code (and the key combination) work as expected.

    I created a new document using rand(5,3) and was not able to produce the keystroke behavior until I pasted the style (List Paragraph) from my photo exhibit document to the new document. The inconsistency seems a bit random. With ShowAll off in the new document, Ctrl+Shift+Up Arrow selects the bottom paragraph on the first stroke, but it might take 4 to 10 hits of Ctrl+Shift+Up to add the next parapraph above to the selection.


    GrouseHiker


    • Edited by GrouseHiker Friday, January 31, 2014 1:41 PM
    Sunday, January 26, 2014 11:01 PM

Answers

  • If at all possible, you should avoid trying to mimic keyboard and user actions; better to work directly with the object model, where you can be reasonably sure what you're working with.

    Best would be if the formatting in your documents was controlled by STYLES. If the photo captions are all formatted with a specific style, not used for anything else, then all you'd need to do is change the style's definition - the problem is solved throughout the document(s).

    Barring that, how is the code you're using up to this point identifying that it's in a "photo caption"?

    Because you can do something like:

    Dim rng as Word.Range
    Set rng = Selection.Range.Paragraphs(1) 'Gets the paragraph "around" the range
    rng.MoveStart wdUnit:=wdParagraph, Count:=-1 'Should extend to the previous paragraph, as well
    rng.Font.Size = 10.5


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by GrouseHiker Monday, January 27, 2014 7:16 PM
    Monday, January 27, 2014 4:33 PM
    Moderator

All replies

  • If at all possible, you should avoid trying to mimic keyboard and user actions; better to work directly with the object model, where you can be reasonably sure what you're working with.

    Best would be if the formatting in your documents was controlled by STYLES. If the photo captions are all formatted with a specific style, not used for anything else, then all you'd need to do is change the style's definition - the problem is solved throughout the document(s).

    Barring that, how is the code you're using up to this point identifying that it's in a "photo caption"?

    Because you can do something like:

    Dim rng as Word.Range
    Set rng = Selection.Range.Paragraphs(1) 'Gets the paragraph "around" the range
    rng.MoveStart wdUnit:=wdParagraph, Count:=-1 'Should extend to the previous paragraph, as well
    rng.Font.Size = 10.5


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by GrouseHiker Monday, January 27, 2014 7:16 PM
    Monday, January 27, 2014 4:33 PM
    Moderator
  • Thanks Cindy,

    I had already come to realize all my keystroke mimicking (leftover from "Record Macro") was confusing and sometimes unpredictable. I had been searching for ways to make the conversion (working with ranges), and the code you provided will be put to use immediately. I got it to work in VBA like this:

    Sub ppSelect()
    Dim rng As Word.Range
    Set rng = Selection.Paragraphs(1).Range
    rng.MoveStart Unit:=wdParagraph, Count:=-1
    rng.Select
    End Sub
    I assume the range must be selected before modifying the font.


    GrouseHiker


    • Edited by GrouseHiker Monday, January 27, 2014 7:16 PM
    Monday, January 27, 2014 7:14 PM
  • Hi David

    Very good :-)

    Follow-up: Why do you then use rng.Select?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, January 27, 2014 7:17 PM
    Moderator
  • Your response indicates I was obviously wrong in assuming the range must be selected before the font is modified.

    I just tried your method of modifying the range directly (rng.Font.Size = 10.5), and (as you implied) it works!!!

    rngSelect goes away, and I'm off to fix all my inefficient code.

    Thanks for your continued guidance!


    GrouseHiker

    Monday, January 27, 2014 7:29 PM
  • Hi David

    Sorry, I missed the last line in your previous post, under the code snippet. Yes, you can do almost everything with a Range object that you can with a Selection - no need to jump the cursor around in the document.

    The notable exceptions that spring to mind:

    - working with a table column
    - moving by-line (line break generated by Word's automatic word wrap)
    - working with the old, internal bookmarks (let you work with a specific page, for example)


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, January 28, 2014 8:49 AM
    Moderator
  • Cindy,

    I'm starting to use and understand using ranges versus selections and keyboard mimicking; however, there seems to be a few cases where it seems I will need to select a location (e.g., entering a paragraph or page break). In my Photo Exhibit project, I start out with 100% numbered paragraphs that will all become photo captions. Working from top to bottom (formatting paragraphs, calculating a coding spacing, and mounting photos), a page break is entered to separate each pair of photo captions.

    Is there a more efficient way to do something like this, or will I have to resort to selecting the "cursor" location in some cases?

    david


    GrouseHiker

    Tuesday, January 28, 2014 3:56 PM
  • Obviously there is. I found it here:

    http://msdn.microsoft.com/en-us/library/office/aa212208(v=office.11).aspx

    Set myRange = ActiveDocument.Paragraphs(2).Range
    With myRange
        .Collapse Direction:=wdCollapseEnd
        .InsertBreak Type:=wdPageBreak
    End With

    This thread got a bit off course from the original title, so I modified the title to reflect the range versus selection technique.


    GrouseHiker

    Friday, January 31, 2014 1:41 PM
  • Hi David

    Glad you found it and posted, I somehow missed the reply you posted yesterday. Once a post is marked as an answer it does happen that we miss follow-ups. For new questions (that don't match the original subject) it's usually a good idea to start a new thread - you can always link to an existing one if it contains relevant information - you'll get the best visibility that way :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, January 31, 2014 6:36 PM
    Moderator