Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #31  
Old 01-05-2012, 02:35 PM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default Hi makropod,

Sorry, i was thinking if i cover what i want to said, and after i post i remember that i forgot something.



The headings of ms word styles are having other names such as MM Topic 1 for heading 1, MM Topic 2 for Heading 2 ..... MM Topic 9 for heading 9 the previous makro was fantastic to do this job if understand that Heading 1 is MM Topic 1 or MM Topic 2 until 9 is heading 2 until Heading 9.

I try the new makro (I put main style Basic and sub style MM Topic 3 and take error 91 and yellow the text While .Paragraphs.Last.Next.Style <> strStyA.) After that i try Main style MM Topic 3 and sub style Basic and take something that i dont want such copy of text with Bold letters, maybe confused the makro because the Basic style is common for all styles MM Topic 1, MM Topic 2 and.....

Last edited by macropod; 01-05-2012 at 03:06 PM. Reason: Deleted unneccessary quote of entire previous post
Reply With Quote
  #32  
Old 01-05-2012, 03:13 PM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jana,

If someone has used a Style, then changed the paragraph's formatting to something else (even just a little bit), that makes it very difficult for any macro to know that whether a changed paragraph isn't to be treated the same way as one that isn't changed.

I am not sure why the error 91 occurred - was it with one of the documents attached to a previous post?

PS: Please don't keep quoting previous posts unless there is something in them that needs to be quoted - and then only quote that part. Simply quoting an entire previous post only makes the thread longer.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #33  
Old 01-05-2012, 03:36 PM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default Dear Macropod,

I m new on this site, and after that i discover the simple reply instead of quote reply, sorry.
As that you said before, yes i work on the previous document i attached.
I don't make any paragraphs change before run the macro.
Before you ask me if is problem to macro to understand the Headings levels
Heading 1
some text
Heading 2
some text
Heading 3
some text
Heading 1
some text
Heading 3
some text
I think that this one is problem for me, also like the problem you solved before with empty inside {} when i dont have headings appropriate to go inside.
Reply With Quote
  #34  
Old 01-05-2012, 06:18 PM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jana,

I ran the code against the last document you posted, and it did not produce any errors. It also did not produce any output, because that document isn't formatted the same as the others. Your earlier documents had empty paragraphs for spacing between the headings and their text. That is poor design, but that is what I coded for. Your last document had good design with no empty paragraphs for spacing and I did not code for that.

It would be difficult to write a macro that works with all the different possibilities for poor design. However, it is easy to have the macro enforce good design by deleting all the empty paragraphs, and then process the revised document.

The attached macro enforces good design and macro also invites you to nominate a 'stop' Style to deal with the issues discussed earlier. The default 'stop' Style is the same as the 'main' Style, but it can be any other Style (even the sub Style - but then the macro won't do anything).
Code:
Sub InsertRefs()
Application.ScreenUpdating = False
Dim RngHdA As Range, RngHdB As Range, RngRef As Range, oPara As Paragraph
Dim oSty As Style, StrStyList As String, strStyA, strStyB, strStyC, StrTxt As String
Dim Msg As String, MsgA As String, MsgB As String, MsgC As String, MsgErr As String
With ActiveDocument
  StrStyList = "|"
  MsgA = "What is the 'Main' Style to Find"
  MsgB = "What is the 'Sub' Style to Find"
  MsgC = "What is the 'Stop' Style to Find"
  MsgErr = "This document has no Style named:" & vbCr
  For Each oSty In .Styles
    StrStyList = StrStyList & oSty.NameLocal & "|"
  Next
  Msg = MsgA
  While strStyA = ""
    strStyA = InputBox(Msg, "Style Selector")
    If strStyA = "" Then Exit Sub
    If InStr(StrStyList, "|" & strStyA & "|") = 0 Then
      Msg = MsgErr & strStyA & vbCr & vbCr & MsgA
      strStyA = ""
    End If
  Wend
  Msg = MsgB
  While strStyB = ""
    strStyB = InputBox(Msg, "Style Selector")
    If strStyB = "" Then Exit Sub
    If InStr(StrStyList, "|" & strStyB & "|") = 0 Then
      Msg = MsgErr & strStyB & vbCr & vbCr & MsgB
      strStyB = ""
    End If
  Wend
  Msg = MsgC
  While strStyC = ""
    strStyC = InputBox(Msg, "Style Selector", strStyA)
    If Trim(strStyC) = "" Then Exit Sub
    If InStr(StrStyList, "|" & strStyC & "|") = 0 Then
      Msg = MsgErr & strStyC & vbCr & vbCr & MsgC
      strStyC = ""
    End If
  Wend
End With
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[^13]{2,}"
    .Replacement.Text = "^p"
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    .Text = ""
    .Style = strStyA
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    Set RngHdA = .Paragraphs(1).Range.Duplicate
    With RngHdA
      On Error GoTo ParaLast
      While .Paragraphs.Last.Next.Style <> strStyA And .Paragraphs.Last.Next.Style <> strStyC
        .MoveEnd wdParagraph, 1
      Wend
ParaLast:
      If .Paragraphs.Count > 2 Then
        Set RngRef = RngHdA.Paragraphs(2).Range.Characters.Last
        .MoveStart wdParagraph, 2
        Set RngHdB = RngHdA
        StrTxt = ""
        With RngRef
          .MoveEnd wdCharacter, -2
          For Each oPara In RngHdB.Paragraphs
            If oPara.Style = strStyB Then
              If Len(Trim(oPara.Range.Text)) > 1 Then
                StrTxt = StrTxt & Left(oPara.Range.Text, Len(oPara.Range.Text) - 1) & ", "
              End If
            End If
          Next
          If Len(StrTxt) > 0 Then
            StrTxt = "{" & Left(StrTxt, Len(StrTxt) - 2) & "}"
            .InsertAfter StrTxt
          End If
        End With
      End If
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #35  
Old 01-06-2012, 02:58 AM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default Please give an example

I want this document the Heading 9 to be copied to end of paragraph of heading 8. What i must to put as main, sub, and stop style to work?
Sorry for this but lets say this is the formating of my document. Could this macro work with this?
I thought that macro uses the styles to do this job, so it will worked with all poor or not documents, the documents all they have the design of last attached document, so i need something to working only for this design.
Reply With Quote
  #36  
Old 01-06-2012, 01:04 PM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jana,

For that, the 'Main' Style is 'Heading 8', the 'Sub' Style is 'Heading 9' and the 'Stop' Style is any other Style you don't want the macro to look past (eg 'Heading 7') when adding Sub Style text to the Main Style paragraphs.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #37  
Old 01-06-2012, 01:26 PM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default Hi mpod,

I tried this and take error 5560 and yellow the .Execute Replace:=wdReplaceAll.
I' ve set to Main MM Topic 8 instead of heading 8, and sub style MM Topic 9 instead of heading 9, because macro dont recognize these headings.
I tried the last macro. This could happens for the names of styles or for another reason?
Reply With Quote
  #38  
Old 01-06-2012, 07:25 PM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by Jana View Post
I tried this and take error 5560 and yellow the .Execute Replace:=wdReplaceAll.
That is probably because of your system's regional settings. If you are sure your documents will always be well-formatted, you could delete these four lines:
Code:
    .Text = "[^13]{2,}"
    .Replacement.Text = "^p"
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
otherwise, I'd suggest either:
a)change '.Text = "[^13]{2,}"' to '.Text = "[^13]{2;}"'; or
b) replace the above four lines with:
Code:
    On Error Resume Next
    .Text = "[^13]{2,}"
    .Replacement.Text = "^p"
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    .Text = "[^13]{2;}"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
It might also be a good idea to change:
StrStyList = StrStyList & oSty.NameLocal & "|"
to:
If oSty.Type = wdStyleTypeParagraph Then StrStyList = StrStyList & oSty.NameLocal & "|"
Quote:
Originally Posted by Jana View Post
I' ve set to Main MM Topic 8 instead of heading 8, and sub style MM Topic 9 instead of heading 9, because macro dont recognize these headings.
I tried the last macro. This could happens for the names of styles or for another reason?
The macro does work with heading Styles, but only if they exist in your document - you can't find something that isn't there. That's why you had to use 'MM Topic' instead.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #39  
Old 01-06-2012, 09:54 PM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default Working Fine!!!!

I just tried these changes and all working fine!!! Thanks again. Please, let me ask you some things because I'm confused.
You said:
It might also be a good idea to change:
StrStyList = StrStyList & oSty.NameLocal & "|"
to:
If oSty.Type = wdStyleTypeParagraph Then StrStyList = StrStyList & oSty.NameLocal & "|"

And i changed this lines to third way of code you post, is that right or i must changed this lines also to the first way of code or/and the second?

The second thing i wanted to ask, is that when i have a non well formated document, you recommend to try the second change of code or the third?

The third thing i wanted to ask, is the stop style is always must be set one level before the main style, otherwise it will not work?
Anyway, i think you are fantastic. I'm very happy.
Reply With Quote
  #40  
Old 01-06-2012, 10:47 PM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jana,
Quote:
And i changed this lines to third way of code you post, is that right or i must changed this lines also to the first way of code or/and the second?
the statement 'StrStyList = StrStyList & oSty.NameLocal & "|"' only appears at line 13, and that was the only place that change should be made.
Quote:
when i have a non well formated document, you recommend to try the second change of code or the third
With the block of code I posted starting with 'On Error Resume Next', a poorly-formatted document will become well-formatted (the empty paragraphs will be deleted).
Quote:
the stop style is always must be set one level before the main style
The Stop Style can be any paragraph Style, though you wouldn't want to make it the same as the Sub Style. Simply make it whatever Style you don't want it to look past for a Sub Style. By default, it is the same as the Main Style, because it won't look past that anyway.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #41  
Old 01-07-2012, 12:32 AM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default

This line must be changed whatever change of the code do (if i keep the code for well formated documents or use the other for non well formated documents)
If oSty.Type = wdStyleTypeParagraph Then StrStyList = StrStyList & oSty.NameLocal & "|"
Reply With Quote
  #42  
Old 01-07-2012, 12:36 AM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jana,

Changing that line is recommended, but not essential. What is does is to limit the user's choice of Styles to Paragraph Styles. Without it, a user could choose a Character Style, and the results might not be what you would expect.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #43  
Old 01-07-2012, 12:48 AM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default Threat solved, thanks again!!!!

Quote:
Originally Posted by macropod View Post
Hi Jana,

Changing that line is recommended, but not essential. What is does is to limit the user's choice of Styles to Paragraph Styles. Without it, a user could choose a Character Style, and the results might not be what you would expect.
So this means to me, that is good to change whatever code use, the code for well formated documents and this one for non well formated documents.

Thanks Paul again for your help!
Reply With Quote
  #44  
Old 02-04-2012, 03:32 AM
Jana Jana is offline Please help copy inside brackets Windows 7 32bit Please help copy inside brackets Office 2007
Novice
Please help copy inside brackets
 
Join Date: Dec 2011
Posts: 28
Jana is on a distinguished road
Default a liitle help!!

Plz, the code is awesome but i need something else if is this possible. The headings that i need to included inside brackets finished some of them with full stop. So i dont need this full stop. example: {Heading 1., Heading 2., Heading 3..}.
But what i really want is {Heading 1, Heading 2, Heading 3.}.
So i think that i need macro to check before execution if the headings ends with full stops and deletes them, before putting into brackets, divided by comma or else and ending with full stop. Is this difficult to make it work and possible? Thanks.
Reply With Quote
  #45  
Old 02-04-2012, 08:08 PM
macropod's Avatar
macropod macropod is offline Please help copy inside brackets Windows 7 64bit Please help copy inside brackets Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jana,

What if the last word in the heading is an abbreviation (eg a heading like 'Subjects to think about, etc.')? It's fairly easy to do if you simply want all the end '.' characters deleted, but not easy if you want to keep some for abbreviations. If you want to keep the periods following abbreviations, you could probably do the clean-up just as easily with an ordinary Find/Replace, where:
Find = .,
Replace = .
and don't use 'Replace All'.

Also, what about other punctuation marks (eg ?,:;!) and spaces?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gray square brackets waldux Word 8 09-25-2013 04:14 PM
Please help copy inside brackets Find/Replace Brackets Problem fatso Word 2 08-04-2011 11:34 AM
brackets citation uncung Word 1 07-13-2011 01:39 PM
Brackets Issue... DarkJudge1 Outlook 0 07-06-2010 05:15 PM
copy a file which does not have copy option jkind Word 0 01-17-2010 09:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:55 PM.


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