Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-15-2021, 03:25 AM
rgros rgros is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2019
Novice
setting paragraph style removes inlines incidentally. Why?
 
Join Date: Jan 2021
Posts: 8
rgros is on a distinguished road
Default 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
The result is:

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
Reply With Quote
  #2  
Old 01-15-2021, 08:55 AM
gmaxey gmaxey is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-19-2021, 03:06 AM
rgros rgros is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2019
Novice
setting paragraph style removes inlines incidentally. Why?
 
Join Date: Jan 2021
Posts: 8
rgros is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 01-19-2021, 10:24 AM
gmaxey gmaxey is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 01-20-2021, 02:26 AM
rgros rgros is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2019
Novice
setting paragraph style removes inlines incidentally. Why?
 
Join Date: Jan 2021
Posts: 8
rgros is on a distinguished road
Default

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
Attached Files
File Type: docm test4.docm (19.2 KB, 9 views)
Reply With Quote
  #6  
Old 01-20-2021, 03:16 AM
macropod's Avatar
macropod macropod is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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:
Public Sub Flatten()
With ActiveDocument.Range
  .Style = wdStyleNormal
  .Font.Reset
End With
End Sub
or:
Code:
Public Sub Flatten()
With ActiveDocument.Range
  .ParagraphFormat.Reset
  .Font.Reset
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 01-20-2021, 09:07 AM
gmaxey gmaxey is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 01-20-2021, 01:07 PM
macropod's Avatar
macropod macropod is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 gmaxey View Post
Paul, here both of those returned the same result the OP is seeing.
In my testing, both restored the Normal formatting for the entire document.

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
or:
Code:
Public Sub Flatten()
ActiveDocument.Range.ParagraphFormat.Reset
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 01-20-2021, 01:23 PM
gmaxey gmaxey is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,


You are typically the man . This one certainly worked.



Code:
Public Sub FlattenII()
    ActiveDocument.Range.Style = wdStyleNormal
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 01-21-2021, 05:01 AM
rgros rgros is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2019
Novice
setting paragraph style removes inlines incidentally. Why?
 
Join Date: Jan 2021
Posts: 8
rgros is on a distinguished road
Default

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
I am still curious what is happening when I use the old code. Googling around I found more mentions of this problem, some from ten years back. Is my conclusion right that setting the paragraph style is in the danger zone when it comes to macro's?

Thanks again, Ruud
Reply With Quote
  #11  
Old 01-21-2021, 05:16 AM
Guessed's Avatar
Guessed Guessed is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #12  
Old 01-21-2021, 05:30 AM
rgros rgros is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2019
Novice
setting paragraph style removes inlines incidentally. Why?
 
Join Date: Jan 2021
Posts: 8
rgros is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 01-21-2021, 12:53 PM
macropod's Avatar
macropod macropod is offline setting paragraph style removes inlines incidentally. Why? Windows 10 setting paragraph style removes inlines incidentally. Why? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 code you posted merely involves a lot of circumlocution to achieve the same result as:
Code:
ActiveDocument.ConvertNumbersToText (wdNumberAllNumbers)
Moreover, your use of:
Code:
wPara.range.Select
merely slows the code down (especially given that you never actually do anything with the resulting Selection) and, combined with the lack of:
Code:
Application.ScreenUpdating = False
results in a lot of screen flicker.

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]
Reply With Quote
Reply

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
setting paragraph style removes inlines incidentally. Why? 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

Other Forums: Access Forums

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