将带书签的部分从Word导入到Excel

时间:2018-10-16 10:23:01

标签: excel vba import ms-word

嗨,我正在尝试使用VBA(在Excel中)将带书签的部分(以“开始”开头的书签和以“结束”结尾的书签)导入Word工作表。

我尝试了以下代码,但无法正常工作。我的范围说明有误:

rngDoc = .Range(Start:=.Bookmarks("Start").Range.Start, End:=.Bookmarks("End").Range.End))

,它告诉我在集合中找不到以下元素(翻译自德语“ der Sammlung vorhanden中的Das aufgeforderte Element ist nicht”)。有谁知道如何用其他方式描述范围?

Sub ImportPartAHoftorbilanz()

Dim wdDoc As Word.Document
Dim wdFileName As Variant
Dim rngStart As Range
Dim rngEnd As Range


'Get Wordfile and Open It
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    Dim rngDoc As Object
    rngDoc = .Range(Start:=.Bookmarks("Start").Range.Start, End:=.Bookmarks("End").Range.End)
    rngDoc.Copy SaveChanges:=False
End With

'Paste Selection
Range("A1").PasteSpecial Paste:=xlPasteValues

Set wdDoc = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

从“ With wdDoc”语句开始,将其余代码更改为以下内容:

With wdDoc
    Dim rngDoc As word.Range
    Set rngDoc = .Range(Start:=.Bookmarks("Start").Range.Start, End:=.Bookmarks("End").Range.End)
    rngDoc.Copy
End With

'Paste Selection
Range("A1").PasteSpecial Paste:=xlPasteValues

wdDoc.Close SaveChanges:=False
Set wdDoc = Nothing