如何通过VBA在Word中测量两端对齐文字之间的间距?

huangapple go评论72阅读模式
英文:

How to measure spaces between justified words in Word by VBA?

问题

I am looking for a method to better arrange justified paragraphs in Word. Because when justifying a paragraph, often there is too much space between the words on a line. For short texts, I visually look for the "ugly" lines, I select them and use small Macros (one or more times) like this:

Sub spacingMinus005()
    With Selection.Font
        If .Spacing >= 10 Then
            .Spacing = -0.05
        Else
            .Spacing = .Spacing - 0.05
        End If
    End With
End Sub

But for long documents, it's too laborious! I would like:
1/ a script which scans the text by measuring the apparent spaces between the justified words and which highlights these words. But I can't find the function to measure these apparent spaces...
2/ with this function, it won't be difficult to write a cycle that goes through these ugly lines by gradually shrinking them, like in my Marco.

I tried the following functions, native in Word:

But the result is insufficient. I also looked for an Office add-in or Macros – even paid – without finding any.

Do you have a solution? I would be very grateful to you. Thank you in advance for your answer and have a nice day!

英文:

Please, I am looking for a method to better arrange justified paragraphs in Word. Because when justifying a paragraph, often there is too much space between the words on a line. For short texts, I visually look for the "ugly" lines, I select them and use small Macros (one or more times) like this:

Sub spacingMinus005()
    With Selection.Font
        If .Spacing >= 10 Then
            .Spacing = -0.05
        Else
            .Spacing = .Spacing - 0.05
        End If
    End With
End Sub

But for long documents, it's too laborious! I would like:
1/ a script which scans the text by measuring the apparent spaces between the justified words and which highlights these words. But I can't find the function to measure these apparent spaces...
2/ with this function, it won't be difficult to write a cycle that goes through these ugly lines by gradually shrinking them, like in my Marco.

I tried the following functions, native in Word:

But the result is insufficient. I also looked for an Office add-in or Marcos – even paid – without finding any.

Do you have a solution? I would be very grateful to you.

Thank you in advance for your answer and have a nice day!

答案1

得分: 2

抱歉,以下是翻译好的部分:

不幸的是,解决这个问题的唯一可行方法是将每个文档更改为 Word 2010 兼容模式,然后应用 WordPerfect 对齐设置并保存。以下是一个执行此操作的宏:

Sub BetterJustification()
    With ActiveDocument
        .SetCompatibilityMode wdWord2010
        .Compatibility(wdWPJustification) = True
        .SaveAs2 CompatibilityMode:=14
    End With
End Sub

请注意,默认情况下,Word 的“另存为”命令会尝试将文件另存为当前的 Word 格式,这会重置对齐设置为 Microsoft 的不佳设置。为了防止这种情况发生,Word 的“另存为”对话框需要选中 与之前版本的 Word 保持兼容性 选项。这是一个粘性设置,对所有文件保持一致,直到用户取消选中它。我没有找到使用 VBA 来选中该复选框的方法。

英文:

Unfortunately, the only workable way to fix this problem is to change each document to Word 2010 compatibility mode, then apply the WordPerfect Justification settings and save. Here's a macro to do that:

Sub BetterJustification()
    With ActiveDocument
        .SetCompatibilityMode wdWord2010
        .Compatibility(wdWPJustification) = True
        .SaveAs2 CompatibilityMode:=14
    End With
End Sub

Please note that by default, Word's Save As command will try to upsave to the current Word format, which will reset the justification to Microsoft's crappy settings. To prevent this, Word's Save As dialog needs to have the option checked for Maintain compatibility with previous versions of Word. This is a sticky setting that remains the same for all files until a user unchecks it. I haven't found a way to check that box using VBA.

huangapple
  • 本文由 发表于 2023年6月27日 18:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定