简体   繁体   中英

MS word macro vba For Each page

my problem is that I have to run this macro in microsoft office word, which inserts a rectangle at the beginning and at the end of the page Now, the macro is registered with word and is as follows.

Sub Macro1()
'
' Macro1 Macro
'
'
    ActiveDocument.Shapes.AddShape(msoShapeRectangle, 0#, -23.65, 634.05, _
        45.15).Select
    Selection.ShapeRange.ScaleHeight 1.14, msoFalse, msoScaleFromTopLeft
    ActiveWindow.ActivePane.VerticalPercentScrolled = 51
    ActiveDocument.Shapes.AddShape(msoShapeRectangle, 0#, 812.4, 623.3, _
        92.45).Select
    ActiveWindow.ActivePane.VerticalPercentScrolled = 0
End Sub

I tried to run a For Each like the following:

Dim pages As Page

For Each pages in ActiveDocument
    'here I have pasted the contents of macro1 except the first and last line' 
Next Page

My goal is to repeat the recorded macro for each page of the document. I ask you for help to create this macro because it would save me a lifetime of time thanks.

thank you all

PS: I'm not a native English speaker if you do not understand something ask me

Why do you not use a header and a footer ? On the Insert menu click Header, Footer. Then insert a rectangle in the header and the footer. Those rectangles will appear on every page.

The below code will also work. Put the cursor on the first page and execute.

Arrivederci.

Dim a
a = ActiveDocument.BuiltInDocumentProperties("Number of Pages")

For i = 1 To a
ActiveDocument.Shapes.AddShape(msoShapeRectangle, 0#, -23.65, 634.05, _
    45.15).Select
Selection.ShapeRange.ScaleHeight 1.14, msoFalse, msoScaleFromTopLeft
ActiveWindow.ActivePane.VerticalPercentScrolled = 51
ActiveDocument.Shapes.AddShape(msoShapeRectangle, 0#, 812.4, 623.3, _
    92.45).Select
ActiveWindow.ActivePane.VerticalPercentScrolled = 0
Selection.GoToNext what:=wdGoToPage
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM