[英]Macro to copy excel range of data as table to word goes outside margins
I want to write macro to copy table from excel to word. 我想编写宏以将表格从Excel复制到Word。 It should maintain formatting and must auto-fit to word.
它应该保持格式,并且必须自动适应单词。 Table should not go out of range when pasted to word.
粘贴到单词时,表格不应超出范围。
Dim wdApp As Object
Dim wd As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wd = wdApp.Documents.Add
wdApp.Visible = True
Sheets("Sheet1").Select
Range("A1:E36").Select
Selection.Copy
wdApp.Selection.PasteExcelTable False, False, False
Above code wors. 以上代码令人担忧。 It copies excel range of data as table to word.
它将Excel范围的数据作为表格复制到Word。 However table goes outside margins of word document.
但是表超出了Word文档的边距。 Data in Excel -
Excel中的数据-
Data in Word after Copy -
复制后Word中的数据-
添加到最后
wd.Tables(1).AutoFitBehavior wdAutoFitWindow
Try this: 尝试这个:
wdApp.Selection.PasteExcelTable False, True, False 'causing losing excel formatting
Appended: 附:
'change fileformat and filename as per your need
wdApp.Selection.PasteExcelTable False, False, False
wd.SaveAs Filename:="demo.docm", FileFormat:=wdformatdocm
wd.Tables(1).AutoFitBehavior wdAutoFitContent 'you may try wdAutoFitWindow as well
尝试
wd.Tables(1).Range.ParagraphFormat.SpaceAfter = 0
将括号放在wdAutoFitWindow周围
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.