且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用VBA仅将Word文档中的某些页面复制到excel中

更新时间:2023-12-04 13:32:10

您可以通过表集合访问表-您可能需要确定想要的两个索引号,我假设它们是前两个在文档中

You can access tables through the tables collection - you may need to workout what index number the two you want are, I've assumed they're the first two in the document

 Sub convertpdftowordthenexcel()

 Dim wordapp As Word.Application
 Dim input1 As String
 input1 = "C:\Users\Me\Desktop\Fruitjuice.pdf"

 'open pdf in word
 Set wordapp = New Word.Application

 wordapp.documents.Open Filename:=input1, Format:="PDF Files", ConfirmConversions:=False
 wordapp.Visible = True
'copy the first two tables of the word file
 wordapp.ActiveDocument.tables(1).range.Copy     

 'go to excel and paste it there
 with Workbooks("openpdfusingdoc.xlsm").Worksheets("Sheet1")
     .Cells(1, 1).PasteSpecial Format:="Text"
     wordapp.ActiveDocument.tables(2).range.Copy    
    .cells(.rows.count,1).end(xlup).offset(2,0).pastespecial format:="Text"
  end with
  wordapp.Quit savechanges:=wdDoNotSaveChanges

 End Sub

(PS永不使用Select)

(PS Never use Select)