Microsoft Word VBA:在设置字符数后插入回车符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22464053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:23:13  来源:igfitidea点击:

Microsoft Word VBA: Insert Carriage Return after set character amount

vbams-wordword-vba

提问by hobbybrouwer

I'm trying to create a VBA that will enter a carriage return after the text has reached 3800 characters. Ideally the script would stop at the beginning of the last word and enter a new return, but it would be tremendous just to have the carriage returns.

我正在尝试创建一个 VBA,它会在文本达到 3800 个字符后输入回车符。理想情况下,脚本会停在最后一个单词的开头并输入一个新的回车符,但如果只使用回车符,那就太棒了。

Any assistance would be greatly appreciated.

任何帮助将不胜感激。

Thank you in advance!

先感谢您!

回答by Kazimierz Jawor

Please remember, that in word even paragraph sign belongs to characters collections.

请记住,在单词中,即使是段落符号也属于字符集。

Here is the code:

这是代码:

Sub Solution()

    Dim i as integer
    i = 3800

        If ActiveDocument.Range(i - 1, i) = " " Then
            ActiveDocument.Range(i, i).InsertBefore Chr(11)
        Else
            ActiveDocument.Range(i + 1, i + 1).InsertAfter Chr(11)
        End If

End Sub

You can consider replacing Chr(11)with Chr(13).

您可以考虑替换Chr(11)Chr(13).