Saving Specific Pages in Word Doc using VBA

How do I save page number 1, 4, 10, 15 as a different word doc using VBA

Currently, i am saving the entire doc as a signal word doc

Code Used:

Application.DisplayAlerts = False
ActiveDocument.SaveAs2 FileName:="C:\Users\Vivek\Documents\savingworkbook\" & entityform.TextBox13.Value & "_" & Information_Form.sanctionedamount.Value & ".docx", Fileformat:=wdFormatXMLDocument
Application.DisplayAlerts = True
Vivek SethiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Try making a duplicate document, deleting the unwanted pages, and saving that:
Sub SaveDoc()
    Application.DisplayAlerts = False
    ActiveDocument.SaveAsByPage "C:\Users\Vivek\Documents\savingworkbook\" & entityform.TextBox13.Value & "_" & Information_Form.sanctionedamount.Value & ".docx", "1,4,10,15"
    Application.DisplayAlerts = True
End Sub


Sub SaveAsByPage(strNewDoc As String, strPagelist As String)
Dim docA As Document
Dim docB As Document
Dim SavePages() As String
Dim p As Integer
Dim q As Integer
Dim bDeletePage As Boolean

SavePages = Split(strPagelist, ",")
Set docA = ActiveDocument
Set docB = Documents.Add(docA.FullName)
For p = docA.Range.Information(wdActiveEndPageNumber) To 1 Step -1
    bDeletePage = True
    For q = 0 To UBound(SavePages)
        If p = Val(SavePages(q)) Then
            bDeletePage = False
            Exit For
        End If
    Next q
    If bDeletePage Then
        docB.GoTo wdGoToPage, , p
        docB.Bookmarks("\Page").Range.Delete
    End If
Next p
docB.SaveAs2 filename:=strNewDoc, FileFormat:=wdFormatXMLDocument
docB.Close wdDoNotSaveChanges
End Sub

Open in new window

0
Vivek SethiaAuthor Commented:
hi,

This does not work

please suggest something else

Thanks
0
GrahamSkanRetiredCommented:
What does happen?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Vivek SethiaAuthor Commented:
i get an error

"Object does not support property or method
0
Vivek SethiaAuthor Commented:
Found this code

can we use this with some slight modifications


Private Sub CommandButton1_Click()

    ' moves cursor to page 2
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
    ' selects content of page 2
    Selection.Extend
    Application.Browser.Next
    ' copies selected page
    Selection.Copy
    ' creates a new document and saves it
    Documents.Add DocumentType:=wdNewBlankDocument
    Selection.PasteAndFormat (wdPasteDefault)
    ActiveDocument.SaveAs FileName:="NewDoc.doc"
    ActiveWindow.Close
End Sub
0
GrahamSkanRetiredCommented:
That could be another way,

Which line does my code fail on?
0
Vivek SethiaAuthor Commented:
ActiveDocument.SaveAsByPage "C:\Users\Vivek..................................................
0
GrahamSkanRetiredCommented:
I didn't test that bit because I couldn't resolve all the bits in the file name. I forgot to remove the ActiveDocument. from the SaveAsByPage line. This is what the calling procedure should look like
Sub SaveDoc()
    Application.DisplayAlerts = False
    SaveAsByPage "C:\Users\Vivek\Documents\savingworkbook\" & entityform.TextBox13.Value & "_" & Information_Form.sanctionedamount.Value & ".docx", "1,4,10,15"
    Application.DisplayAlerts = True
End Sub

Open in new window

0
Vivek SethiaAuthor Commented:
i am sorry to say but the code does not run
0
GrahamSkanRetiredCommented:
Can you say what happens, please? If it stops on an error, can you say what the error message is and which line it fails on? Thank you.
0
Vivek SethiaAuthor Commented:
Compile Error,

Expected Sub, Function or Property

Line:
saveasbypage
0
GrahamSkanRetiredCommented:
That line should be:
Sub SaveAsByPage(strNewDoc As String, strPagelist As String)

Open in new window

0
Vivek SethiaAuthor Commented:
I've tried this and it works. I am able to save only Save page 1 and 3 of the doc separately

Private Sub CommandButton1_Click()

Documents.Add DocumentType:=wdNewBlankDocument
ActiveDocument.SaveAs FileName:="Page 1.doc"
        Windows("Document1").Activate
        ' moves cursor to page 1
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
        ' selects content of page 1
        Selection.Extend
        Application.Browser.Next
        ' copies selected page
        Selection.Copy
Documents("Page 1.doc").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
        Windows("Document1").Activate
        ' moves cursor to page 3
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
        ' selects content of page 3
        Selection.Extend
        Application.Browser.Next
        ' copies selected page
        Selection.Copy
Documents("Page 1.doc").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
ActiveDocument.Save
MsgBox "New Doc Created as File Name Page 1.doc"


Unload Me
End Sub
0
GrahamSkanRetiredCommented:
The point about creating a new document from the original is to ensure that any layout and other document-specific properties don't get changed.
0
GrahamSkanRetiredCommented:
Here is  what is basically your method, but made to loop through the list of pages.
Private Sub CommandButton1_Click()
    Dim docA As Document
    Dim docB As Document
    Dim Pages() As String
    Dim p As Integer
    
    Pages = Split("1,3", ",")
    Set docA = ActiveDocument
    
    Set docB = Documents.Add(DocumentType:=wdNewBlankDocument)
    docA.Activate
    For p = 0 To UBound(Pages)
        Selection.GoTo wdGoToPage, wdGoToAbsolute, Pages(p)
        docA.Bookmarks("\Page").Range.Copy 'copies whole page with the clipboard
        docB.Bookmarks("\EndOfDoc").Range.Paste
    Next p
    docB.SaveAs filename:="Page 1.doc" 'save new document
MsgBox "New Doc Created as File Name Page 1.doc"


Unload Me
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.