excel - 有没有办法在使用 VBA 从 excel 导出后保持表格格式?
问题描述
我正在使用 VBA 将大型 Excel 表复制到单词表中。当我运行宏时,工作表及其格式导出正常(请参见下面的代码),但是在我保存 word 文档并重新打开或将其发送给某人后,word 文档默认恢复为原始格式并切断表格。我可能会设置页面大小,但表格的宽度和长度大小每次都会改变。
这是我正在使用的代码,取自:Source Code
Sub CopyWorksheetsToWord()
Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet
Application.ScreenUpdating = False
Application.StatusBar = "Creating new document..."
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Add
For Each ws In ActiveWorkbook.Worksheets
Application.StatusBar = "Copying data from " & ws.Name & "..."
ws.UsedRange.Copy
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste
Application.CutCopyMode = False
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter
If Not ws.Name = Worksheets(Worksheets.Count).Name Then
With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range
.InsertParagraphBefore
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
End If
Next ws
Set ws = Nothing
Application.StatusBar = "Cleaning up..."
With wdApp.ActiveWindow
If .View.SplitSpecial = wdPaneNone Then
.ActivePane.View.Type = wdNormalView
Else
.View.Type = wdNormalView
End If
End With
Set wdDoc = Nothing
wdApp.Visible = True
Set wdApp = Nothing
Application.StatusBar = False
End Sub
有没有办法来解决这个问题?谢谢!
解决方案
推荐阅读
- xml - XSLT 转换 XML:选择 uuid 并按顺序排序
- ide - 是否有任何工具或实践可用于跟踪开发人员 IDE 和工具版本?
- node.js - Nodejs - RTSP 实时摄像机源 - 分布式架构
- java - 带有gui的javamail多个附件
- python - 试图编写python代码来解决二次公式,有一些问题
- reactjs - 使用 Material UI 的 AutoComplete + react hook 表单时出错
- security - Letsencrypt 日志中的证书
- java - 如何使用 Jaxb 或 jackson 在另一个 XML 标记中解组具有相同名称但结构不同的重复 XML 标记
- spring - Apache Open JPA 和 Spring JPA 的区别
- javascript - discord.js 带 fetch 的间隔消息