Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 08-09-2016, 02:42 PM
inosent inosent is offline Windows 8 Office 2010 64bit
Novice
 
Join Date: Jun 2015
Posts: 5
inosent is on a distinguished road
Default Word Macro Shuts Down exactly half way through Document

My first challenge was to automate underlining headers in a word doc. This code worked:


Code:
    Sub Underline_Header()

    Dim numOfParagraphs As Integer
    numOfParagraphs = ActiveDocument.BuiltInDocumentProperties("NUMBER OF PARAGRAPHS")
    Selection.HomeKey Unit:=wdStory
    For x1 = 1 To numOfParagraphs

    Selection.Paragraphs(1).Range.Select

    char_count = Len(Selection.Paragraphs(1).Range)

    If char_count < 50 Then
    Selection.Font.Underline = True
    End If

    Selection.MoveDown Unit:=wdParagraph, Count:=1

    Next x1


    End Sub
But it turns out if the doc is 20 pages, the macro stops at page 10. If 10, stops at 5. 4, then stops at page 2.



I have tried altering the code at Unit:=wdStory to Unit:=wdDocument but that was not the solution. I also tried adding Selection.EndKey Unit:=wdStory to the code but I get the same result.
Reply With Quote
  #2  
Old 08-09-2016, 09:59 PM
Guessed's Avatar
Guessed Guessed is online now Windows 10 Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 954
Guessed is on a distinguished road
Default

You should be using styles rather than local formatting. Try this code
Code:
Sub Underline_Headings()
  Dim aPar As Paragraph
  For Each aPar In ActiveDocument.Paragraphs
    If Len(aPar.Range.Text) < 50 Then
      aPar.Range.Style = "Heading 1"
    End If
  Next aPar
  ActiveDocument.Styles("Heading 1").Font.Underline = wdUnderlineSingle
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-10-2016, 07:40 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 795
gmaxey will become famous soon enough
Default

While I agree with Andrew, I would like to ask you a couple of questions about your code and offer some tips for looping through paragraphs when you need to.

Why did you declare numOfParagrpahs but not x1 or char_count?
Why did you use char_count in the first place? Isn't

If Len(Selection.Paragraphs(1).Range) < 50

the same as:

char_count = Len(Selection.Paragraphs(1).Range)
If char_count < 50 Then

In most case you don't really have to select things to do things with those things you selected.

In addition to Andrews For ... Each method, here are two alternate ways to loop through paragraphs.

Code:
Sub LoopThroughParagraphsI()
Dim lngIndex As Long
  For lngIndex = 1 To ActiveDocument.Range.Paragraphs.Count
    If Len(ActiveDocument.Range.Paragraphs(lngIndex).Range) < 50 Then
      ActiveDocument.Range.Paragraphs(lngIndex).Range.Font.Underline = wdUnderlineSingle
    End If
  Next
lbl_Exit:
  Exit Sub
End Sub
Sub LoopThroughParagraphsII()
'Typically faster.
Dim oPar As Paragraph
  Set oPar = ActiveDocument.Range.Paragraphs(1)
  Do
    If Len(oPar.Range.Text) < 50 Then oPar.Range.Font.Underline = wdUnderlineNone
    Set oPar = oPar.Next
  Loop Until oPar Is Nothing
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word shuts down when trying to open certain docs lesliejames Word 0 01-19-2015 11:36 AM
How do I see one document map for each half of a split MS WORD 2010 document? quickwin Word 3 07-09-2013 10:20 PM
Scrolling wheel in Excel shuts down my computer... lazlow Excel 1 11-01-2011 10:59 AM
Word cutting off half the text stacereally Word 1 03-01-2011 09:46 AM
Outlook 2007 Shuts Down in Windows 7 TOActor Outlook 0 03-06-2010 02:39 AM


All times are GMT -7. The time now is 04:44 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft