首页 > 解决方案 > 从 Excel 运行 Word 宏

问题描述

我目前在 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

标签: excelvbams-word

解决方案


推荐阅读