2009-10-20 62 views
0

使用VBA和與Word文檔工作,我嘗試創建使用集NewStyle不MS Project 2003的工作

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 

這是我執行設置CreateStyleHeadingTask聲明將盡快崩潰了新的風格。 如果我刪除'Set CreateStyleheadingTask =',它不會崩潰。 什麼是錯的,我該如何糾正它?

回答

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