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

对着背影说爱祢 提交于 2020-01-16 13:14:28

问题


I have sValA = 10/140/000 string.

Need inserting 10/140/000 as text in a right-to-left document.

When I performs below

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

It returns 000/140/10

Any helps are appreciated.


回答1:


You can add a UDF to swap the parts by a swap character. Then call it like:

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

UDF code:

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



回答2:


You need to call the LtrRun method on the Selection object, which you can get from the Range object:

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

This tells Word that the direction of this sequence of direction-neutral characters should be left-to-right, unlike the rest of the document.




回答3:


Try:

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

which you'd call with code like:

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

The advantage of this approach is that you can update the bookmarked range again, if needed.

If you want the Text to be in LTR format, change Selection.RtlRun to Selection.LtrRun. I'd change the macro name, too.



来源:https://stackoverflow.com/questions/48274685/inserting-formatted-numbers-as-text-in-right-to-left-ms-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!