有没有办法可以自动将信息从Microsoft Word表复制到Microsoft Excel表?
你什么意思?您在Excel中有一个表,当您进行更改时,您希望在Word中更新?
—
James Mertz 2013年
有没有办法可以自动将信息从Microsoft Word表复制到Microsoft Excel表?
Answers:
是的,这很有可能。正如我昨天所展示的那样,您可以通过VBScript使用Word和Excel自动化来执行VBA支持的任何操作。尝试以下VBS脚本:
Option Explicit
Dim objWord, objDoc, objExcel, objSheet, tbl, srcRow, dstRow, col, cell
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
objWord.Documents.Open "C:\MyDoc.docx", False, True ' path, confirmconversions, readonly
Set objDoc = objWord.ActiveDocument
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
objExcel.Workbooks.Add
Set objSheet = objExcel.ActiveSheet
dstRow = 0
For Each tbl In objDoc.Tables
For srcRow = 1 To tbl.Rows.Count
col = 0
For Each cell In tbl.Rows(srcRow).Cells
col = col + 1
objSheet.Cells(dstRow+srcRow, col).Value = Left(cell.Range.Text, Len(cell.Range.Text)-1)
Next
Next
dstRow = dstRow + SrcRow
Next
objWord.Quit 0 ' wdDoNotSaveChanges
Set objWord = Nothing
Set objDoc = Nothing
objExcel.ActiveWorkbook.SaveAs "C:\MyBook.xlsx"
objExcel.Quit
Set objExcel = Nothing
Set objSheet = Nothing
MsgBox "All table data copied from document to workbook!"