Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2015, 08:46 AM
caboy caboy is offline Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Windows 7 32bit Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Office 2010 32bit
Novice
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank
 
Join Date: Mar 2015
Posts: 2
caboy is on a distinguished road
Default Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank

I'm trying to insert text at the beginning of every paragraph that has a specific style format unless that paragraph is blank or begins with a space. If the paragraph is blank or contains a space I would like to macro to do nothing and move onto the next paragraph. here is what I have so far:



Code:
Sub PortionMarking()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Style = wdStyleBodyText
        Do While .Execute(Forward:=True, Format:=True) = True
                With .Parent
                    If .End = ActiveDocument.Content.End Then
                        .StartOf unit:=wdParagraph, Extend:=wdMove
                        .InsertAfter "(U) "
                        Exit Do
                    Else
                        .StartOf unit:=wdParagraph, Extend:=wdMove
                        .InsertAfter "(U) "
                        .Move unit:=wdParagraph, Count:=1
                    End If
                End With
        Loop
    End With
End Sub

Now I believe it needs another nested if statement within the do loop to check for a space or blank before deciding to insert text but I am not sure the best way to go about doing that. any help would be greatly appreciated
Reply With Quote
  #2  
Old 03-31-2015, 07:55 PM
Guessed's Avatar
Guessed Guessed is offline Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Windows 7 32bit Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this
Code:
Sub PortionMarking2()
  With ActiveDocument.Content.Find
    .ClearFormatting
    .Style = wdStyleBodyText
    Do While .Execute(Forward:=True, Format:=True) = True
      With .Parent
        If Left(.text, 1) = vbCr Or Left(.text, 1) = " " Then
          'do nothing
        Else
          .InsertBefore "(U) "
        End If
        
        If .End = ActiveDocument.Content.End Then
          Exit Do
        Else
          .Move unit:=wdParagraph, count:=1
        End If
      End With
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-01-2015, 07:00 AM
caboy caboy is offline Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Windows 7 32bit Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Office 2010 32bit
Novice
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank
 
Join Date: Mar 2015
Posts: 2
caboy is on a distinguished road
Default

Thank you. this appears to work almost perfectly. the only place it still inserts texts is a section break. If I add another "or left" statement before the "then" statement, what is the correct syntax to identify the section break?

Update: I added this or statement which seemed to address my issue

"Or Left(.Text, 1) = Chr(12)"

Last edited by caboy; 04-01-2015 at 09:56 AM. Reason: Update
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Why when I paste text into word, blank space appears after a paragraph & I can't delete it? AWD Word 3 10-16-2018 01:10 PM
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank Macro to insert multiple pictures to word to a specific size and text wrap mescaL Word VBA 3 11-03-2014 10:51 PM
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank need macro insert blank row(s) under data gogita_79 Excel Programming 12 09-16-2014 07:15 AM
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank need macro insert row under specific data gogita_79 Excel Programming 2 09-15-2014 01:48 AM
Looping macros to add text to beginning and end of a paragraph pachmarhi Word VBA 0 02-16-2009 06:57 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:29 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft