【问题标题】:Access Rich Text to Word via VBA: Formatting Ignored and Added Linebreak通过 VBA 访问富文本到 Word:忽略格式并添加换行符
【发布时间】: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 字符并将字符串包装在 &lt;html&gt;&lt;/html&gt; 标记中,然后保存到临时文件

    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


    【解决方案1】:

    在解决尾随换行符后不久,我找到了解决问题前后空格的解决方案。

    我创建了一个公共子目录,允许我格式化文档的表格单元格(我的所有内容都发布到表格中)。我很可能会很快更新它以删除所有尾随和前导换行符,因为它们可能会导致难看的问题。

    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
    

    我会将空格前/后应用于整个选择,但当应用于列表时,单个项目的间距会调整。

    【讨论】:

      【解决方案2】:

      我已经尝试过这个 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 中使用样式。

      【讨论】:

      • 不幸的是,当 HTML 插入表格时,word 中的预设样式被忽略了。
      • 您所担心的行距是一个单词功能,它在相同样式的内容之间保持没有间距。使用它来关闭它:Selection.Range.Style.NoSpaceBetweenParagraphsOfSameStyle = False
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多