Fixed index in VBA macro is incorrect

+1 vote
asked May 12, 2014 in Bug by anonymous

in function

CreateStyleImg() there is fixed index 1 as in line

''''''''''''''''''''''''''''''''''''''

myStyle.BaseStyle = ActiveDocument.Styles.Item(1).BaseStyle

''''''''''''''''''''''''''''''''''''''''

which is not correct as if the currecnt document contains many styles then this line will fail

I think to solve this problem the code need to get this index programatically as below.

'''''''''''''''''''''''''''''''''''''''''''''''''

Function CreateStyleImg()
    Dim i As Integer
    Dim correctIndex As Integer
    On Error GoTo CreateStyleImgAdding
    Set myStyle = ActiveDocument.Styles("PlantUMLImg")
    For i = 1 To 256
        If ActiveDocument.Styles.Item(i).NameLocal = "PlantUMLImg" Then
        correctIndex = i
        i = 256
        End If
    Next i
    myStyle.BaseStyle = ActiveDocument.Styles.Item(correctIndex).BaseStyle
    On Error GoTo 0
    Exit Function
CreateStyleImgAdding:
    Set myStyle = ActiveDocument.Styles.Add(Name:="PlantUMLImg", Type:=wdStyleTypeParagraph)
    myStyle.AutomaticallyUpdate = True
End Function

''''''''''''''''''''''''''''''''''''''''

am I right?

 

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

[Antispam2 Feature: please please wait 1 or 2 minutes (this message will disappear) before pressing the button otherwise it will fail](--------)
To avoid this verification in future, please log in or register.
...