从嵌入在MS Word中的工作表中存储数据

时间:2018-10-10 17:59:15

标签: excel vba

我是这个委员会的新手。我搜索了过去与我有关的各种问题的答案,但未成功找到解决我问题的方法。

我需要做的是: 1)在Excel文档中使用vba / vb,找到嵌入在Word文档中的Excel电子表格。 2)从其单元读取数据。 3)将数据存储在当前Excel文档的单元格中。

我已经能够找到嵌入式Excel电子表格,但是看不到如何将其数据存储到当前电子表格的单元格中。这是我找到嵌入式电子表格的代码:

Sub GetWordata()

  Dim objWord As Object
  Set objWord = CreateObject("Word.Application")
  Dim singleLine As Paragraph
  Dim lineText As String
  Dim word_app As word.Application
  Dim wDoc As word.Document
  Dim r As Integer
  Dim i As Long, Rng As Range
  Dim Evalue As String
.
.
.
  With activeDocument
    For i = .InlineShapes.Count To 1 Step -1
      With .InlineShapes(i)
        If Not .OLEFormat Is Nothing Then
          If Split(.OLEFormat.ClassType, ".")(0) = "Excel" Then
            MsgBox "Excel File"
          End If
        End If
      End With
    Next
  End With

上面的代码中将显示消息框,表明我已经找到了嵌入式电子表格。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这将从文件夹中所有Word文件中的所有表中导入数据。这只是使用COM for Word和Excel(从Excel运行代码)可以完成的事情的一小部分。

Sub WordToExcel()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim x As Integer
Dim strFilename As String
Dim strFolder As String
Dim temp As String

Set wdApp = New Word.Application
'initialise counter
x = 1
'search for first file in directory
strFolder = "C:\test\"
strFilename = Dir(strFolder & "*.doc")
'amemd folder name
Do While strFilename <> ""
Set wdDoc = wdApp.Documents.Open(strFolder & strFilename)
temp = wdDoc.Tables(1).Cell(2, 1).Range.Text 'read word cell
Range("A2").Offset(x, 0) = temp
temp = wdDoc.Tables(1).Cell(2, 2).Range.Text 'read word cell
Range("A2").Offset(x, 1) = temp
'etc
temp = wdDoc.Tables(1).Cell(2, 3).Range.Text 'read word cell
Range("A2").Offset(x, 2) = temp
temp = wdDoc.Tables(1).Cell(2, 4).Range.Text 'read word cell
Range("A2").Offset(x, 3) = temp

wdDoc.Close
x = x + 1
strFilename = Dir
Loop
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub