Excel VBA程序开发

OKJSJSF Lv.4

关注
本帖最后由 OKJSJSF 于 2025-11-10 23:09 编辑

VBA2007版在excel中能正常使用的.mergecells属性,为什么在word中操作excel.application时,就消失了呢?微软这么设计的目的是什么?最近需要这个功能,最后发现这个功能不可用,并记起多年前曾提及此事,已忘了。
359阅读
9回复 倒序

OKJSJSF 楼主 2楼

是所有的merge开头的都没有了。
Sub wdtableinserhtmltag()
    Dim xlapp As Object
    Dim cr As Range, d As Object, c As Byte, r As Integer, s1 As String, s2 As String
    On Error Resume Next
    With ActiveDocument.Tables(1)
        .Range.Select
        Selection.Copy
        Set xlapp = CreateObject("Excel.Application")
        With xlapp
            .Visible = True
            .ScreenUpdating = False
            .DisplayAlerts = False
            .Workbooks.Add
            .Cells(2).Select
            With .ActiveSheet
                .Paste
                Set cr = .usedRange
                If cr.MergeCells Then
                    For r = 1 To cr.Rows.Count
                        For c = 2 To cr.Columns.Count + 1
                            If c = 2 Then
                                s1 = "<tr><td>" & .Cells(r, 2) & "</td>"
                            Else
                                s1 = s1 & "<td>" & .Cells(r, c) & "</td>"
                            End If
                        Next
                        s2 = s2 & s1 & "</tr>"
                    Next
                    s2 = "<table>" & s2 & "</table>"
                Else
                    Set cr = .Cells(1).Resize(.usedRange.Rows.Count, .usedRange.Columns.Count + 1)
                    Set d = CreateObject("scripting.dictionary")
                    For r = 1 To cr.Rows.Count
                        For c = 1 To cr.Columns.Count
                            If c = 1 Then
                                s1 = "<tr><td></td>"
                            Else
                                If .Cells(r, c).MergeCells = False Then
                                    s1 = s1 & "<td>" & .Cells(r, c) & "</td>"
                                Else
                                    With .Cells(r, c).MergeArea
                                        If d.Exists(.Address) = False Then
                                            s1 = s1 & "<td" & IIf(.Rows.Count = 1, "", " rowspan=" & .Rows.Count) & IIf(.Columns.Count = 1, "", " colspan=" & .Columns.Count) & ">" & Cells(r, c) & "</td>"
                                            d.Add .Address, Nothing
                                        End If
                                    End With
                                End If
                            End If
                        Next
                        s2 = s2 & s1 & "</tr>"
                    Next
                    s2 = "<table>" & Replace(s2, "<tr><td></td>", "<tr>") & "</table>"
                End If
            End With
            .ActiveWorkbook.Close savechanges:=False
            .DisplayAlerts = True
            .ScreenUpdating = True
            .Visible = False
            .Quit
        End With
        Set xlapp = Nothing
        MsgBox s2
        ActiveDocument.Range(Start:=.Range.End, End:=.Range.End).InsertAfter s2 & vbCrLf
        .Delete
    End With
End Sub

ykcbf1100 Lv.7 3楼

质问微软是一件无聊的事,纯浪费时间。

sancoz Lv.2 4楼

没07版的OFFICE,但2019是有的
e0a7cc7c-93e4-482e-8eb6-5f47c37aba40.png

lss001 Lv.7 5楼

office高版本都有
屏幕截图 2025-11-11 090139.png

OKJSJSF 楼主 6楼

看来我的office不升级不行了。

OKJSJSF 楼主 7楼

谢谢家人们!

gwjkkkkk Lv.7 8楼

image.png

word中引用一下试试。。。

OKJSJSF 楼主 9楼

引用: sancoz 发表于 2025-11-11 08:45
没07版的OFFICE,但2019是有的

再问一下,2019版的word是否直接支持表格单元格的mergecells等功能而不需通过excel组件?

sancoz Lv.2 10楼

本帖最后由 sancoz 于 2025-11-12 09:54 编辑
引用: OKJSJSF 发表于 2025-11-11 20:27
再问一下,2019版的word是否直接支持表格单元格的mergecells等功能而不需通过excel组件?

WORD的表格吗?那确实没这个,WORD的是Cells.Merge

已显示全部内容