Word vba跳转到下一页

时间:2017-10-11 13:22:10

标签: vba word-vba

好,

所以对于我的学徒生涯,我需要记录我每天的工作。由于这将是一个很长的文档,我写了一个VBA脚本,它已经编写了与日期保持一致的内容。

问题是,格式应该是每页一周。意思是,当我星期六到达时,脚本必须跳到下一页的开头(并且可能用段落填充结果空间)。如果有任何功能可以帮助我,我完全不知道。我认为应该有一个,这听起来不像是一个太奇特的问题。

现在这不是一个至关重要的问题,但它仍然令人讨厌,我想知道是否有任何解决方案。迄今为止的研究也没有做到这一点。

如果有助于理解,请提供一些代码。我基本上是评论部分:

For i = InitDate To endDate
jump:
  If CurrentDate = endDate Then GoTo EndSub
    x = x + 1
    strWeekday = weekday(CurrentDate, vbUseSystemDayOfWeek)
    txtStr = Format(CurrentDate, "dddddd")
     With Selection
     .Font.Bold = True
     .Font.Underline = True
     .TypeText Text:=txtStr
     .TypeParagraph
    If x = 4 Then GoTo Thursday
    If x = 5 Then GoTo Friday
   'If x = 6 Then GoTo NewSheet
    If x = 6 Then x = 0

    .TypeParagraph
    End With
CurrentDate = CurrentDate + 1

Next i

1 个答案:

答案 0 :(得分:1)

ActiveDocument.StoryRanges(wdMainTextStory).InsertAfter ChrW(12)

将在文本末尾插入硬分页符。 (或者,对于r.InsertAfter ChrW(12)变量Range,通常情况为r。然后您可以从那里继续进行!

修改

现在您已经添加了代码,我可以向您展示更多内容。但是,由于您没有包含所有代码,因此我无法提供按原样运行的内容。另外,我认为您可以将CurrentDate替换为i(反之亦然)。

For i = InitDate To endDate
jump:
  If CurrentDate = endDate Then GoTo EndSub
    x = x + 1
    strWeekday = weekday(CurrentDate, vbUseSystemDayOfWeek)
    txtStr = Format(CurrentDate, "dddddd")
    With Selection
         .Font.Bold = True
         .Font.Underline = True
         .TypeText Text:=txtStr
         .TypeParagraph
    End With

     ' ** By the way, I would suggest you not GoTo out of a With block - that strikes me as likely to cause confusion later on.
     If x = 4 Then GoTo Donnerstag  
     If x = 5 Then GoTo Freitag
     'If x = 6 Then GoTo NewSheet
     If x = 6 Then       ' ** Try this
         Selection.InsertAfter ChrW(12)
         Selection.Collapse wdCollapseEnd
     End If
     If x = 6 Then x = 0

     Selection.TypeParagraph
     CurrentDate = CurrentDate + 1
Next i