如何在所有 Word 页面上放置一个文本框(无页眉)?

如何在所有 Word 页面上放置一个文本框(无页眉)?

我尝试在每一页上添加一个文本框,以便能够以我自己的方式(稍后)修改一本书,其中有“该页面上的正确答案(页面将位于特定页面上,直到我开发它我才会知道)”。

这是我的代码:

Sub Test()
Dim oShp As Word.Shape
Dim i As Long

For i = ActiveDocument.Shapes.Count To 1 Step -1
Set oShp = ActiveDocument.Shapes(i)

Dim Box As Shape
    Dim Sp() As String
    Debug.Print ActiveDocument.Name
    Set Box = ActiveDocument.Shapes.AddTextbox( _
                             Orientation:=msoTextOrientationHorizontal, _
                             Left:=358.1, Top:=544, Width:=168, Height:=30)
    Sp = Split(ActiveDocument.Name, ".")
    


With Box
        With .TextFrame.TextRange
            .Text = ("Respuestas correctas en la página")
            .Font.Name = "Arial"
            .Font.Size = 9
            .Font.Color = wdColorWhite
            .Font.Italic = wdToggle
            .Font.Bold = True
        End With
        .Line.Visible = msoFalse
    End With


Next i
End Sub

问题是它在同一页上重复循环多次,并且不会转到下一页。如果没有页眉,我该如何解决这个问题?

答案1

以防有人觉得它有用。需要的是一个 for 循环并确定Rng要放入的变量Anchor:=Rng

Sub Text_box_all_pages()
Dim oShp As Word.Shape
Dim i As Long, Rng As Range, Shp As Shape
With ActiveDocument
  For i = 1 To .ComputeStatistics(wdStatisticPages)
  
    Set Rng = .GoTo(What:=wdGoToPage, Name:=i)
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
    Rng.Collapse wdCollapseStart

Set oShp = ActiveDocument.Shapes(i)

Dim Box As Shape
    Dim Sp() As String
    Debug.Print ActiveDocument.Name
    Set Box = ActiveDocument.Shapes.AddTextbox( _
                             Orientation:=msoTextOrientationHorizontal, _
                             Left:=358.1, Top:=544, Width:=168, Height:=30, Anchor:=Rng)
    Sp = Split(ActiveDocument.Name, ".")
    


With Box
        With .TextFrame.TextRange
            .Text = ("Respuestas correctas en la página")
            .Font.Name = "Arial"
            .Font.Size = 9
            .Font.Color = wdColorWhite
            .Font.Italic = wdToggle
            .Font.Bold = True
        End With
        .Line.Visible = msoFalse
    End With


Next i
End With
End Sub

相关内容