Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need VBA code for Word 2010 to modify all indented text

    Hello,

    I have a problem that I am hoping can be solved using VBA, but have never used this before. I'd really appreciate any advice anyone could give me...

    I have been given a 500 page word document in which all text with a left indent needs to be changed from full justification to left-justification. The document is full of 'lists' (where the original user used indent and numbered the lists by hand instead of using bullet/number list) and equations that I need to change the justification of, and the only thing these have in common is that the text is indented.

    Is there a simple way to do this? I am trying to avoid having to go through the document and manually change all the affected text.

    many thanks

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    I would try the AutoFormat command first. It might be able to identify all the lists and apply a style to them. You can add the AutoFormat... button onto your Quick Access Toolbar and run it from there. It is in the 'Commands not in the Ribbon' set of commands.

    Try running this command on a copy of your document in case it has some unwanted side-effects.

    If you don't have any luck with that method, post back and we can have a go at code that does as you requested.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #3
    New Lounger
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Andrew, thanks for the response, I've now managed to fix my problems and have posted the solution here in case anyone else has a similar issue.

    As it turned out my problem was a bit more extensive. The root of all the problems is that the word document was actually created by (exported from) another piece of software, the export process is buggy and inserts these weird formatting problems every time. So I needed to create a macro that can fix all the problems in one go, so that each time I get a document exported in that way I can use it as a 'magic bullet' to fix all the oddities. The problems I needed to fix were: all left-indented text needed to be left-justified, all first line indents were to be removed, all tab characters were to be removed (these appeared at random points all through the text!). In the end I came up with this which did the trick:

    Sub FixMyExportedDocument

    n = Selection.Paragraphs.Count
    For i = 1 To n
    If Selection.Paragraphs(i).LeftIndent > 0 Then
    Selection.Paragraphs(i).Alignment = wdAlignParagraphLeft
    End If

    If Selection.Paragraphs(i).FirstLineIndent > 0 Then
    Selection.Paragraphs(i).Format.FirstLineIndent = 0
    End If
    Next
    Selection.Find.Execute FindText :="^t", ReplaceWith :=" ", Replace :=wdReplaceAll
    End Sub


    This works well, although I had to use the Selection. format instead of ActiveDocument. as my document is >10,000 paragraphs long and running the macro on the whole thing crashes Word. Instead I set section breaks throughout the document, highlight a section at a time, and run the macro.

  5. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    Possibly your crash is related to using n which could be restricted if it is defined as an integer. Try the following code as an alternative way of doing the same thing
    Code:
    Sub FixMyExportedDocument2()
      Dim aPar As Paragraph
      For Each aPar In ActiveDocument.Paragraphs
        aPar.Format.FirstLineIndent = 0
        If aPar.LeftIndent > 0 Then aPar.Alignment = wdAlignParagraphLeft
      Next aPar
      ActiveDocument.Content.Find.Execute FindText:="^t", ReplaceWith:=" ", Replace:=wdReplaceAll
    End Sub
    Note that the find/replace will fail if you haven't cleared other relevant settings such as find format. These other settings get 'remembered' from when you previously used the find and replace dialog.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #5
    New Lounger
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Andrew Lockton View Post
    Try the following code as an alternative way of doing the same thing
    Wow thanks! That works brilliantly, and takes seconds (whereas my original method took a few minutes to process the whole document). I really appreciate this

    All the best

  7. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,889
    Thanks
    0
    Thanked 188 Times in 172 Posts
    The 'preferred' way to handle re-formatting of this kind is to modify the underlying paragraph Style. This, of course, assumes the content of interest already has an appropriate Style applied. If so, the change requires nothing more than editing the Style definition. If not (as appears to be the case here), create (if necessary) a suitable Style or modify the existing one, then apply the Style to the relevant content.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Tags for this Thread

Posting Permissions

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