#1
|
|||
|
|||
setting paragraph style removes inlines incidentally. Why?
Hi, I am new to this forum. I hope I manage to ask this question properly in one go.
The problem I am facing: I have a whole lot of word macro's running on documents to prepare it for conversion to XML. Last week, I got a report of italic inlines disappearing in the macro output. I traced this back to a routine that removes the paragraph styles from the document. In some cases the setting of the paragraph style has a side effect that italic inlines are removed. I certainly want to preserve them in order to get the expected output. I made a minimal example to show this behaviour. I hope you can explain me why word behaves like this and that you can advise me what I can do in the macro are to prevent this from happening. I have a document that looks like this: abcdef 123456 abcdef 1234567 abcdef 12345 abcdef 1234 I run this macro on it to remove any user defined paragraph styles: Code:
Public Sub flatten() Dim wPara As Word.Paragraph For Each wPara In Word.ActiveDocument.Paragraphs wPara.range.Select wPara.style = ActiveDocument.styles(wdStyleNormal).NameLocal Next End Sub abcdef 123456 abcdef 1234567 abcdef 12345 abcdef 1234 in the first two lines, the italic inline is deleted, whereas the last two lines still have it. I found out the inline is deleted when the number of inline characters exceeds the number of normal characters (this is the case in the first two paragraphs). As said, I want to preserve the inlines in all situations. I hope this is not a FAQ that I missed. Thanks in advance, Ruud |
#2
|
|||
|
|||
I am not seeing that "exact" behavior (... number of inline characters ...). When I first ran the code it worked as expected. What I did see is if the "Italic" direct formatting extends to and includes the paragraph mark then the result is like you show. You might try:
Code:
Public Sub flatten() Dim wPara As Word.Paragraph For Each wPara In Word.ActiveDocument.Paragraphs wPara.Range.Characters.Last.Select On Error Resume Next With Selection .ClearCharacterDirectFormatting .ClearCharacterStyle End With wPara.Style = ActiveDocument.Styles(wdStyleNormal).NameLocal Next End Sub |
#3
|
|||
|
|||
Hi Greg,
I have had a look at my test documents and indeed it seems I have taken the wrong conclusion about the string lenghts and more important, that the real problem is the line endings being italic. I have tested the code you posted and actually that solved my problem completely with the test document. Unfortunately, the solution does not work with the real life documents. Obviously, there is more going on; the disappearing italics are inside list items. In order to provide a test document that shows this, I should post it in this forum. Is that posssible? Ruud |
#4
|
|||
|
|||
I believe you can use the go advanced to attach file, it not you can send website feedback to me and I will reply.
Will be out most of the day though. |
#5
|
|||
|
|||
here is a short document that clearly shows the problem. It has a list with 3 items.
When the macro flatten is run, the italics are removed from the secord item, while the 1st and the 3rd are not affected. But, more interestingly, when you change the string zzzzzzzz into z before running the macro, the italics are preserved. This was the origin of my observation of string lengths playing a role to this behaviour of word. I hope this helps nailing down this issue! thanks, Ruud |
#6
|
||||
|
||||
Try:
Code:
Public Sub Flatten() With ActiveDocument.Range .Style = wdStyleNormal .Font.Reset End With End Sub Code:
Public Sub Flatten() With ActiveDocument.Range .ParagraphFormat.Reset .Font.Reset End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Paul, here both of those returned the same result the OP is seeing.
Ruud, back to your original observation, it does seem that the length of the italic string in the paragraph text is having and affect. I copied your line 2 as lines 4 and 5. Then shortened the italic string in line 4. Ran your macro and lines 2 and 5 were not converted to all normal font. I don't know why that is happening. I don't know how helpful this will be for your production documents, but for this example, this worked: Code:
Public Sub flatten() Dim oRng As Range Dim wPara As Word.Paragraph Set oRng = ActiveDocument.Range With oRng.Find .Font.Italic = True .Replacement.Text = "<i>^&</i>" .MatchWildcards = True .Execute Replace:=wdReplaceAll End With For Each wPara In Word.ActiveDocument.Paragraphs wPara.Style = ActiveDocument.styles(wdStyleNormal).NameLocal Next Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Text = "(\<i\>)(*)(\</i\>)" .MatchWildcards = True .Replacement.Text = "\2" .Replacement.Font.Italic = True .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#8
|
||||
|
||||
Quote:
If I misread the OP's requirement and the italics are to be retained, that's as simple as: Code:
Public Sub Flatten() ActiveDocument.Range.Style = wdStyleNormal End Sub Code:
Public Sub Flatten() ActiveDocument.Range.ParagraphFormat.Reset End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Paul,
You are typically the man . This one certainly worked. Code:
Public Sub FlattenII() ActiveDocument.Range.Style = wdStyleNormal End Sub |
#10
|
|||
|
|||
Paul and Greg,
thank you very much for this. Both Greg's and Paul's solutions worked for me. I used this for flattening lists, they needed to be converted to plain text paragraphs (including the list number). This is probably going to end the issues we had with it concerning the italics. A trimmed down code example is now: Code:
Public Sub flattenLists() Dim strListNr As String Dim wPara As Word.Paragraph Dim wList as Word.List For Each wList In Word.ActiveDocument.Lists With wList For Each wPara In .ListParagraphs wPara.range.Select With wPara strListNr = .range.ListFormat.ListString .range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph wPara.Range.ParagraphFormat.Reset .range.InsertBefore strListNr & Chr(32) End With Next End With Next End Sub Thanks again, Ruud |
#11
|
||||
|
||||
Flattening lists is a lot simpler than your code. You can either do it to the entire doc or to a selection
Code:
'This is for the whole document ActiveDocument.ConvertNumbersToText (wdNumberAllNumbers) 'This is for a selection Selection.Range.ListFormat.ConvertNumbersToText wdNumberParagraph
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#12
|
|||
|
|||
That is good to keep in mind. The actual code contains a lot more logic, for instance to handle bullet lists and doing proper indentation and so on. It is not my own code, so I dear not tamper with it too much.
But it is certainly helpful to know this, in case I have to rewrite it. Ruud |
#13
|
||||
|
||||
The code you posted merely involves a lot of circumlocution to achieve the same result as:
Code:
ActiveDocument.ConvertNumbersToText (wdNumberAllNumbers) Code:
wPara.range.Select Code:
Application.ScreenUpdating = False At the very most, all you need is: Code:
Public Sub Flatten() With ActiveDocument .ConvertNumbersToText (wdNumberAllNumbers) .Range.Style = wdStyleNormal End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA - (Re-) Setting a Table Style | mikejvir | PowerPoint | 2 | 12-04-2019 10:15 AM |
Why does paragraph inherit style of the following paragraph? | WADEVCAMP | Word VBA | 2 | 04-08-2019 02:13 PM |
Setting part of a paragraph bold | Fred256 | Word VBA | 4 | 10-07-2016 02:54 PM |
Style seems to inherit bold setting from previous style | Jennifer Murphy | Word | 3 | 02-16-2012 04:18 PM |
Character style stripped while applying paragraph style | sams_gates | Word | 0 | 08-29-2009 02:03 AM |