【问题标题】:Import bookmarked section from Word to Excel将已添加书签的部分从 Word 导入 Excel
【发布时间】:2019-03-20 20:36:37
【问题描述】:

您好,我正在尝试使用 VBA(在 Excel 中)将一个带书签的部分(开头的书签名为“Start”,末尾的书签名为“End”)从 Word 文档中导入 Excel 工作表。

我尝试了以下代码,但它不起作用。我的范围描述有误:

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

它告诉我它在集合中找不到以下元素(翻译自德语“Das aufgeforderte Element ist nicht in der Sammlung vorhanden”)。有人知道如何描述范围吗?

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。 “它不起作用。” 不是有用的错误描述。而是描述你的代码做什么与你期望它做什么,或者你在哪里得到错误和哪个。还有“Can someone help me?” is not an actual question,因为我们无法回答这个问题。而是提出与您的代码相关的问题。
  • @Pᴇʜ 感谢尝试将其更改为更好。我是新来的,也是 VBA 的新手
  • 您是否在文档中插入了两个名为“开始”和“结束”的书签?
  • @CindyMeister 是的,我确实在文档中插入了两个书签(Word-Document)
  • 你的代码的基本问题是你定义了'rngStart As Range'和'rngEnd As Range';您需要使用 'rngStart As Word.Range' 和 'rngEnd As Word.Range'

标签: excel vba import ms-word


【解决方案1】:

从“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 2017-09-22
    • 2022-07-06
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多