Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Excel运行Word宏_Excel_Vba_Ms Word - Fatal编程技术网

从Excel运行Word宏

从Excel运行Word宏,excel,vba,ms-word,Excel,Vba,Ms 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

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

您是否熟悉
应用程序。运行允许您执行宏的
方法?是;我的问题是把它放到代码中,然后告诉我们你用它做了什么,并解释它是如何“不起作用的”。可能是重复的