我目前在excel中有一个宏,它允许我选择要打开的Word文档,然后选择要导入的表。但是,此宏无法在excel拆分单元格中正确格式化表格。理想情况下,我希望可以修复此宏,但无法找到方法。
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
wdFileName = Application.GetOpenFilename("Word files ,*.doc;*.docx;*.docm", , _
"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
TableNo = wdDoc.TAbles.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If
With .TAbles(TableNo).Range.Copy
Range("A1").Activate
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End With
End With
Set wdDoc = Nothing
End Sub
因此,我有一个新脚本(在Word中),该脚本可以按所需方式格式化表格并将其复制到剪贴板,并在excel中将下一个宏从剪贴板粘贴。
无论如何,我想做的是: 1)从Excel中选择打开Word文档 2)打开Word文档后自动运行宏 3)退出word并返回到excel,我将在该系列中调用下一个宏。
任何想法?
Sub RunWordMacroExe()
Dim oWFile As String
wdFileName = Application.GetOpenFilename("Word files ,*.doc;*.docx;*.docm", , _
"Browse for file containing table to be imported")
wordApp.ActiveDocument.CopyTableForExcel
End Sub