VBA Error: Compile Error: Method or Data Member not found. When Pasting Excel Data into Word

asgreek

New Member
Joined
Apr 24, 2019
Messages
4
Hello,

I'm running into a VBA error: Compile Error: Method or Data Member not found. I'm trying to run my code which copies data from Excel and pastes it into Word FormFields. The error occurs on this line of code


VBA Code:
For r = 3 To xlSht.Range("A" & .Rows.Count).End(xlUp).Row

and it specifically highlights
Rich (BB code):
.Rows.

My entire code is:


Code:
Sub CC()
Dim wdApp As New Word.Application, wdDoc As Word.Document, wdRng As Word.Range
Dim xlSht As Worksheet, r As Long, c As Long
Set xlSht = ThisWorkbook.Sheets("Sheet1")
With wdApp
  .Visible = True
  For r = 3 To xlSht.Range("A" & .Rows.Count).End(xlUp).Row
    If xlSht.Range("A" & r).EntireRow.Hidden = False Then
      Set wdDoc = wdApp.Documents.Add(Template:="X:\abcde.docx")
      With wdDoc
        For c = 1 To 4
          If .Bookmarks("Text" & c).Exists Then
            .Bookmarks("Text" & c).Range.Fields(1).Result.Text = xlSht.Cells(r, c).Text
          Else
            MsgBox "Form field bookmark 'Text" & c & "' doesn't exist in:" & vbCr & _
              .AttachedTemplate.FullName
          End If
        Next
        .SaveAs2 Filename:="X:\abcde.docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        .Close
      End With
    End If
  Next r
  .Quit
End With
End Sub

Any ideas why this is occurs? Thanks!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Use xlSht.Rows.Count. The Word.Application doesn't have a Rows property.
 
Upvote 0
Solution
Use xlSht.Rows.Count. The Word.Application doesn't have a Rows property.
Wow, thank you! I'm running into the same problem with the
Rich (BB code):
If .Bookmarks("Text" & c).Exists Then
, the Exists part of the code is highlighted which I'm guessing the Word.Application doesnt have this property as well. Do you know the alternative? Thank you so much!
 
Upvote 0
That's not the right syntax. It's:

VBA Code:
If .Bookmarks.Exists("Text" & c) Then
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top