【发布时间】:2019-07-19 12:29:30
【问题描述】:
我有一个包含 28 列数据的电子表格。根据哪些列有数据,通过使用 if 语句创建一个句子。一旦创建了句子,它就会被发送到 Word 文档。其中两个单元格中的数据需要格式化,当输出到 Word 时,A 列单元格需要加粗并加下划线,而另一个则简单地加下划线。这些单元格没有在 Excel 中格式化(尽管如果它更容易我可以更改它)。 Excel 只是保存数据,因为它是通过 Word 呈现给其他人的,它需要格式化以使读者受益。我知道如何将数据输出到 Word,但由于这是我第一次这样做,我不知道如何传递格式。我在想也许是通过选择性粘贴,但我又不知道该怎么做。以下是我的代码的适用部分。
一个名为 strLineofText 的字符串由单元格中的数据组成。随着单元格 A 和单元格 I 添加到字符串中,它们需要格式化为单元格 A 数据为粗体和下划线。我只包括下面适用于单元格 A 的部分。
Const strSHEET_NAME = "Sheet1"
Dim strLineOfText As String
Dim blnNewApp As Boolean
Dim wordApp As Object
Dim wordDoc As Object
Dim j As Long
On Error Resume Next
Set wordApp = GetObject(, "Word.Application")
On Error GoTo ErrorHandler
If wordApp Is Nothing Then
Set wordApp = CreateObject("Word.Application")
blnNewApp = True
End If
Set wordDoc = wordApp.Documents.Add()
With ThisWorkbook.Sheets(strSHEET_NAME)
For j = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
strLineOfText = .Cells(j, "A").Text & ", enlisted " & .Cells(j, "B") & ", served " & .Cells(j, "C") & ", " & .Cells(j, "D") & ", " & .Cells(j, "E")
wordApp.Selection.TypeText strLineOfText
wordApp.Selection.TypeParagraph
Next j
End With
wordApp.Visible = True
AppActivate wordApp.Caption
【问题讨论】: