“未找到方法或数据成员”(VBA Word)

时间:2017-06-07 15:39:33

标签: vba ms-word

我有以下代码部分继续接收“方法或数据”     找不到成员“错误消息:

If p.myStyle = "Headings_Sub" Then
p.Range.Select

End If

不确定如何解决。以下是完整的参考代码:

Sub testCopyPasteVBA()

testCopyPasteVBA Macro

Dim wordDoc As Object
Dim oXL As Excel.Application
Dim DocTarget As Word.Document
Dim Target As Excel.Workbook
Dim tSheet As Excel.Worksheet
Dim StrTxt As String
Dim oRng As Word.Range
Dim p As Paragraph
Dim myStyle As Style

Set oRng = ActiveDocument.Range
oRng.Start = ActiveDocument.Bookmarks("D_Start").Range.End
oRng.End = ActiveDocument.Bookmarks("D_End").Range.Start

Set wordDoc = GetObject(, "word.application")
oRng.Select

Set myStyle = ActiveDocument.Styles.Add(Name:="Headings_Sub", _
Type:=wdStyleTypeCharacter)
With myStyle.Font
.Bold = True
.Italic = False
.Name = "Times New Roman"
.Size = 12
.AllCaps = True

End With

If p.myStyle = "Headings_Sub" Then
p.Range.Select

End If

Selection.Copy

'If Excel is running
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application

End If

oXL.Visible = True
Set Target = oXL.Workbooks.Add
Set tSheet = Target.Sheets(1)
tSheet.Paste

End Sub

谢谢!

1 个答案:

答案 0 :(得分:2)

myStyle不是Paragraph对象的属性

尝试:

If p.Style = "Headings_Sub" Then