通过 VBA 访问富文本到 Word:忽略格式并添加换行符
Access Rich Text to Word via VBA: Formatting Ignored and Added Linebreak
目标
我正在通过 VBA 将我们的访问数据库中的数据发布到 word。一些正在发布的内容是富文本格式,一些正在使用 HTML 即时格式化,我正在使用 .InsertFile
和一个临时 HTML 文件来完成这个。这有效,我能够将数据插入到 word 中的表格中,但是我的模板中使用的所有格式都被忽略了。我已经修复了字体大小和系列,但我还要求 space-before/after 为特定大小并删除额外的行。
问题
我无法更改使用 .InsertFile
发布的内容的 space-before 或 after 并且插入内容后始终有一个尾随空白行。
例子
这是内容在发布时通常的样子。
注意尾随 space 以及列表如何紧靠单元格边界。
这是使用 HTML 即时生成的内容示例。
<html>
<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>etc...</li>
</ul>
</html>
这是内容发布到模板之前的样子。
代码
奇迹发生的地方,或者更确切地说,没有发生的地方。 htmlToRich
函数检查空值,删除 Unicode 字符并将字符串包装在 <html></html>
标记中,然后保存到临时文件
With oWord.selection
.Collapse Direction:=wdCollapseEnd
.Move Unit:=wdCharacter, Count:=-1
' Using htmlToRich function, format the string then wrap in
' HTML tags
.InsertFile _
filename:=htmlToRich("<ul>" & sReference & "</ul>")
' Format, unsuccessfully...
.ParagraphFormat.SpaceBefore = 6
.ParagraphFormat.SpaceAfter = 6
.Font.Size = 10
End With
最后的话
我已经使用 VBA 大约一年了,但是格式化 word 一直被证明是我的弱点。如果您需要任何更多信息来帮助解决此问题,请告诉我。
编辑
在插入 HTML 并设法想出这一点后,我又尝试删除多余的行。
With oWord.selection
' Using htmlToRich function, format the string then wrap in
' HTML tags
.InsertFile _
filename:=htmlToRich("<ul>" & sReference & "</ul>")
' Move the cursor to the end of the cell
.move wdCharacter
' move the cursor back two characters
.MoveEnd count:=-2
' Select the last two cell characters, which should always be a
' line break and a blank cell character.
.MoveEnd
' delete selected
.Range.Delete
End With
到目前为止,这在我的所有测试中都运行良好,即使它并不优雅。这只会给我留下一个 space-before/after 问题,我似乎无法使用相同的方法解决该问题。我能够 select 列表的第一行并应用 oWord.Selection.ParagraphFormat.SpaceBefore = 6
并且没有进行任何更改。回到绘图板...
我试过这个html
<html>
<table width="100%">
<tr>
<td>test1</td><td>test1</td><td>test1</td>
<td rowspan="3">
<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>etc...</li>
</ul>
</td>
</tr>
<tr>
<td>test1</td><td>test1</td><td>test1</td>
</tr>
<tr>
<td>test1</td><td>test1</td><td>test1</td>
</tr>
</table>
</html>
并且没有多余的段落来了。
对于格式,如果您已经有模板,您可以在 word 中使用样式。
在解决尾随换行符后不久,我找到了 space-前后问题的解决方案。
我创建了一个 public sub,它允许我格式化文档的 table 单元格(我的所有内容都发布到 tables)。我很可能会很快更新它以删除所有尾随和前导换行符,因为它们会导致难看的问题。
Public Sub formatRichText(oWord As Word.Application, oDoc As Word.Document, iTable As Integer, _
iRow As Integer, iCol As Integer)
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' Delete trailing paragraph character
With oWord.selection
.Move wdCharacter
.MoveEnd wdCharacter, -2
.MoveEnd
.Delete
End With
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' Fix space before
With oWord.selection
.Move Count:=-1
.MoveEnd wdLine
.ParagraphFormat.SpaceBeforeAuto = False
.ParagraphFormat.SpaceBefore = 6
End With
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' fix space after
With oWord.selection
.Move Count:=1
.MoveEnd Count:=-1
.ParagraphFormat.SpaceAfterAuto = False
.ParagraphFormat.SpaceAfter = 6
End With
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' Format font face and size
With oWord.selection
.Font.Name = "Arial"
.Font.Size = 10
End With
End Sub
我会将 space-before/after 应用于整个选择,但当应用于列表时,各个项目的间距会调整。
目标
我正在通过 VBA 将我们的访问数据库中的数据发布到 word。一些正在发布的内容是富文本格式,一些正在使用 HTML 即时格式化,我正在使用 .InsertFile
和一个临时 HTML 文件来完成这个。这有效,我能够将数据插入到 word 中的表格中,但是我的模板中使用的所有格式都被忽略了。我已经修复了字体大小和系列,但我还要求 space-before/after 为特定大小并删除额外的行。
问题
我无法更改使用 .InsertFile
发布的内容的 space-before 或 after 并且插入内容后始终有一个尾随空白行。
例子
这是内容在发布时通常的样子。
注意尾随 space 以及列表如何紧靠单元格边界。
这是使用 HTML 即时生成的内容示例。
<html>
<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>etc...</li>
</ul>
</html>
这是内容发布到模板之前的样子。
代码
奇迹发生的地方,或者更确切地说,没有发生的地方。 htmlToRich
函数检查空值,删除 Unicode 字符并将字符串包装在 <html></html>
标记中,然后保存到临时文件
With oWord.selection
.Collapse Direction:=wdCollapseEnd
.Move Unit:=wdCharacter, Count:=-1
' Using htmlToRich function, format the string then wrap in
' HTML tags
.InsertFile _
filename:=htmlToRich("<ul>" & sReference & "</ul>")
' Format, unsuccessfully...
.ParagraphFormat.SpaceBefore = 6
.ParagraphFormat.SpaceAfter = 6
.Font.Size = 10
End With
最后的话
我已经使用 VBA 大约一年了,但是格式化 word 一直被证明是我的弱点。如果您需要任何更多信息来帮助解决此问题,请告诉我。
编辑
在插入 HTML 并设法想出这一点后,我又尝试删除多余的行。
With oWord.selection
' Using htmlToRich function, format the string then wrap in
' HTML tags
.InsertFile _
filename:=htmlToRich("<ul>" & sReference & "</ul>")
' Move the cursor to the end of the cell
.move wdCharacter
' move the cursor back two characters
.MoveEnd count:=-2
' Select the last two cell characters, which should always be a
' line break and a blank cell character.
.MoveEnd
' delete selected
.Range.Delete
End With
到目前为止,这在我的所有测试中都运行良好,即使它并不优雅。这只会给我留下一个 space-before/after 问题,我似乎无法使用相同的方法解决该问题。我能够 select 列表的第一行并应用 oWord.Selection.ParagraphFormat.SpaceBefore = 6
并且没有进行任何更改。回到绘图板...
我试过这个html
<html>
<table width="100%">
<tr>
<td>test1</td><td>test1</td><td>test1</td>
<td rowspan="3">
<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>etc...</li>
</ul>
</td>
</tr>
<tr>
<td>test1</td><td>test1</td><td>test1</td>
</tr>
<tr>
<td>test1</td><td>test1</td><td>test1</td>
</tr>
</table>
</html>
在解决尾随换行符后不久,我找到了 space-前后问题的解决方案。
我创建了一个 public sub,它允许我格式化文档的 table 单元格(我的所有内容都发布到 tables)。我很可能会很快更新它以删除所有尾随和前导换行符,因为它们会导致难看的问题。
Public Sub formatRichText(oWord As Word.Application, oDoc As Word.Document, iTable As Integer, _
iRow As Integer, iCol As Integer)
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' Delete trailing paragraph character
With oWord.selection
.Move wdCharacter
.MoveEnd wdCharacter, -2
.MoveEnd
.Delete
End With
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' Fix space before
With oWord.selection
.Move Count:=-1
.MoveEnd wdLine
.ParagraphFormat.SpaceBeforeAuto = False
.ParagraphFormat.SpaceBefore = 6
End With
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' fix space after
With oWord.selection
.Move Count:=1
.MoveEnd Count:=-1
.ParagraphFormat.SpaceAfterAuto = False
.ParagraphFormat.SpaceAfter = 6
End With
oDoc.Tables(iTable).Cell(iRow, iCol).Select
' Format font face and size
With oWord.selection
.Font.Name = "Arial"
.Font.Size = 10
End With
End Sub
我会将 space-before/after 应用于整个选择,但当应用于列表时,各个项目的间距会调整。