Results 1 to 9 of 9
  1. #1
    New Lounger
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA for Word & Document Conversion (VBA/Word/2000)

    Hi all.

    I'm rather new to VBA, and have written some small things for Excel, and now I'm trying to fumble my way through writing a small app for Word. I have many years of programming experience under C/C++ (though not any visual work, all backend work for compilation and some embedded systems).

    I'm trying to write a VBA app for word that will convert a document to a text file with some embedded tags.

    For example, if something is in italics, it would turn into [it]the text[/it]. If it's a bulleted list, it would turn into [list]

    If you think this looks a lot like HTML you're right. It's actually for an in-house webboard we use to throw ideas around.

    My coworkers want to convert their word files to post on the board, rather than post attachments and such.

    I have looked through VBA's help in regards to word, and I think I need to go through character by character (Can't do it by word, on the assumption that a single word may not all be italics, for example. Sounds crazy, but don't question my user's weirdness <img src=/S/smile.gif border=0 alt=smile width=15 height=15>). I can pick up proprerties such as Bold, Italics, etc. No problems... But, I can't figure out for the life of me other complex things - like how do I know if a character is in a bulleted list? And if it is, what item is it in in the list? I can see that there are ways to get all the list objects and then iterate through them, but that doesn't give me any context of where in the document they are.

    Or, it could be that I'm completely off base.

    I'm limiting the conversion, at present, to bold, italics, centering, and (I'd like...) lists. Things like tables are too obscene to think about at the moment, but most of my coworkers don't use them anyway, so for now it's OK.

    Thanks for any assistance you can offer.

    Harry

  2. #2
    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: VBA for Word & Document Conversion (VBA/Word/2

    While you certainly could go through character by character, if you only have a limited set of attributes that you wish to preserve, then that would be overkill. It might be easier to save to the RTF format and then work with that.

    A simple (if repetitive) approach would be to use Word's built-in Find feature, looking for formatting attributes and tagging the text as you go. For an example, see <post#=286765>post 286765</post#>.

    Added: To take best advantagte of VBA in the Office environment, the key thing is to explore the applications' object models, find the analogues to the features present in the UI, and then find the even-more-efficient ways to use them in VBA. If you find that you aren't leveraging the editor's unique features, or are not comfortable doing so, then it might be more efficient to generate a text file (e.g., RTF or filtered HTML) and work with that. There's really no telling which will be more efficient in the end...

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word & Document Conversion (VBA/Word/2

    As an experienced programmer, you should come to speed rather quickly by reading Steve Roman's book Writing Word Macros.

    Also, see http://www.standards.com/index.html?WordVBABooks.

  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: VBA for Word & Document Conversion (VBA/Word/2

    Here's a sample VBA procedure for the lists. It inserts "Lounge-style" markup around the list items; the list then can (should?) be changed to normal text but I didn't take that step. It does not know how to deal with outline-level lists. Not yet, anyway. Obviously you can change the markup to suit your needs.

    For tables, you may be in luck. Check out this star post: Copying a Word, Excel or Access table to a post.

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: VBA for Word & Document Conversion (VBA/Word/2000)

    A completely different approach to solving this problem is to get Word to give you the HTML code for a document and then to search that string for tags.

    <code>ActiveDocument.HTMLProject.HTMLProjectItems( 1).Text</code>
    will return a very long text string with the entire document formatted as HTML.

    You can parse this string, looking for HTML tags, which may be easier than looking at the format of every single character in the word document.

    StuartR

  6. #6
    New Lounger
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word & Document Conversion (VBA/Word/2

    Hi all,

    Thanks for the amazingly quick responses. For one, I'll check out the book recommendation, good print media is always nice to thumb through and keep around as a reference.

    Jefferson, thanks for posting the sample, I'll grab it and take a look at it. It was nice of you to do so.

    I didn't realize you could treat the document as HTML (as per Stuart's suggestion). That has some good possibilities.

    I was trying to avoid having the person do a Save-As and then post processing the document. My goal was to have them load their document into Word, click a button for a macro, and have the file converted and saved for them (Hmmm, or maybe opened up in a new document with the text they can just copy and then paste into the webboard, that sounds like a better idea).

    Thanks again everyone.

  7. #7
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA for Word & Document Conversion (VBA/Word/2

    Hi Jefferson
    I'll probably smack my forehead when you reply, But, what does UI stand for?

    TIA
    <hr>the analogues to the features present in the UI, and then find the even-more-efficient ways to use them in VBA<hr>
    Regards
    Don

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: VBA for Word & Document Conversion (VBA/Word/2

    Since Jefferson isn't online at the moment.

    UI = User Interface
    and
    GUI = Graphical User Interface

    StuartR

  9. #9
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VBA for Word & Document Conversion (VBA/Word/2

    <img src=/S/stupidme.gif border=0 alt=stupidme width=30 height=30> <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Regards
    Don

Posting Permissions

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