微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Word到Excel保留格式的问题

如何解决从Word到Excel保留格式的问题

我在尝试将Word表导出到Excel工作表的同时仍在努力保持数字格式。我的代码如下所示,但是我注释掉的部分是我当前正在尝试做的事情(并且失败了)。有人可以指出我在做什么错吗?

Public Sub copyTabletoExcel()

    Dim xlApp As Excel.Application
    Dim xlwb As Excel.Workbook
    Dim doc As Word.Document
    Dim tbl As Word.Table
    Dim lastRow As Long,lastColumn As Integer
    Dim tblRange As Word.Range
    Dim excelRange As Excel.Range
    
    Set doc = ThisDocument
    Set xlApp = CreateObject("Excel.Application") 
    Set xlwb = xlApp.Workbooks.Add 'Create new workbook
    
    Set tbl = doc.Tables(2)
    
    With tbl:
    
        lastRow = .Rows.Count
        lastColumn = .Columns.Count
        
        Set tblRange = .Cell(1,1).Range
        tblRange.End = .Cell(lastRow,lastColumn).Range.End
        tblRange.copy

        xlwb.Worksheets(1).Paste

        'This part doesn't work,but I'm trying to do something like this:
        'Set excelRange = xlwb.Worksheets("Sheet1").Range("A1")
        'excelRange.PasteSpecial (xlPasteValuesAndNumberFormats)
       
    End With

    Set xlwb = nothing
    Set xlApp = nothing
    Set tbl = nothing
    Set doc = nothing

End Sub

感谢您的帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。