Saving File with name from in document

sid2107

New Member
Joined
Jun 23, 2016
Messages
1
Hi there,

I'm looking to take a document, and save each page as a separate Word doc. I lifted the code from Word's website to split the document, so that's fine. However, I'd like to name each page based on a textbox within the document, and I'm not sure how to do that. Is it easier to:

(a) copy text from a textbox (and how do i copy that text) and into the string
(b) "find" the text (what I'm looking for will always be in format "___ Price") and copy into the string
(c) copy the text from the header and into a string

The code in question in bolded below. Appreciate your help!!
Cheers,


-----------

Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage


For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy


' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the page, if any.
Selection.TypeBackspace
ChangeFileOpenDirectory "C:"
DocNum = DocNum + 1

Dim Price As String
price = ActiveDocument.Shapes("textbox1").Text
ActiveDocument.SaveAs FileName:=& price & ".doc"


ActiveDocument.Close


' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Actually, I just noticed you already did the below after I posted the code. At any rate, maybe this will be helpful.

Try the code below (modify folder name as necessary).


Code:
Sub BreakOnPage()

    Dim i As Integer
    Dim DocNum As Integer
    
    'Save document first prior to adding bookmarks.
    ActiveDocument.Save
    
    'Used to set criteria for moving through the document by page.
    Application.Browser.Target = wdBrowsePage

    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
      
        'Select and copy the text to the clipboard.
        ActiveDocument.Bookmarks("\page").Range.Copy

        'Open new document to paste the content of the clipboard into.
        Documents.Add
        Selection.Paste
        
        'Removes the break that is copied at the end of the page, if any.
        Selection.TypeBackspace
        ChangeFileOpenDirectory "U:\My Documents\"
        DocNum = DocNum + 1
        ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
        ActiveDocument.Close

        'Move the selection to the next page in the document.
        Application.Browser.Next
    Next i
    
    'Do not want to save document with bookmarks in it.
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,462
Messages
6,130,781
Members
449,591
Latest member
sharmavishnu413

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top