Hey all,
Trying to get a massive macro to pull information from a website, and put it in excel (done)
then it needs to sort the information into the format that I need (done)
and then I want to open up a template word document and put all of that info into an existing table (getting there, but I need help...)
A portion of the code I've been able to pull together is below:
Set wdApp = CreateObject("Word.application")
Set wdDoc = wdApp.Documents.Open _
(Filename:="C:\Users\564930\Desktop\Templatev1.docx", ReadOnly:=False)
wdApp.Visible = True
ErrorType = "Find Replace"
wdApp.Selection.Find.Execute
With wdApp.Selection.Find
.Text = "[EnterMonth]"
.Replacement.Text = EnterMonth
.Execute Replace:=wdReplaceAll
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With wdApp.Selection.Find
.Text = "[EnterYear]"
.Replacement.Text = EnterYear
.Execute Replace:=wdReplaceAll
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wdApp.Selection.Find.Execute
'Copy table from excel to word
Dim LastRow As Long
Dim Rng As Excel.Range
Dim Wks As Worksheet
Set Wks = Worksheets("Sheet 1")
Set Rng = Wks.Range("A2:G2")
LastRow = Wks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
Set Rng = Rng.Resize(RowSize:=LastRow)
Dim C As Long
Dim RX As Long
Dim W As Long
Dim wdTbl As Object
W = 2
For RX = 2 To LastRow
Set wdTbl = wdDoc.Tables(2)
For C = 1 To Rng.Columns.Count
Rows(W).Insert Shift:=xlDown
wdTbl.Rows(W).Select
Rng.Rows(RX).Copy
wdApp.Selection.Paste
Next C
W = W + 1
Next RX
wdApp.Visible = True
Set wdApp = Nothing
Set wdDoc = Nothing
Set wdTbl = Nothing
For some reason the Find Replace won't work, no error, it just goes right through the code to the next part. It was working before and I don't think I changed anything in that section of code, but it won't work...
Next problem, when it runs an error comes up that says: '5941: requested member of the collection does not exist', and highlights this line: "wdTbl.Rows(W).Select"
Before the error comes up, it does manage to copy something in to the word document, but there is no text, and the color of the box is yellow.
Please point me in the right direction to fix this problem. Thanks!
Bookmarks