Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-20-2017, 06:27 AM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 08-20-2017, 02:11 PM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 08-20-2017, 10:40 PM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default Find/Replace result

Hi, I tried to run the find/replace on style Normal as suggested and I'm getting 0 results
Attached Files
File Type: docx Doc2.docx (42.4 KB, 12 views)
Reply With Quote
  #4  
Old 08-20-2017, 10:41 PM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You haven't checked the 'use wildcards' Find/Replace option!!
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-20-2017, 11:09 PM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default

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
Attached Files
File Type: docx Doc1.docx (23.8 KB, 13 views)
Reply With Quote
  #6  
Old 08-20-2017, 11:16 PM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The wildcard Find/Replace does exactly what you asked for:
Quote:
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 "/>"
It has no effect one way or the other on 'bullets' - which your document doesn't have - and your 'sentences' are evidently in different paragraphs, all using the Normal Style...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 08-21-2017, 12:05 AM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 08-21-2017, 12:17 AM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #9  
Old 08-21-2017, 01:05 AM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default 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
Attached Files
File Type: docx Doc2.docx (25.4 KB, 16 views)
Reply With Quote
  #10  
Old 08-21-2017, 02:17 AM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 08-21-2017, 02:28 AM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default

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.
Reply With Quote
  #12  
Old 08-21-2017, 02:29 AM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I'm sure you could attach a document with some representative content...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 08-21-2017, 02:49 AM
macropod's Avatar
macropod macropod is offline word macro To insert text at the beginning and at end of paragraph Windows 7 64bit word macro To insert text at the beginning and at end of paragraph Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #14  
Old 08-21-2017, 03:28 AM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default

Hi Paul,

It works
Thanks you for your help, I appreciate your patience and dedicated on that

Arie
Reply With Quote
  #15  
Old 08-30-2017, 04:01 AM
ArieH ArieH is offline word macro To insert text at the beginning and at end of paragraph Windows 10 word macro To insert text at the beginning and at end of paragraph Office 2007
Novice
word macro To insert text at the beginning and at end of paragraph
 
Join Date: Aug 2017
Posts: 10
ArieH is on a distinguished road
Default 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?
Reply With Quote
Reply

Tags
macro, macro help, table control, word 2007



Similar Threads
Thread Thread Starter Forum Replies Last Post
word macro To insert text at the beginning and at end of paragraph Can't insert space at the beginning of a line in Word p89.schneider Word 6 03-24-2016 11:38 PM
word macro To insert text at the beginning and at end of paragraph 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
word macro To insert text at the beginning and at end of paragraph VBA code for Microsoft Word macro — select text and insert footnote ndnd Word VBA 10 01-06-2015 01:47 PM
word macro To insert text at the beginning and at end of paragraph 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:27 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