这是我使用的代码,但是我想添加其他文件类型以能够打开,例如docm,docx等。这样,当对话框提示打开文件时,我可以选择任何可用的word文件类型?
您还必须在表格编号中嵌入代码;有没有一种基于表格标题插入表格的方法?
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),*.doc", , _
"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)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With
Set wdDoc = Nothing
End Sub
答案 0 :(得分:1)
我的Google-fu找到了以下内容: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-getopenfilename-method-excel
简而言之,将"Word files (*.doc),*.doc"
替换为"Word files (*.doc),*.doc;*.docx;*.docm"
等。链接页面的相关部分是如何构造filefilter参数。用逗号分隔的成对列表,然后是文本,然后是扩展名,允许有多个选择。如果有多个文件扩展名,请使用分号将它们分开。