在从右到左的 MS-Word 文档中插入格式化数字作为文本

Inserting formatted numbers as text in Right to Left MS-Word document

我有 sValA = 10/140/000 个字符串。

需要在 从右到左 文档中插入 10/140/000 作为文本。

当我执行以下

ThisDocument.Bookmarks("Temp_GrandTotal").Range.Text = sValA

它returns 000/140/10

感谢任何帮助。

需要调用Selection对象的LtrRun方法,可以从Range对象中得到:

ThisDocument.Bookmarks("Temp_GrandTotal").Range.Select
Selection.LtrRun

这告诉 Word 与文档的其余部分不同,这一系列中性字符的方向应该是从左到右。

您可以添加一个UDF,通过交换字符来交换零件。然后这样称呼它:

ThisDocument.Bookmarks("Temp_GrandTotal").Range.Text = SwapParts(sValA,"\")

UDF代码:

Option Explicit

Function SwapParts(ByVal sText As String, ByVal SwapChar As String) As String
    Dim oItems As Variant, oItem As Variant, sOutput As String
    oItems = Split(sText, SwapChar)
    For Each oItem In oItems
        If Len(sOutput) > 0 Then sOutput = SwapChar & sOutput
        sOutput = oItem & sOutput
    Next oItem
    SwapParts = sOutput
End Function

尝试:

Sub UpdateRTLBookmark(StrBkMk As String, StrTxt As String)
Dim RngBkMk As Range, RngSel As Range
With ActiveDocument
  If .Bookmarks.Exists(StrBkMk) Then
    Set RngSel = .Selection.Range
    Set RngBkMk = .Bookmarks(StrBkMk).Range
    RngBkMk.Text = StrTxt
    RngBkMk.Select
    Selection.RtlRun
    .Bookmarks.Add StrBkMk, RngBkMk
    RngSel.Select
  End If
End With
Set RngBkMk = Nothing: Set RngSel = Nothing
End Sub

您可以使用如下代码调用:

Sub Demo()
Application.ScreenUpdating = False
Dim StrBkMk As String, StrTxt As String
StrBkMk = "Temp_GrandTotal": StrTxt = "10/140/000"
Call UpdateRTLBookmark(StrBkMk, StrTxt)
Application.ScreenUpdating = True
End Sub

这种方法的优点是您可以根据需要再次更新书签范围。

如果您希望文本为 LTR 格式,请将 Selection.RtlRun 更改为 Selection.LtrRun。我也会更改宏名称。