Seun
Seun

Reputation: 11

"Method or data member not found" (VBA Word)

I have this portion of code below that keeps receiving the "Method or data member not found" error message:

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

End If

Not sure how to solve. Here is the complete code for reference:

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

Thanks!

Upvotes: 1

Views: 895

Answers (1)

Tim Williams
Tim Williams

Reputation: 166306

myStyle is not a property of the Paragraph object

Try:

If p.Style = "Headings_Sub" Then

Upvotes: 2

Related Questions