Hi all,
I am currently trying to automate the transfer of data from a Word to excel (copy from word & paste special as HTML in excel). After importing into excel there are few macro's which i am running to do the compare.
I've managed to get the VBA code which will copy the word document data & paste in excel as HTML for around 50 pages, but I'm stumped when the word document contains pages greater than 50. The word document contains different tables, text etc.
Could someone please guide me on how to narrow down the code to first copy 50 pages from word document & paste special as HTML in excel & then again go to the 51 page & copy from 51 to 100 & so on. Usually the word document consists of more than 500 pages.
Please find below the code. Please note here the word document name i have referred as "Data.doc" & the excel file i have referred as "MasterData".
Public Lastrow As Integer
Sub copyMacro()
Dim appWD As Word.Application
Dim n As Integer
Dim Tpages As Integer
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.8")
appWD.Visible = True
ChangeFileOpenDirectory "I:\"
Documents.Open fileName:="data.doc", ConfirmConversions:= _
False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
n = appWD.ActiveDocument.Tables.Count
ActiveDocument.Repaginate
Tpages = ActiveDocument.BuiltinDocumentProperties(wdPropertyPages)
WD.DisplayAlerts = wdAlertsNone
Application.DisplayAlerts = False
If Tpages <= 300 Then
appWD.Selection.WholeStory
appWD.Selection.Copy
'Call the paste procedure
paste
'Call the Lastrow procedure to know the last row
ElseIf Tpages > 300 And Tpages < 609 Then
' Get the rows in pages of 100
appWD.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="100"
appWD.Selection.MoveUp Unit:=wdLine, Count:=3, Extend:=wdExtend
appWD.Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
appWD.Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
appWD.Selection.Copy
paste
appWD.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="201"
appWD.Selection.MoveUp Unit:=wdLine, Count:=3, Extend:=wdExtend
appWD.Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
appWD.Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
appWD.Selection.Copy
'Sheets("MainData").ActiveRow = Lastrow
paste
End If
MsgBox Lastrow
appWD.ActiveDocument.Close
' Next i
' Close the Word application
appWD.Quit
End Sub
Sub paste()
Worksheets("MainData").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveSheet.PasteSpecial _Format:="HTML", _DisplayAsIcon:=False
End Sub