首页 > 解决方案 > 更改字符中的语言 ID

问题描述

我有一个 word 文档,其中一些字符(如“[”)在 lang.charaters LanguageID=2057 和一些 LanguageID=1037

我试图通过以下代码更改它:

For Each oChr In pRange.Characters
         If oChr.LanguageID = 2057 Then
           oChr.LanguageID = 1037
         End If
Loop Until pRange Is Nothing

但分配后 LanguageID 没有改变

为什么 ?我怎样才能让它工作?

标签: vbams-word

解决方案


这是我不久前编写的一个更改校对语言的程序。

您是正确的,可以将校对语言属性应用于字符,但您无需检查每个字符即可重置。以下检查文档的所有故事并设置校对语言。您可以将常量更改为您想要的,而不是 EnglishUS。

Sub ProofingLanguageEnglishUSAllStory()    ' based on field updater by Greg Maxey
    ' https://gregmaxey.com/word_tip_pages/word_fields.html
    ' Charles Kenyon 6 November 2018
    ' https://answers.microsoft.com/en-us/msoffice/forum/all/force-all-documents-to-be-edited-in-uk-english/df6d1f8e-5426-49d9-bea0-5620d0208294
    ' Changes proofing language to English US in all stories of document
    ' Language IDs https://docs.microsoft.com/en-us/office/vba/api/word.wdlanguageid
    Dim rngStory As Word.range
    Dim lngValidate As Long ' do not know purpose of this
    Dim oShp As Shape
    lngValidate = ActiveDocument.Sections(1).Headers(1).range.StoryType
    For Each rngStory In ActiveDocument.StoryRanges
      'Iterate through all linked stories
      Do
        On Error Resume Next
        rngStory.LanguageID = wdEnglishUS  ' delete or comment out if you do not want to change the language ID

        rngStory.NoProofing = False        Select Case rngStory.StoryType
          Case 6, 7, 8, 9, 10, 11
            If rngStory.ShapeRange.Count > 0 Then
              For Each oShp In rngStory.ShapeRange
                If oShp.TextFrame.HasText Then
                   ' Comment out or delete the next line if you do not want to change proofing language
                   oShp.TextFrame.TextRange.LanguageID = wdEnglishUS  ' delete or comment out if you do not want to change the language ID
                   ' Comment out or delete the next line if you do not want to change the "no proofing" setting
                   oShp.TextFrame.TextRange.NoProofing = False
                End If
              Next
            End If
          Case Else
            'Do Nothing
        End Select
        On Error GoTo -1
        'Get next linked story (if any)
        Set rngStory = rngStory.NextStoryRange
      Loop Until rngStory Is Nothing
      Next
End Sub

这是页面上处理不同问题的三个宏之一。宏使用 Word 常量作为语言 ID,而不是数字等价物。这是我在 Microsoft Answers 网站上关于以下主题的文章:修复校对语言问题


推荐阅读