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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,134
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Use xlSht.Rows.Count. The Word.Application doesn't have a Rows property.
 
Solution

asgreek

New Member
Joined
Apr 24, 2019
Messages
4
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!
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,134
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
That's not the right syntax. It's:

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

Forum statistics

Threads
1,136,641
Messages
5,676,954
Members
419,663
Latest member
Xbox_360

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
Top