Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Macro to format multiple documents (2K and XP)

    The folks who run the training program here at the industrial site where I work have asked me to help them do something neither of us knows how to do. They have about 1200 lessons that were stored in some legacy program. They're going to trash the program, but they've salvaged the documents as text files. The next step is to format these documents, and they want to use Word. Of course, formatting 1200 documents might take from now till Christmas. They plan to use a macro to handle the formatting chore, but even that approach will take forever if they do it one document at a time.

    So...what they've asked me is if there's a way to automate this entire process. That is, they'd like to set up a process that will automatically take on the entire batch of 1200 text files and format each of them. I don't have a clue where to begin--anybody got some suggestions?

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

    Re: Macro to format multiple documents (2K and XP)

    Lucas,

    As you must surely be aware of yourself, your question is very broad and vague. If all 1200 text files are laid out exactly the same way, with a well-defined structure, it might be possible, although it still would be a lot of work to fine-tune the code. But if the structure varies from file to file, it would be hopeless.

    The basic idea would be:
    <UL><LI>Search until you find some fixed element. There can be several kinds of these - headings, subheadings, remarks, questions, .... Which ones? It depends on the contents of your documents.
    <LI>Select the appropriate text.
    <LI>Apply the desired paragraph or character style (wherever possible, use styles instead of direct formatting, this makes it much easier to change the way the document looks later on.)
    <LI>Repeat until you reach the end of the document.[/list]Make sure that you're always working on copies of the files. You won't get this right the first time (and that's nothing to be ashamed of.)

    BTW, Word has an AutoFormat option in the Format menu. I've seldom had much luck with it, but you might let it loose on a copy of one of the documents and see what happens.

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Macro to format multiple documents (2K and XP)

    Hello, Hans

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

    Re: Macro to format multiple documents (2K and XP)

    Lucas,

    Writing the part that loops through files in a folder is not difficult. The "skeleton" is like this:

    Sub LoopThroughFolder()
    Dim strPath As String
    Dim strFile As String
    Dim oDoc As Document

    strPath = "C:Test"
    strFile = "*.txt"
    strFile = Dir(strPath & strFile)
    Do While Not strFile = ""
    Set oDoc = Documents.Open(FileName:=strPath & strFile, Format:=wdFormatText)
    ' Call routine to format document here
    oDoc.SaveAs FileName:=strPath & Left(strFile, Len(strFile) - 3) & "doc", FileFormat:=wdFormatDocument
    oDoc.Close SaveChanges:=wdDoNotSaveChanges
    strFile = Dir
    Loop
    Set oDoc = Nothing
    End Sub

  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: Macro to format multiple documents (2K and XP)

    Another possibility is not to create Word documents, but instead to convert to HTML. The same kind of code would parse and "style" the document, but by "tagging" the various parts of the document (e.g., Big Heading, Topic Heading, Body, etc.) with appropriate tags, they can separate the question of what characteristics each of those parts should have, and use a single "training" style sheet to apply that decision to all of the documents. One thing that's nice about HTML display programs is that they doesn't care about extra line breaks (in other words, they ignore them), so the clean up from "80 column plain text" if that's what you have, might be simpler.

  6. #6
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Macro to format multiple documents (2K and XP)

    Hello again, Hans

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Macro to format multiple documents (2K and XP)

    Thanks for that suggestion, Jefferson! HTML sounds interesting, but I know almost nothing about using it. I am also confident that the people in charge of this project know very little about HTML either. I think we'll do OK if we just fix the problem with margins

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

    Re: Macro to format multiple documents (2K and XP)

    Hi Lucas,

    If you end up with tens or hundreds of subfolders, it might become attractive to let the code loop through a folder with all its subfolders, but if the number is limited, it's easier and safer to do it a folder at a time, as you write. What you must do is replace that line with the instructions needed to set the margin. It could look like this (you'll probably recognize a part - modified of course - of the macro you recorded):

    With oDoc.PageSetup
    .LeftMargin = InchesToPoints(1)
    .RightMargin = InchesToPoints(1)
    End With

    >> Another question: I'm not sure I see exactly where in the code to find the "routine to format document." Is the routine "wdFormatDocument"?

    Since I had no idea what would have to be done, I only put a comment in the macro at the point where the document would have to be formatted. My previous reply only contains code to loop through the text files in a folder, open them and save them as Word documents. The "wdFormatDocument" tells the SaveAs instruction to save the file as a document (necessary because we opened a text file.)

  9. #9
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Macro to format multiple documents (2K and XP)

    Thanks, Hans! Now I'll try to make it work. (I may be back....)

  10. #10
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Macro to format multiple documents (2K and XP)

    I'm already back. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> I've set up the code (I think). When I'm working with VBA in Access, I fire event procedures with On Update or OnGotFocus or whatever. In Word...well, where do I light the fuse? That is, how do I tell Word or VBA to start this looping procedure? I tried using F5 (Run Sub/UserForm), but nothing happened.

    By the way, I quite unintentionally misled you earlier. "They" had led me to believe that we'd be working with text files. When I saw a sample of their stuff this morning, all the files were Word 6.0/95 documents, not text files. Doesn't this mean I need to modify the code somewhat?

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

    Re: Macro to format multiple documents (2K and XP)

    If they are Word docs, they will probably have extension .doc, so use "*.doc" instead of "*.txt" and wdOpenFormatDocument instead of wdOpenFormatText in the "skeleton" macro I poster higher up in this thread.

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

    Re: Macro to format multiple documents (2K and XP)

    You can run code directly from the VBA window by positioning the insertion point inside the Sub and pressing F5.

    If you want the code to run from within Word, you can put a command button on a document (use the Control Toolbox) or assign the macro to a toolbar button or shortcut key (select Tools | Customize...)

  13. #13
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to format multiple documents (2K and XP)

    Hi Lucas

    Without seeing a sample, it's a hard one to tackle. But I have done some (re)formatting of plain text documents in Word and might offer some techniques I've used. I notice you want to reset margins. This is to no avail if "linebreak" characters are used within "paragraphs" of the text document to limit line length. Also, double linebreaks are often used to separate paragraphs.

    To get the paragraphs to something like the Word definition of a flowing paragraph, I've used the following sequence of search & replaces on sections or whole documents:

    Search for ^p^p, replace with ~~ (or some other obscure character combination)
    Search for ^p, replace with <space character>
    Search for ~~, replace with ^p
    Search for <space character><space character>, replace with <space character> (repeat until "not found" condition)

    This method has to be used judiciously though, and is not really conducive to full "automation". Things like tables in the text files will have to be dealt with separately too. But this method will allow reformatted text to flow properly to the Word margins.

    BTW, ^p is code for a paragraph marker in the search & replace dialogs.

    Alan

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

    Re: Macro to format multiple documents (2K and XP)

    Just for completeness, that old VAX system that the documents came from has a very sophisticated text processing language called TPU that could almost certainly do most ot fhe preparation of the files before you transfer them over.

    StuartR

  15. #15
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Macro to format multiple documents (2K and XP)

    That's interesting! It wouldn't surprise me if the folks who are running this project don't know a thing about TPU. I've never seen VAX or TPU, and I'm not (yet) really involved in this project. I'll mention TPU to those guys and see if they can make it do things before they yank everything out of VAX. Thanks!

Page 1 of 2 12 LastLast

Posting Permissions

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