#1
|
|||
|
|||
word macro To insert text at the beginning and at end of paragraph
Hello,
I need to add a sighn like "/>" at the beginning of each paragraph which normally is in style Normal, and after the last character at the end of the paragraph "/>" I tried this VB, but it cause some problem on the headings , so I need to use different methods and to put the BeforeText and AfterText within the paragraph and not at the headings. Do someone have any suggestion or example? Dim doc As Document Dim para As Paragraph Const BeforeText = "</" Const AfterText = "/>" Application.ScreenUpdating = False Set doc = ActiveDocument For Each para In doc.Paragraphs If para.Style = doc.Styles(wdStyleHeading1) Then para.Range.InsertBefore (BeforeText) para.Range.InsertAfter (AfterText) End If If para.Style = doc.Styles(wdStyleHeading2) Then para.Range.InsertBefore (BeforeText) para.Range.InsertAfter (AfterText) End If If para.Style = doc.Styles(wdStyleHeading3) Then para.Range.InsertBefore (BeforeText) para.Range.InsertAfter (AfterText) End If If para.Style = doc.Styles(wdStyleHeading4) Then para.Range.InsertBefore (BeforeText) para.Range.InsertAfter (AfterText) End If If para.Style = doc.Styles(wdStyleHeading5) Then para.Range.InsertBefore (BeforeText) para.Range.InsertAfter (AfterText) End If Next para End Sub |
#2
|
||||
|
||||
So why not use a wildcard Find/Replace, where:
Find = [!^13]{1,} Replace = />^&/> and you set the Find Style to Normal? No code required.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Find/Replace result
Hi, I tried to run the find/replace on style Normal as suggested and I'm getting 0 results
|
#4
|
||||
|
||||
You haven't checked the 'use wildcards' Find/Replace option!!
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hi, after check the 'use wild Card' it put the </ at the beginning of each sentence and on each bullet and not at the beginning of each paragraph
see example |
#6
|
||||
|
||||
The wildcard Find/Replace does exactly what you asked for:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
OK so maybe I didn't express myself correctly
I need that the result will be like that: (the </ will start after the HEADING1 or HEADING2 and so will end with /> before the next HEADING1 or HEADING2 and so) 1. HEADING1 </ aaaaaaaaaa bbbbbbb 1.1 ccccccccc 1.2 dddddddddd 1.2.1 eeeeeeeee /> 2. HEADING1 </ aaaaaaaaaaa /> 2.1 HEADING2 </ ddddddddd ddddddddddddd /> 3. HEADING1 |
#8
|
||||
|
||||
Yes, that's quite different. Even your prefix expression is different! Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Par As Paragraph, Rng As Range For Each Par In ActiveDocument.Paragraphs If Par.Style = "Normal" Then If Rng Is Nothing Then Set Rng = Par.Range Else Rng.End = Par.Range.End End If Else Call RngFmt(Rng) End If If Par.Range.End = ActiveDocument.Range.End Then Call RngFmt(Rng) End If Next Application.ScreenUpdating = True End Sub Sub RngFmt(Rng As Range) If Not Rng Is Nothing Then With Rng .End = .End - 1 .InsertBefore "</" .InsertAfter vbCr & "/>" End With Set Rng = Nothing End If End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Begin End of paragraph
Hi ,
The procedure you send me is much better, and it did most of the work, just one problem if you can help? In case of Table It put the end sign "/>" on the last column, and I need it after the table and not within the table, see example |
#10
|
||||
|
||||
And what about if the Style changes within or immediately before the table?
PS: Is there a reason you can't attach a document with the actual content you want to discuss? Documents containing screenshots are pointless - after all you could attach the screenshot directly to a post - and means the people you want to help you have to recreate the content for themselves in another document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Hello Paul,
Thanks for your help, Attaching the document won't help, since I need to handle ~200 documents like that and I try to create a general macro that can run on all the documents. For now I can see that this macro you put do most of the work beside in case I have a table, which cause the problem I put in the above. |
#12
|
||||
|
||||
I'm sure you could attach a document with some representative content...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Par As Paragraph, Rng As Range For Each Par In ActiveDocument.Paragraphs If Par.Style = "Normal" Then If Rng Is Nothing Then Set Rng = Par.Range Else Rng.End = Par.Range.End End If Else Call RngFmt(Rng) End If If Par.Range.End = ActiveDocument.Range.End Then Call RngFmt(Rng) End If Next Application.ScreenUpdating = True End Sub Sub RngFmt(Rng As Range) If Not Rng Is Nothing Then With Rng If .Characters.Last.Information(wdWithInTable) = True Then .Characters.Last.Next.InsertBefore "/>" & vbCr .Characters.Last.Next.Style = "Normal" Else .End = .End - 1 .InsertAfter vbCr & "/>" End If If .Characters.First.Information(wdWithInTable) = True Then .Characters.First.Previous.InsertBefore vbCr & "</" .Characters.First.Previous.Style = "Normal" Else .InsertBefore "</" End If End With Set Rng = Nothing End If End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Hi Paul,
It works Thanks you for your help, I appreciate your patience and dedicated on that Arie |
#15
|
|||
|
|||
Word 2007 Inserting <T before and R>at the end of Table
I have Word Documents with many tables inside.
I need to mark with delimited key like <T before and T> at the end of the table (not inside the table's cells), so other tool that convert the document will know that this item is a table artifact. Any idea for Macro or any other option? |
Tags |
macro, macro help, table control, word 2007 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Can't insert space at the beginning of a line in Word | p89.schneider | Word | 6 | 03-24-2016 11:38 PM |
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank | caboy | Word VBA | 2 | 04-01-2015 07:00 AM |
VBA code for Microsoft Word macro — select text and insert footnote | ndnd | Word VBA | 10 | 01-06-2015 01:47 PM |
Macro to insert multiple pictures to word to a specific size and text wrap | mescaL | Word VBA | 3 | 11-03-2014 10:51 PM |
Looping macros to add text to beginning and end of a paragraph | pachmarhi | Word VBA | 0 | 02-16-2009 06:57 AM |