none
Macro to convert styles in Word - Lines that crashes Word RRS feed

  • Question

  • Hi,

    I have a macro that convert styles from our old styles to the new styles. I got errors inside a macro that add the new styles in the document. In the marority of the files, the command below works but for1/3 of the file I tested, word craches and close all documents. Any help for the 3 questions below will be very appreciated.

    Thank you,
    Alex

    1)  Regarding this sub (oStyle.NextParagraphStyle = oStyle.Parent.Styles(vStyleName) returns an error)

    Private Sub SetNextParagraphStyle(oStyle As Word.Style, vStyleName As Variant)

        If VarType(vStyleName) = vbString Then
            If oStyle.NextParagraphStyle <> vStyleName Then
                oStyle.NextParagraphStyle = oStyle.Parent.Styles(vStyleName) 'This line crash when vStyleName = "Normal 2" (already exists)
            End If
        Else
            If oStyle.NextParagraphStyle <> oStyle.Parent.Styles(vStyleName).NameLocal Then
                oStyle.NextParagraphStyle = vStyleName 'Crash with some files when the value oStyle.NextPrographStyle already equal to vStyleName (the line above solve the issue)
            End If
        End If

    2) Regarding this sub (oStyle.Frame.Delete returns an error)

    Public Sub SetBaseStyle(oStyle As Word.Style, vStyleName As Variant)
        Dim oBaseStyle As Word.Style
        If oStyle.NameLocal <> "Normal" Then
            If oStyle.BaseStyle <> oStyle.Parent.Styles(vStyleName).NameLocal Then         
                  oStyle.BaseStyle = oStyle.Parent.Styles(vStyleName) 'Crash with specific files when the value oStyle.BaseStyle already equal to oStyle.Parent.Styles(vStyleName) (the line above solve the issue)
            End If
        End If
           
        Set oBaseStyle = oStyle.BaseStyle
        oStyle.Font = oBaseStyle.Font '.BaseStyle.Font        
        oStyle.ParagraphFormat = oBaseStyle.ParagraphFormat

        oStyle.Frame.Delete 'This line returns an error in specific files. When we add a new style, sometimes, there is a frame and I would like to delete it. Is there a way to check if if a frame exits or something else that may fix this issue?

    End Sub

    3)  Regarding this sub (oSection.PageSetup.Orientation returns an error)

    Sub AdjustSections(oDocWord as Word.Document)

    For Each oSection In oDocWord.Sections
            With oSection.PageSetup
                If .Orientation = wdOrientPortrait Then 'This line returns an error for the first section for specific files. It doesn't find the value of oSection.PageSetup.Orientation. And then, I run it again and it works! Is there a fix to know the information?
                    '....code

               End If
            End With
    Next oSection

    'This error happens in the first section where there is only one table (one cell with 5 paragraphs in the table) and there is a section break thereafter.


    • Edited by SLIVED Friday, March 16, 2012 3:10 PM
    Friday, March 16, 2012 2:53 PM

All replies

  • Hi SLIVED,

    Thank you for posting.

    What is the version of your word application?

    It will be appreciate if you can share the project on the skydrive: http://www.skydrive.com and share it with me so that we can reproduce and analyze the problem more quickly. 

    I will try to reproduce on my side with your demo file. 

    Best Regards,

    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us

    • Edited by Bruce Song Tuesday, March 20, 2012 6:54 AM
    Tuesday, March 20, 2012 6:53 AM