设置NewStyle不起作用

时间:2009-10-20 11:26:13

标签: vba word-vba

在MS Project 2003中使用VBA并使用Word文档我尝试使用

创建新样式
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

一旦执行Set CreateStyleHeadingTask语句,这将崩溃。 如果我删除'Set CreateStyleheadingTask ='它不会崩溃。 有什么问题,我该如何纠正呢?

1 个答案:

答案 0 :(得分:0)

错误是因为函数返回数据类型。您需要指定Word.Style,而不仅仅是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