Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    writing macros (2002)

    I've received a lot of help from this lounge in the past, especially with respect to macros. The last one, from Andrew Savikas, rocked! I'm working on a 300-page dictionary that has loads of cross references, and I'm working on ways to automate checking them. I have an idea of the macros I'll need in about a month, and I'm pretty sure I won't be able to create them myself, so I'll post a question here when the time comes. In the meantime, what I'd really love to know is, How can I learn to write cool macros myself?
    Michael Coleman
    BOMI International

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: writing macros (2002)

    See for example the threads starting at <post#=70521>post 70521</post#> and <post#=142298>post 142298</post#>. Both contain links.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: writing macros (2002)

    Hi Michael,

    In addition to reading the suggested books (and read them again and again; you'll learn more as you know more), here are two, somewhat related, things that, IMHO, will help you write better macros:

    First, get some exposure to at least one other scripting language, like Perl, Python, or Ruby. No need to become a guru -- in fact, any of these languages can have you writing basic stuff in your first sitting -- but enough to get a sense of how different languages solve similar problems.

    Second, learn how to script Word with one of these other languages. It will help you separate out VBA from the Word Object Model; they are, after all, two different things. Word's objects are just items that can be manipulated by a programming language; typically that's VBA, but it's also possible from Perl or Python or Ruby (or any language that offers COM support).

    For example, here's the macro I wrote for you, implemented in Ruby:

    <pre>require 'win32ole'

    print "Enter initials: "
    initials = gets.chomp()

    if initials.length() == 0
    exit
    end
    wd = WIN32OLE.new('Word.Application')
    wd.Visible = 1
    doc = wd.Documents.open("C:MyDocWithComments.doc")
    docComments = wd.Documents.add()
    doc.Comments.each do |c|
    if c.Range.Text[0..2].upcase() == initials.upcase()
    c.Reference.Paragraphs.each do |para|
    para.Range.Copy
    docComments.Range(docComments.Range.End - 1).Paste
    end
    end
    end
    </pre>


    Even though it's a different language, notice how similar much of it is -- just a different way of manipulating those same Word objects. There's no VBA here, but several of the lines are identical to their VBA counterparts.

    Read, read, read (easy for me to say, if you notice where I work <img src=/S/grin.gif border=0 alt=grin width=15 height=15>), and hack, hack, hack (ditto).

    BTW, if anyone out there has Ruby installed (it's free), save the above as "wdlistcomments.rb", make sure you've got a document named "C:MyDocWithComments.doc", and that it's got a few comments that start with intitals (as in "ABC - this is my comment..."), and you can run the above from a DOS command line:
    <pre>> ruby wdlistcomments.rb
    </pre>


  4. #4
    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: writing macros (2002)

    This board is something like a course in macro-writing. You get the problem, then you get several variations on solutions. As you work through them, you should get a lot of insight into how to solve common Word problems. Some of the problems on the VBA board are more advanced, butn ot all of them.

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: writing macros (2002)

    I second Jefferson. I haven't been on this board very long, but it's been long enough to realize it's a diverse community of competent, intelligent people, who recognize the value in sharing ideas and information, and truly enjoy helping others learn.

Posting Permissions

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