【问题标题】:Copy only certain pages from word doc into excel using VBA使用 VBA 仅将 word doc 中的某些页面复制到 excel 中
【发布时间】:2019-03-21 02:44:15
【问题描述】:

1) 我通过 excel VBA 使用 Microsoft Word 打开一个 pdf。

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

3) 目前只能将整个word doc复制到excel中,比较麻烦。

下面是我的代码:

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

关于如何做到这一点的任何建议?

非常感谢大家!

【问题讨论】:

    标签: excel vba pdf ms-word


    【解决方案1】:

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

     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)

    【讨论】:

    • 谢谢。像魅力一样工作。但是表索引以 2 而不是 1 开头。所以我猜文档的创建者在文档中的某处插入了一个表作为第一个表。但是有一个问题,为什么从不使用 select?我使用 VBA 已经有一段时间了,之前也看到过这个建议,但没有人解释过为什么我们应该避免使用 select
    • 考虑 r=3(其中 r 是指向“A1”的范围类型的对象变量)与 Range("a1").Select:Activecell.formula = 3 后者需要两倍的行数,但进程数量大大增加。第一个版本将值 3 放入 r 指向的内存位置 - 这需要 1 个时钟周期。第二个要求excel检查单元格是否可见,如果不滚动屏幕使其显示,检查编辑栏是否可见,如果是则将其设置为新单元格的详细信息,检查是否显示栏是否显示,如果是,...
    • ...查找显示了哪些自动更新公式并更新它们,最后重绘屏幕。然后对于第二行,查找什么是活动单元格,查找它在内存中的位置,将 3 放入该单元格,查找应用于该单元格的数字格式,计算数字将如何以该格式显示,最后重绘屏幕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多