首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用VBA仅将某些页面从word文档复制到excel中。

使用VBA仅将某些页面从word文档复制到excel中。
EN

Stack Overflow用户
提问于 2019-03-21 10:44:16
回答 1查看 881关注 0票数 0

1)我用Microsoft word打开一个pdf,通过excel VBA。

2)从word doc中,我只希望将第3页和第4页(这两个是不带标题的表格)复制到excel中。

3)目前,我只能将整个单词doc复制到excel中,这可能会很麻烦。

下面是我的代码:

代码语言:javascript
运行
复制
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 content of the word file
wordapp.ActiveDocument.Content.Copy     '<------this is where I want to change

'go to excel and paste it there
Workbooks("openpdfusingdoc.xlsm").Worksheets("Sheet1").Activate
Worksheets("Sheet1").Activate
Cells(1, 1).Select
ActiveSheet.PasteSpecial Format:="Text"

wordapp.Quit savechanges:=wdDoNotSaveChanges

End Sub

对如何做到这一点有什么建议吗?

非常感谢大家!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-21 19:19:34

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

代码语言:javascript
运行
复制
 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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55273025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档