Insert a new page in word without header

别来无恙 提交于 2019-12-12 05:29:18

问题


I am trying to insert a new page in word i.e insert a section break. The problem is that i want to change this page to A3 landscape and remove all headers, which my code currently does not do. How to modify my code below to achieve this?

Below is my current code that insert new page, but keeps the header and a4 portrait:

If wordDrawingExist Then
    Selection.EndKey Unit:=wdStory
    Selection.InsertFile FileName:=wFile, link:=False

    Set wb = Documents.Open(wFile)
    Selection.WholeStory
    Selection.Copy
    Documents(docLogSkjema).Activate
    Selection.EndKey Unit:=wdLine
    Selection.InsertBreak Type:=wdPageBreak
    Selection.Paste

    wb.Close False
End If

wFile is fullpath to a wordfile, which is basically a pdf to word from freepdfsolutions.com (Tried inserting the pdf directly but then the quality of the pdf was so bad that numbers were hard to read) and wordDrawingExist is the boolean saying if the wordfile exist or not


回答1:


OK, first of all, you will need a section break, not a simple page break:

Selection.InsertBreak Type:=wdSectionBreakNextPage

To change to landscape orientation:

Selection.PageSetup.Orientation = wdOrientLandscape

Make sure you are in the section you want to change. Note that after inserting the section break, the cursor will be in the new section.

To change the size to A3, you will need to set the size manually:

With Selection.PageSetup
 .PageWidth = CentimetersToPoints(42)
 .PageHeight = CentimetersToPoints(29.7)
End With

To delete the header:

selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete

Your selection doesn't include multiple sections, so from the one section it touches, you need the first (duh) hence the Sections(1).

Put it all together:

Selection.InsertBreak Type:=wdSectionBreakNextPage
With Selection.PageSetup
 .Orientation = wdOrientLandscape
 .PageWidth = CentimetersToPoints(42)
 .PageHeight = CentimetersToPoints(29.7)
End With
Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete

This code will insert a new section+page break, set this new section to landscape A3, and remove the headers from it.

Note: You might need to unlink the headers before deleting it:

selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious=False

Hope this helps.




回答2:


Here is the working code in case someone else also find it useful:

    'Add drawing
    If wordDrawingExist Then
        Set wb = Documents.Open(wFile)
        Selection.WholeStory
        Selection.Copy
        Documents(docLogSkjema).Activate
        Selection.EndKey Unit:=wdStory
        Selection.InsertBreak Type:=wdSectionBreakNextPage
        With Selection.PageSetup
         .Orientation = wdOrientLandscape
         .PageWidth = CentimetersToPoints(42)
         .PageHeight = CentimetersToPoints(29.7)
        End With
        Selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
        Selection.Sections(1).Headers(wdHeaderFooterPrimary).Range.Delete
        Selection.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
        Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range.Delete
        Selection.Paste
        wb.Close False
    End If


来源:https://stackoverflow.com/questions/35741762/insert-a-new-page-in-word-without-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!