Word VBA - document formatting

esuhl
esuhl Posts: 9,409 Forumite
Part of the Furniture 1,000 Posts Name Dropper
I just wondered if anyone might be able to offer a few tips for getting started with VBA programming in Word. I've used VBA in Excel a fair bit and it's easy to step through cells sequentially until some specified text is found... how would you do something similar in Word?

I have a number of documents in a standard format, and I want to write a script/macro that can re-format them automatically. If anyone can even suggest the objects that I need to manipulate that would be a massive help! I'm guessing it's not as straightforward as: ActiveDocument.Sentence(0).Format.Textcolor = vbBlack

In case it helps explain what I'm trying to do, this is my "pseudocode":
  1. Delete the page numbers in the header
  2. Delete the "====Section Break (Continuous)====" line at the very top of the document, and delete all text up to and including the first image.
  3. Find the first paragraph of 16 point text. Cut this text and paste it at the top of the document.
  4. Remove any paragraph that begins with the words BYLINE, SECTION, LENGTH, LOAD-DATE, LANGUAGE or PUBLICATION-TYPE
  5. Make all text black and left-justified
  6. Remove all paragraph indentation
I've also uploaded an example document here: http://ftp2.dns-systems.net/~pat002/unrelated/Formatting_demo.rtf.

As I said, just a clue as to the objects I need to manipulate would really help - I'm certainly not expecting anyone to write the whole thing for me or anything that cheeky!

Thanks so much for any suggestions :-)

Comments

  • esuhl
    esuhl Posts: 9,409 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Well, just in case anyone's trying something similar, you can step through paragraphs like this:
    Sub pStepThroughParagraph()
    Dim paraObject as Paragraph
    Dim sParagraph as String
    
    For Each paraObject in ActiveDocument.Paragraphs
         sParagraph = paraObject.Range.Text
         '*** Do some stuff here with paraObject or sParagraph ***
    Next paraObject
    
    End Sub
    

    Now... does anyone know how to step through images within a document? Essentially I'm trying to delete all images in a document...
  • angelavdavis
    angelavdavis Posts: 4,714 Forumite
    Mortgage-free Glee!
    You can use find and replace (ie, find ====Section Break (Continuous)====^p (I assume you have a paragraph mark at the end of it - ^p will remove this too) and Replace with nothing to remove items.

    To move to the paragraphs starting with a particular word, I would also use Find command to find the paragraphs starting with the different words, cancel the find box, then press F8 3 times in order to select that paragraph.

    You can always record macros whilst doing these actions and then copy and paste the code into another VBA sheet with your code for moving to a specific paragraph.

    To change all text to black and a certain font, create a style with the right formatting, save into the Normal template and then use the Control+A option to select all, then choose the style you want to apply.

    Personally, this is what I would do to save time, then just piece it all together.

    Edit: Just had a look at your sample document, so you need to actually remove the section break itself? If so, replace my instruction in paragraph one and use ^b in Find and replace command to remove Section Breaks.
    :D Thanks to MSE, I am mortgage free!:D
  • esuhl
    esuhl Posts: 9,409 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Thanks, angelavdavis. I think I'm nearly finished now! I've discovered that the Section Break is represented in ASCII as decimal 12 (i.e. chr(12) in VBA), so I've been able to search for & remove that.

    I've also got rid of page numbers (by recording a macro, doing it manually and checking the code) and have also deleted the first image on the document like this:
    Sub pDeleteFirstImage()
        Selection.WholeStory
        Selection.GoTo what:=wdGoToGraphic, Count:=1
        Selection.Delete
    End Sub
    
    The only thing I have left to do is point 3 in my original post:
    3. Find the first paragraph of 16 point text. Cut this text and paste it at the top of the document.
    I've written some code to locate the text, make a copy, and delete the original, so I just have to work out how to insert a paragraph at the top of the document. Hopefully it won't take me too long to figure that out!

    Thanks for your help :-)
  • esuhl
    esuhl Posts: 9,409 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Argh! I've just realised that if there are no images in the document, the above subroutine (pDeleteFirstImage) will put the cursor at the top of the page, so Selection.Delete removes the first character in the document!

    Is there any way to count the number of images in a document so the subroutine only runs if "number_of_images > 1"... Or is there a way to detect whether the selection contains an image, rather than text?
  • angelavdavis
    angelavdavis Posts: 4,714 Forumite
    Mortgage-free Glee!
    The F5 (Go To Next) option has a graphic option, is that worth an experiment?
    :D Thanks to MSE, I am mortgage free!:D
  • esuhl
    esuhl Posts: 9,409 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Aha! I've discovered that you delete the first image on the page with:
    ActiveDocument.InlineShapes(1).Delete
    

    Phew! That's a relief - my first little Word macro is finished! Thanks for the suggestions, angela.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 349.9K Banking & Borrowing
  • 252.7K Reduce Debt & Boost Income
  • 453.1K Spending & Discounts
  • 242.9K Work, Benefits & Business
  • 619.8K Mortgages, Homes & Bills
  • 176.4K Life & Family
  • 255.9K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 15.1K Coronavirus Support Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.