tags:

views:

14

answers:

1

Using VBA in MS Project 2003 and working with a Word Document I try to create a new style using Function CreateStyleHeadingTask(NameStyle As String) As Style Set CreateStyleHeadingTask = Nothing If Not wdDoc Is Nothing Then With wdDoc Set CreateStyleHeadingTask = .Styles.Add(Name:=NameStyle, Type:=wdStyleTypeParagraph) '// .Styles.Add Name:=NameStyle, Type:=wdStyleTypeParagraph With .Styles(NameStyle).Font .Size = 14 .Bold = True '// .Color = wdColorBlue .Color = wdColorRed End With Set CreateStyleHeadingTask = .Styles(NameStyle) End With End If End Function

This will crash as soon as I execute the Set CreateStyleHeadingTask statement. If I remove the 'Set CreateStyleheadingTask =' it doesn't crash. What is wrong and how can I correct it?

A: 

The error is because of the function return datatype. You need to specify Word.Style, not just Style.

Function CreateStyleHeadingTask(NameStyle As String) As Word.Style

    Set CreateStyleHeadingTask = Nothing

    If Not wdDoc Is Nothing Then
        With wdDoc
            Set CreateStyleHeadingTask = .Styles.Add(Name:=NameStyle, Type:=1)
            With .Styles(NameStyle).Font
                .Size = 14
                .Bold = True
                .Color = wdColorRed
            End With
            Set CreateStyleHeadingTask = .Styles(NameStyle)
        End With
    End If

End Function