我在本网站上找到了以下代码。它几乎可以工作,除了它将日期导出到Word文档中的表单。相反,我想有段落,保持原有的字体,大小和颜色在Excel中。有人可以帮忙吗?非常感谢!
Sub Export_Excel_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").Activate
Set Rng = ThisWorkbook.ActiveSheet.Range("A2:F21")
Rng.Copy
With wd.Range
.Collapse Direction:=wdCollapseStart 'Slutet av dokumentet
.InsertParagraphAfter 'Lagg till rad
.Collapse Direction:=wdCollapseStart 'Slutet av dokumentet
.PasteSpecial xlPasteFormats, False, False 'Paste with format
End With
End Sub
答案 0 :(得分:1)
这很简单,你使用pasteSpecial方法,错误的参数。这在一开始就让我错误。试试这个来粘贴纯无格式文本:
.PasteSpecial DataType:=2 ' wdPasteDataType.wdPasteText
或保留字体格式
.PasteSpecial DataType:=1 ' wdPasteDataType.wdPasteRtf
在粘贴后用单个空格替换标签:
With wd.Range
.Collapse Direction:=wdCollapseStart
.InsertParagraphAfter
.Collapse Direction:=wdCollapseStart
.PasteSpecial DataType:=2
With .Find
.ClearFormatting
.Text = vbTab
.Replacement.ClearFormatting
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End With
答案 1 :(得分:1)
我能想到的最简单的替代方法是粘贴Excel范围并将表格转换为文本:
ThisWorkbook.Sheets("sheet1").Range("A2:F21").Copy
wdApp.Selection.Paste
wdApp.DefaultTableSeparator = " "
wdApp.Selection.Previous(15).Rows.ConvertToText