Results 1 to 13 of 13
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Coding Exercise (Any)

    If you've got the time, consider trying your hand at this little programming exercise: write double-indent increase and double-indent decrease macros. I've done it, so I'm not mooching code off of you. I'd just like to see what you come up with and will gladly post what I've got. It was a rather interesting experience. Here are the requirements:

    1) the increase code will always indent 1 inch from the left and right margins
    2) the double-indented text will always be single spaced.
    3) with the IP in a paragraph (or with any number of paragraphs selected) running the increase code will perform the single-spaced double 1" indentation.
    4) with the IP in a paragraph (or with any number of paragraphs selected) running the decrease code will remove 1" of indentation from the right and left and leave the line spacing as is.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    I did this with my NesterTester two years ago. I certainly had the time. I was in Hartford Conneticut, voted most boring town in the world by me, that week.

    Table-driven. You get to specify the syntactic elements that make the nests (An If must be matched by an Endif, a While must be matched by a Wend etc.) and the level of indentation. It was therefore suited not only to VBA but most other languages as well. ALGOL came to mind ...

    I suppose now I should go dig up the code, right? The essential part was the table and the stack, keeping track of where I was each time a recognisable lexical element came in.

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    Hartford. That's loaded with insurance companies, right?

    I'd like to see your solution, but trim it to just the double increase/decrease part, s'il vous pla
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    JustCallMeAl
    Guest

    Re: Coding Exercise (Any)

    <pre>Public Sub DoubleIndentSingleSpace()

    With Selection.ParagraphFormat
    .RightIndent = InchesToPoints(1)
    .LeftIndent = InchesToPoints(1)
    .LineSpacingRule = wdLineSpaceSingle
    End With

    End Sub

    Public Sub RestoreDoubleIndentSingleSpace()

    Selection.ParagraphFormat.Reset

    End Sub</pre>


  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Coding Exercise (Any)

    I addressed only (4), as Al seems to have covered the other part.

    There is some ambiguity in the case that indentation is presently less than 1", so you choose among these options: the first preserves indents of less than 1", the second removes them.

    <pre>Sub OutDent()
    Dim aPara As Paragraph
    For Each aPara In Selection.Range.Paragraphs
    If aPara.LeftIndent > 72 Then 'greater than 1" in points
    aPara.LeftIndent = aPara.LeftIndent - 72
    ElseIf aPara.LeftIndent = 72 Then 'exactly 1" in points
    aPara.LeftIndent = 0
    End If
    If aPara.RightIndent > 72 Then 'greater than 1" in points
    aPara.RightIndent = aPara.RightIndent - 72
    ElseIf aPara.RightIndent = 72 Then 'exactly 1" in points
    aPara.RightIndent = 0
    End If
    Next
    End Sub</pre>

    <pre>Sub OutDent()
    Dim aPara As Paragraph
    For Each aPara In Selection.Range.Paragraphs
    If aPara.LeftIndent > 72 Then 'greater than 1" in points
    aPara.LeftIndent = aPara.LeftIndent - 72
    ElseIf aPara.LeftIndent <= 72 Then '1" or less in points
    aPara.LeftIndent = 0
    End If
    If aPara.RightIndent > 72 Then 'greater than 1" in points
    aPara.RightIndent = aPara.RightIndent - 72
    ElseIf aPara.RightIndent <= 72 Then '1" or less in points
    aPara.RightIndent = 0
    End If
    Next
    End Sub</pre>

    Is this close, professor?

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    I haven't been to dig it up yet, but I'd go close wih Al, and add the rider from JScher.

    What do you do if Indents are negative, or already partial inches? What to do if the L+R indents add up to 8.5" or more? There are a slew of boundary conditions (which is how macros get out of hand).

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    Well, class, thanks for the submissions. BTW, has anybody
    tried my Envelope code? I need to post one final update, but
    I think I've got much better "extractor" or "pick up" code than
    the Wopr env tool. Granted, I didn't write an new interface
    and all that (the Wopr one is excellent), I just needed the
    code to pick up addresses where MS (and Wopr) fail to do
    so. More on that later if there's any interest.

    Ok, Al first:

    Al, your solution is elegant in it's simplicity. It seems to fit the
    bill quite well except for what I admit was a somewhat unclear
    point. When the user invokes the code (I've got buttons
    that are modified from the stock indent and outdent buttons),
    we need to indent +1, not just 1. No one will ever indent more
    than 2", however. Simply replacing your lines with


    .LeftIndent = .LeftIndent + InchesToPoints(1)
    .RightIndent = .RightIndent + InchesToPoints(1)

    seem to do the trick. So, nicely done, Al. But the really nice
    touch is the paragraph reset. But, (sorry, man) if, the user
    goes in 2", then wants to undo to 1", the reset takes 'em back
    to the left margin. It should work like the Word increase and
    decrease indent. Was I not clear enough on that?

    Don't worry, you still get an A, but this is why I had to say "it
    was an interesting experience". Unfortunately, the .reset (as
    clever as it is) is out.

    Now, Jefferson. We see here another fine thinker
    brining "preventative" code into the mix. Consider first this:
    what if the user has selected paragraphs of various
    indentation and then presses one of the double buttons?
    See the attachment for the error. In order to get around this, I
    just used On Error Resume next. Works fine. For your
    insights you also get an A.

    As to the issued raised by Chris -- what to do with negative
    margins or margins off the page or sub inch sizes -- I say that
    if the user wants the document to look that ugly, they're
    welcome to it. Nobody's that dumb. <img src=/S/hushmouth.gif border=0 alt=hushmouth width=16 height=16> Since you
    did not post any code, you get a B. Sorry, man.

    Thank you guys! Unless you can find a snafu in the
    code below, I'll go with mine. This was educational for me. I
    hope you had a fun too.

    Here's what I came up with:

    <pre>Sub DoubleIndentIncrease()
    If fDocsOpen Then
    If fIPAtEndOfParagraph() Then
    Selection.TypeParagraph
    Call BookMarkAdd("ContinueAfterQuote")
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    End If

    With Selection.Paragraphs
    On Error Resume Next
    .LineSpacing = wdLineSpaceSingle
    .LeftIndent = .LeftIndent + InchesToPoints(1)
    .RightIndent = .RightIndent + InchesToPoints(1)
    End With
    Application.ScreenRefresh 'Not sure why I put this here.
    End If
    End Sub


    Sub DoubleIndentDecrease()
    If fDocsOpen Then
    If fBookMarkExists("ContinueAfterQuote") Then
    Call BookMarkSelect("ContinueAfterQuote")
    Call BookMarkDelete("ContinueAfterQuote")
    Else
    With Selection.Paragraphs
    On Error Resume Next
    .LeftIndent = .LeftIndent + InchesToPoints(-1)
    .RightIndent = .RightIndent + InchesToPoints(-1)
    End With
    End If
    Application.ScreenRefresh
    End If
    End Sub
    </pre>


    The functions used above should be self-expanatory, but I'll
    post if you need to see them.
    Attached Files Attached Files
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    Jefferson,

    Thanks for playing. See my post above to Al. It's really to you all.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    Chris,

    See my post to Al below for the wrap...

    Thanks for your help!
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  10. #10
    JustCallMeAl
    Guest

    Re: Coding Exercise (Any)

    Nope. Doesn't fly. Your "LeftIndent = .LeftIndent + InchesToPoints(1)" does not follow the instructions.

    No. 1 states: "the increase code will always indent 1 inch from the left and right margins." You didn't say from the left indent, but from the left and right margins. Therefore, that is what happens. My increase code "always indent[s] 1 inch from the left and right margins."

    Regarding reset. I realize that this next point could be argued ad infinitum (or is that nauseum?). However, direct formatting is discouraged in any and all Word documents. Therefore, my reset was based on that fact. The user has NOT applied direct formatting up to this point because we are not to use direct formatting. Therefore my Reset does meet the criteria.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    Technically you are correct, and I was unclear. Nevertheless, the requirements are the requirements despite the professor's mistatement (the professor assumed that once the left indent was moved in to 1", the new margin is 1". In fact, that's what the user thinks has happened: the margins have moved in 1"). You got an A for your brains AND meeting the stated requirements, but your code is rejected 'cause the dumb professor was not clear enough.

    How's that? Do you still want to play?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Coding Exercise (Any)

    I'm surprised that On Error Resume Next for a gigantic measurement error message solved the problem. That is truly bizarre.

    I don't think the problem is varying measurements among paragraphs, because I tested it with a selection of three paragraphs with varying measurements. I more suspect that while Office 2000 interprets "72" as points, Office 97 thinks it is something else. But if I am correct on that, then the On Error Resume Next really makes no sense.

    Either way, I think this whole experience can only enhance my resume.

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Coding Exercise (Any)

    Well, Jefferson, just put me down as a reference if your resume needs any more enhancing <img src=/S/yadda.gif border=0 alt=yadda width=15 height=15>
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •