PDA

View Full Version : Split word document into seperate files



James Niven
03-23-2016, 12:42 PM
Hi All,

I am looking for a little assistance here. I found the following code online that splits my word document into separate files, of one page. As you can see the file name for each document is "Test_1". What I want help with is the first row of each page has a line with a code. I want to use this as the file name when I convert all these pages as JPEG files. How do I change the code to do this?
Thanks



Sub BreakOnPage()
Dim I As Integer
Dim DocNum As Integer
Application.ScreenUpdating = False
' 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:\Test\"
DocNum = DocNum + 1

ActiveDocument.SaveAs FileName:="test_" & DocNum & ".jpeg"
ActiveDocument.Close
' Move the selection to the next page in the document.
Application.Browser.Next
Next I
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
Application.ScreenUpdating = True
End Sub

gmaxey
03-23-2016, 06:11 PM
I don't know what you mean by the first row but if it is the first paragraph then something like:

FileName:=ActiveDocument.Paragraphs(1).Range.Text & ".jpeg"

James Niven
03-24-2016, 09:32 AM
Hi Gmaxey,

Thank for the reply, much appreciated. Yes, I do mean the first row (Paragraph) of the document The first line on the page is like a code "A401 Test Page", this is what I want for the file name.
Where do you suggest I place this line or other code to change?

Thanks

gmaxey
03-24-2016, 11:07 AM
ActiveDocument.SaveAs FileName:= Left(ActiveDocument.Paragraphs(1).Range.Text, Len(ActiveDocument.Paragraphs(1).Range.Text) -1) & ".jpeg"

James Niven
03-24-2016, 07:00 PM
Greg,

Thanks, this works nicely and as wanted.
Thanks for your assistance with this!

gmaxey
03-24-2016, 07:12 PM
Ok. Glad to help.

James Niven
03-24-2016, 07:18 PM
Hi Greg,

Ok, I have a twist with the above that you helped me with.

The same word document has a paragraph of text, but below that this is an image which I want to save as a jpg file with the text above the image as the file name, what needs to be added to do this code to select the image and name?
I am finding the splitting of the word document into separate pages is not working for my needs, plus the file size is a little large. The end result is these images are to be displayed in an access database, but the images are stored in folders on my computer.

Thanks

gmaxey
03-24-2016, 07:35 PM
You are asking for something that isn't easily done with Word. See http://windowssecrets.com/forums/showthread.php/144880-Macro-needed-to-find-embedded-MS-Word-graphics-and-save-out-to-a-file-name