VBA将嵌入的Excel工作表转换为Word文件中的单词表

时间:2019-06-10 03:43:00

标签: excel vba word-table

我需要将嵌入的excel工作表对象转换为word文件中的单词表,我目前正在使用的是打开嵌入的excel工作表对象,选择内容并粘贴到word中。有没有更好的方法来简化此操作?

我尝试创建一个mirco,但是.OLEformat保持错误,并说无法在水平线上访问此成员。

Sub ConvertXLObjs()
Application.WindowState = wdWindowStateMinimize
Dim i As Long, j As Long, k As Long, Rng As Range, bDel As Boolean
Dim objOLE As Word.OLEFormat, objXL As Object
With ActiveDocument
For i = .InlineShapes.Count To 1 Step -1
With .InlineShapes(i)
  If Not .OLEFormat Is Nothing Then
    If Split(.OLEFormat.ClassType, ".")(0) = "Excel" Then
      Set Rng = .Range
      Set objOLE = .OLEFormat
      objOLE.Activate
      Set objXL = objOLE.Object
      With objXL.ActiveSheet
        .Range("$A$1:" & _
        .Cells.SpecialCells(11).Address).Copy ' 11 = xlCellTypeLastCell
      End With
      objXL.Application.Undo
      .Delete
      With Rng
        .Characters.First.PasteAndFormat wdTableInsertAsRows
        .MoveEnd wdParagraph, 2
        With .Tables(1)
          .AllowAutoFit = False
          .BottomPadding = 0
          .LeftPadding = 0
          .RightPadding = 0
          .TopPadding = 0
          .Rows.AllowBreakAcrossPages = False
          .Rows.HeightRule = wdRowHeightExactly
          If .Uniform = True Then
            For j = .Columns.Count To 1 Step -1
              bDel = True
              For k = 1 To .Columns(j).Cells.Count
                If Len(.Columns(j).Cells(k).Range.Text) > 2 Then
                  bDel = False
                  Exit For
                End If
              Next
              If bDel = True Then
                .Columns(j).Delete
              Else
                Exit For
              End If
            Next
          End If
        End With
      End With
    End If
  End If
End With
Next
End With
Set objXL = Nothing: Set objXL = Nothing: Set Rng = Nothing
Application.WindowState = wdWindowStateNormal
MsgBox "Finished processing!"
End Sub

该代码来自https://social.msdn.microsoft.com/Forums/office/en-US/5955da06-725d-45f2-aa1b-5eb37c0646c6/how-to-convert-all-embeded-excel-sheets-in-word-into-words-tables?forum=worddev

示例: Example

0 个答案:

没有答案