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!
YowE3K
23.8k7 gold badges26 silver badges40 bronze badges
asked Jun 7, 2017 at 15:39
0
myStyle
is not a property of the Paragraph object
Try:
If p.Style = "Headings_Sub" Then
answered Jun 7, 2017 at 15:57
Tim WilliamsTim Williams
150k8 gold badges96 silver badges124 bronze badges
4
- Remove From My Forums
-
Question
-
Hi,
From a Newb. I searched to no avail to get help with this simple program that exports text from Access to a Word doc. Here is the code that is giving me the error message on compiling:
Public Sub WordXPort()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim fld As FieldSet db = CurrentDb()
Set rs = db.OpenRecordset(«Headers»)
Set fld = rs.Fields(0)WordEx
Do Until rs.EOF
sel.TypeText Text:=»ENTRY — » & fld.Value &
vbCrLf & vbCrLf
rs.MoveNext
LoopEnd Sub
The underlined/bolded object is where the compiler finds the error. Any help would be greatly appreciated. Thanks!
Answers
-
Hi Bernie,
The fix was to simply declare fld as DAO.Field. Thanks for you help!
-
Marked as answer by
Wednesday, April 29, 2015 6:04 PM
-
Marked as answer by
-
#1
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
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!
How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
RoryA
MrExcel MVP, Moderator
-
#2
Use xlSht.Rows.Count
. The Word.Application
doesn’t have a Rows
property.
-
#3
Use
xlSht.Rows.Count
. TheWord.Application
doesn’t have aRows
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
-
#4
That’s not the right syntax. It’s:
VBA Code:
If .Bookmarks.Exists("Text" & c) Then
ryguy7272
-
#1
I keep getting a message that says the following:
Compile Error:
Method or data member not found
The code looks like this:
Private Sub CommandButton1_Click()
ListBox1.BoundColumn = 1
ActiveDocument.Variables(«FirstName»).Range.InsertBefore
ListBox1.ValueNext i
ListBox1.BoundColumn = 2
ActiveDocument.Variables(«LastName»).Range.InsertBefore
ListBox1.ValueNext i
‘ etc.
ActiveDocument.Fields.Update
UserForm1.Hide
End Sub
And the error occurs on this line:
ActiveDocument.Variables(«FirstName»).Range.InsertBefore
I got this idea form this site:
http://www.pcreview.co.uk/forums/thread-875661.php
I get my ListBox1 to load with this sub:
Sub Userform_Initialize()
Dim i As Integer, Addressee As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
‘ Open the database
Set db = OpenDatabase(«C:Documents and
SettingsRMSDesktopContacts.xls», False, False, «Excel 8.0»)
‘ Retrieve the recordset
Set rs = db.OpenRecordset(«SELECT * FROM `List`»)
‘ Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
‘ Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
‘ Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
‘ListBox1.List = rs.GetRows(NoOfRecords) ‘Transposed List
‘ Cleanup
rs.Close
db.Close
‘AddressBlock = db
Set rs = Nothing
Set db = Nothing
End Sub
Does anyone have any ideas as to what I’m doing wrong?
This would be so awesome if I could get it to work!!!
Thanks,
Ryan—
Advertisements
Jay Freedman
-
#2
I’m afraid my colleague Doug Robbins misspoke in the article you
cited. When you’re using document variables and {DOCVARIABLE
«varname»} fields in the document, your macro needs to assign values
to the variables, rather than inserting anything physically into the
document body.
Each of those lines should be like
ActiveDocument.Variables(«FirstName»).Value = ListBox1.Value
Then when the ActiveDocument.Fields.Update statement runs, the
corresponding DocVariable fields will display the values of the
variables.
Advertisements
ryguy7272
-
#3
Thank you 1,000,000*
That was EXACTLY what I was looking for (I won’t even say how long I was
looking for it). I have been working with VBA in Excel for a while, and I am
thrilled to be learning VBA for Word now.
For those interested, here is the final arrangement of code:
Code in Module:
Sub Userform_Initialize()
UserForm1.Show
End Sub
Code in UserForm (there are two subs here):
Sub Userform_Initialize()
Dim i As Integer, Addressee As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
‘ Open the database
Set db = OpenDatabase(«C:Documents and SettingsContcts.xls», False,
False, «Excel 8.0»)
‘ Retrieve the recordset
Set rs = db.OpenRecordset(«SELECT * FROM `List`»)
‘ Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
‘ Set the number of Columns = number of Fields in recordset
ListBox1.ColumnCount = rs.Fields.Count
‘ Load the ListBox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
‘ListBox1.List = rs.GetRows(NoOfRecords) ‘Transposed List
‘ Cleanup
rs.Close
db.Close
‘AddressBlock = db
Set rs = Nothing
Set db = Nothing
End Sub
Private Sub CommandButton1_Click()
ListBox1.BoundColumn = 1
ActiveDocument.Variables(«FirstName»).Value = ListBox1.Value
ListBox1.BoundColumn = 2
ActiveDocument.Variables(«LastName»).Value = ListBox1.Value
ListBox1.BoundColumn = 3
ActiveDocument.Variables(«Company»).Value = ListBox1.Value
ListBox1.BoundColumn = 4
ActiveDocument.Variables(«BusinessStreet»).Value = ListBox1.Value
ListBox1.BoundColumn = 5
ActiveDocument.Variables(«BusinessCity»).Value = ListBox1.Value
ListBox1.BoundColumn = 6
ActiveDocument.Variables(«BusinessState»).Value = ListBox1.Value
ListBox1.BoundColumn = 7
ActiveDocument.Variables(«BusinessPostalCode»).Value = ListBox1.Value
‘ etc.
ActiveDocument.Fields.Update
UserForm1.Hide
End Sub
In your Excel file, name your range (my range is called ‘List’ and the Excel
file is called ‘Contacts.xls’). Finally, on the UserForm, you need to have a
CommandButton, named CommandButton1 and you also need a ListBox, named
ListBox1. The last step is to go to Word, click Insert > Field > DocVariable
(assign a name in the ‘New Name’ box) > Ok. Assign the links to
‘DocVariable’ wherever required throughout your document, fiddle with it a
little if it doesn’t work after the first attempt…you will get it to work.
Kind Regards,
Ryan—
-
06-10-2021, 09:24 AM
#1
Application.Inputbox getting Method of Data Member not found
Upgraded my office version a few months ago, and for the first time today used an application.inputbox macro that I hadn’t used in a while. Got a «Method or Data Member Not Found» error. Couldn’t figure out why it failed, so copied a couple of different sample macros from other sites, had the same issue. «InputBox» works find, but «Application.Inputbox» fails, and I can’t understand why. Below is a sample macro that fails for me. Any help would be appreciated.
I know I’m not stupid, but I suspect I’m a lot stupider than I think I am
-
06-10-2021, 03:41 PM
#2
Re: Application.Inputbox getting Method of Data Member not found
That is Excel code, not Word. Word doesn’t have an Application.Inputbox method.
-
06-10-2021, 04:07 PM
#3
Re: Application.Inputbox getting Method of Data Member not found
Ah, that’s good to know! I didn’t realize there would be a difference on that, but it certainly explains why my code is failing.
-
06-10-2021, 10:51 PM
#4
Re: Application.Inputbox getting Method of Data Member not found
You don’t need to use Application.Inputbox in Excel or Word. Inputbox on its own works fine in both and Application.Inputbox causes an error in Word.
Cheers,
Paul Edstein
[Fmr MS MVP — Word]
-
06-11-2021, 02:13 AM
#5
Re: Application.Inputbox getting Method of Data Member not found
You do in this case in Excel as it�s returning a range object (type:=8)
-
06-11-2021, 08:40 AM
#6
Re: Application.Inputbox getting Method of Data Member not found
I agree with Rorya, that was the whole reason for using Application.Inbox, but I think I can reconfigure my macro to use just the vanilla inputbox version. If not I’ll make a form.
Thanks for all your help.