#31
|
|||
|
|||
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 |
#32
|
||||
|
||||
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] |
#33
|
|||
|
|||
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. |
#34
|
||||
|
||||
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] |
#35
|
|||
|
|||
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. |
#36
|
||||
|
||||
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] |
#37
|
|||
|
|||
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? |
#38
|
||||
|
||||
Quote:
Code:
.Text = "[^13]{2,}" .Replacement.Text = "^p" .MatchWildcards = True .Execute Replace:=wdReplaceAll 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 StrStyList = StrStyList & oSty.NameLocal & "|" to: If oSty.Type = wdStyleTypeParagraph Then StrStyList = StrStyList & oSty.NameLocal & "|" 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] |
#39
|
|||
|
|||
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. |
#40
|
||||
|
||||
Hi Jana,
Quote:
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#41
|
|||
|
|||
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 & "|" |
#42
|
||||
|
||||
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] |
#43
|
|||
|
|||
Threat solved, thanks again!!!!
Quote:
Thanks Paul again for your help! |
#44
|
|||
|
|||
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. |
#45
|
||||
|
||||
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] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Gray square brackets | waldux | Word | 8 | 09-25-2013 04:14 PM |
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 |