Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 循环添加重复到Word文档末尾的空行_Vba_Ms Word - Fatal编程技术网

Vba 循环添加重复到Word文档末尾的空行

Vba 循环添加重复到Word文档末尾的空行,vba,ms-word,Vba,Ms Word,我正试图编写一个宏来分解某人的一长串地址。基本上,宏需要在每三行之后添加一个空行,并对整个文档重复此过程。我已经完成了最初的操作,但我不知道如何让它重复并在文档末尾停止。我在网上搜索过,一直在寻找只适用于Excel中情况的while循环。我不确定如何指定循环何时以Word结尾 以下是我现在拥有的: Sub AddFix () Do Selection.MoveDown Unit:= wdline, Count:= 3 Selection.InsertParagraph Loop Until (

我正试图编写一个宏来分解某人的一长串地址。基本上,宏需要在每三行之后添加一个空行,并对整个文档重复此过程。我已经完成了最初的操作,但我不知道如何让它重复并在文档末尾停止。我在网上搜索过,一直在寻找只适用于Excel中情况的while循环。我不确定如何指定循环何时以Word结尾

以下是我现在拥有的:

Sub AddFix ()

Do
Selection.MoveDown Unit:= wdline, Count:= 3
Selection.InsertParagraph
Loop Until (Selection.End = ActiveDocument.Content.End - 1)

EndSub

如何让此sub在整个文档中工作?

这将满足您的要求,但是可能会有一个更紧凑的解决方案,这正是我所能想到的

Sub InsertLines()

    Dim lTotalLines As Long
    Dim lCurrentLine As Long
    Dim bContinue As Boolean

    lTotalLines = ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)

    If lTotalLines < 4 Then Exit Sub

    lCurrentLine = 4
    bContinue = True

    While bContinue
        Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=lCurrentLine
        Selection.InsertParagraph

        lCurrentLine = lCurrentLine + 3
        lTotalLines = ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
        If lCurrentLine > lTotalLines Then bContinue = False
    Wend

End Sub
子插入行()
暗线和长线一样长
暗淡的L电流和长的一样
Dim b作为布尔值继续
lTotalLines=ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
如果lTotalLines<4,则退出Sub
lCurrentLine=4
b继续=真
继续
Selection.GoTo What:=wdGoToLine,Which:=wdGoToAbsolute,Count:=lCurrentLine
选择.插入段落
lCurrentLine=lCurrentLine+3
lTotalLines=ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
如果lCurrentLine>lTotalLines,则bContinue=False
温德
端接头

这将满足您的需求,但是可能会有一个更紧凑的解决方案,这正是我所能想到的

Sub InsertLines()

    Dim lTotalLines As Long
    Dim lCurrentLine As Long
    Dim bContinue As Boolean

    lTotalLines = ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)

    If lTotalLines < 4 Then Exit Sub

    lCurrentLine = 4
    bContinue = True

    While bContinue
        Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=lCurrentLine
        Selection.InsertParagraph

        lCurrentLine = lCurrentLine + 3
        lTotalLines = ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
        If lCurrentLine > lTotalLines Then bContinue = False
    Wend

End Sub
子插入行()
暗线和长线一样长
暗淡的L电流和长的一样
Dim b作为布尔值继续
lTotalLines=ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
如果lTotalLines<4,则退出Sub
lCurrentLine=4
b继续=真
继续
Selection.GoTo What:=wdGoToLine,Which:=wdGoToAbsolute,Count:=lCurrentLine
选择.插入段落
lCurrentLine=lCurrentLine+3
lTotalLines=ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)
如果lCurrentLine>lTotalLines,则bContinue=False
温德
端接头

根据Chumble的答案,您将希望在文本中后退一步

Sub InsertLines()

    Dim lTotalLines As Long
    Dim lCurrentLine As Long

    lTotalLines = ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)

    For lCurrentLine = lTotalLines To 3 Step -3
        Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=lCurrentLine
        Selection.InsertParagraph
    Next lCurrentLine

End Sub

在Chumble的答案的基础上,你会想在文本中后退一步

Sub InsertLines()

    Dim lTotalLines As Long
    Dim lCurrentLine As Long

    lTotalLines = ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)

    For lCurrentLine = lTotalLines To 3 Step -3
        Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=lCurrentLine
        Selection.InsertParagraph
    Next lCurrentLine

End Sub

欢迎来到这么饿。。。你的第一篇文章做得很好。我已经清理了一些小事情:不要在标题中使用“标签”。还要确保你问了一个明确的问题。最后,因为这是未来访问者的参考,所以不要使用多余的文本。至于可能的答案:计算文档中的行数,除以三,然后对x=1进行
,以计算下一个x的行数,但是我希望能够写一些无论文档中有多少行都可以执行的东西。到目前为止,我还没有在网上找到任何内容。Peckish,您可以使用我的代码来识别流动文档上的行总数,并在每行中向后添加空行。欢迎使用so Peckish。。。你的第一篇文章做得很好。我已经清理了一些小事情:不要在标题中使用“标签”。还要确保你问了一个明确的问题。最后,因为这是未来访问者的参考,所以不要使用多余的文本。至于可能的答案:计算文档中的行数,除以三,然后对x=1进行
,以计算下一个x的行数,但是我希望能够写一些无论文档中有多少行都可以执行的东西。到目前为止,我还没有在网上找到任何内容。Peckish,您可以使用我的代码来标识流体文档上的行总数,并在每一行中向后添加空行。因为您正在添加行,所以您可能希望从末尾开始向后操作。否则,每次添加结束目标移动时。因为您正在添加行,所以您可能希望从结束开始并向后操作。否则,每次添加结束目标移动时。