目标: 在Word中使用VBA,我希望能够在Word文档中键入或粘贴文本,然后确保每个行字都包含一定数量的字符(通常为50,尽管这可能会改变)。我宁愿不使用文档顶部的标尺进行手动调整,尤其是当字体不是恒定宽度时!
尝试失败: 我试图使用以下内容,导致错误"超出范围的价值":
Public Sub setWordsPerLine()
ActiveDocument.PageSetup.CharsLine = 50
End Sub
我还试图在一个段落中每50个字符插入一个返回字符。但是这会导致类型不匹配错误:
For Each pg In ActiveDocument.Paragraphs
b = pg.Range.Characters.Count
c = 50
If b > c Then
For atch = c To pg.Range.Characters.Count Step c
ActiveDocument.Range(pg.Range.Characters(atch)).InsertBefore (Chr(13))
Next
End If
Next
需要帮助:
我应该使用另一种方法,属性或函数来执行此操作吗? Paragraphs.RightIndent = x
基于点,而不是字符。
答案 0 :(得分:1)
我已经改进了你的第二个解决方案,现在工作正常。 (我用简单的lorem ipsum
文档做了一些测试)。
Sub qTest_line()
Dim PG As Paragraph
Dim B, C, ATCH, S
For Each PG In ActiveDocument.Paragraphs
B = PG.Range.Characters.Count
S = PG.Range.Start
C = 50
If B > C Then
For ATCH = (S + C) To (S + B) Step C
'check the position
If Len(Trim(ActiveDocument.Range(ATCH - 1, ATCH + 1).Text)) < 2 Then
'just insert new line mark
ActiveDocument.Range(ATCH, ATCH).InsertAfter Chr(11)
Else
'move to the beginnng of word
ActiveDocument.Range(ATCH, ATCH).Select
Selection.MoveLeft wdWord
Selection.InsertBefore Chr(11)
ATCH = Selection.Start
End If
Next
End If
Next
End Sub
答案 1 :(得分:0)
KazJaw的other answer here运行良好,但我已修改为包含一些改进,并希望发布修改以防其他人遇到此问题。
改进包括:
使用更优雅的方法确定是否在单词的中间,通过替换
If Len(Trim(ActiveDocument.Range(char_index - 1, char_index + 1).Text)) < 2 Then
带
If ActiveDocument.Range(char_index).Text = " " Or _
ActiveDocument.Range(char_index).Text = "-" Then
这是修改后的代码,再次感谢KazJaw的原始解决方案。
Public Sub wrap_words()
'adapted from stackoverflow
'https://stackoverflow.com/a/19109049/2658159
Dim para_index As Paragraph
Dim para_index_numchars, char_index, para_index_start As Long
'set the max line length constraint
Const line_max As Byte = 50
'loop through each paragraph
For Each para_index In ActiveDocument.Paragraphs
'find number of characters
para_index_numchars = para_index.Range.Characters.Count
'find the paragraph starting point
para_index_start = para_index.Range.Start
'if the paragraph has more than the predetermined amount of characters,
If para_index_numchars > line_max Then
'loop through each character starting with line_max position
'and stepping by line_max position, to the end of the paragraph
For char_index = (para_index_start + line_max) To _
(para_index_start + para_index_numchars) Step line_max
'if there is not a word in this position...
If ActiveDocument.Range(char_index).Text = " " Or _
ActiveDocument.Range(char_index).Text = "-" Then
'...just insert new line mark
ActiveDocument.Range(char_index, char_index).InsertAfter Chr(13)
Else
'... if there is a word, or a hyphenated word that
'can be split, move to the beginnng of word or the
'end of the hyphenated section.
ActiveDocument.Range(char_index, char_index).Select
Selection.MoveLeft unit:=wdWord, Count:=1
'insert newline at the beginning
Selection.InsertBefore Chr(13)
'since a new paragraph created before the word,
'the previous paragraph structure has changed.
'change char_index to reflect that change.
char_index = Selection.Start
End If
Next
End If
Next
End Sub