【发布时间】:2015-05-20 00:54:16
【问题描述】:
目标
我正在通过 VBA 将我们的访问数据库中的数据发布到 word。一些正在发布的内容是富文本格式,其中一些是使用 HTML 即时格式化的,我正在使用 .InsertFile 和一个临时 HTML 文件来完成此操作。这很有效,我可以将数据插入到 word 表格中,但是我的模板中使用的所有格式都被忽略了。我已经修复了字体大小和系列,但我还要求之前/之后的空格是特定大小,并且要删除多余的行。
问题
我无法更改我使用 .InsertFile 发布的内容的前后空格,并且在插入内容后总是有一个尾随空行。
示例
这是发布时内容通常的样子。
注意尾随空格以及列表如何紧靠单元格边框。
这是使用 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 大约一年了,但格式化单词一直被证明是我的弱点。如果您需要更多信息来帮助解决此问题,请告诉我。
编辑
在插入 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
到目前为止,这在我的所有测试中都运行良好,即使它并不优雅。这只是给我留下了一个空间之前/之后的问题,我似乎无法使用相同的方法来解决这个问题。我能够选择列表的第一行并应用 oWord.Selection.ParagraphFormat.SpaceBefore = 6 并且没有进行任何更改。回到绘图板...
【问题讨论】:
标签: html vba ms-word ms-access-2010