开发者

set NewStyle doesn't work

开发者 https://www.devze.com 2022-12-08 18:59 出处:网络
Using VBA in MS Project 2003 and working with a Word Document I try to create a new style using Fun开发者_如何学JAVAction CreateStyleHeadingTask(NameStyle As String) As Style

Using VBA in MS Project 2003 and working with a Word Document I try to create a new style using

Fun开发者_如何学JAVAction 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消