Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help to divide file into 2 pages each. 1

Status
Not open for further replies.
Oct 11, 2007
44
US
I have a huge file of 50 pages. I want to divide into 25 files of 2 pages each. The code i have copies one page at a time. How can i select 2 pages at a time?


On MS Word:
Alt+F11
Copy and Run the code
Select the file with at least 4 pages long to divide and save.
File is saved at C:\




See Code Below:



'Macro recorded/Edited 10/07/07

Sub FileOpenTest()

Dim FileToOpen
Dim FileToSave


Dim szFileName As String
Dim wrdDoc As Document
'''This is just to demonstrate the below library function.
szFileName = FileOpenCustom1("", "*.Doc")


'''In reality it would be more productive to pass a document object back.
If szFileName <> "" Then
Set wrdDoc = Word.Documents(szFileName)

' disabled
' MsgBox wrdDoc.Path + Chr$(13) + wrdDoc.Name


FileToOpen = wrdDoc.Name


'Modified oct 6 2007
'Parse out .txt and replace with .doc
FileToSave = Left(wrdDoc.Name, Len(wrdDoc.Name) - 4) & ".doc"


Dim varDocOne As Variant


Documents.Open FileName:=FileToOpen, ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto
Selection.WholeStory

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

'Select and copy the text to the clipboard

ActiveDocument.Bookmarks("\page").Range.Copy


With ActiveDocument
.Bookmarks("\page").Range.Copy

End With

' 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.
ChangeFileOpenDirectory "C:\"
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
ActiveDocument.Close savechanges:=wdDoNotSaveChanges

'Message displayed on completing the macro
MsgBox "Files have been saved at the specified location."

End If
End Sub

'''
''' Function: FileOpenCustom
'''

Function FileOpenCustom1(pszInitDir As String, pszFilter As String) As String
Dim dlgFileOpen As Word.Dialog
Dim szCurDir As String
Dim szdlgFullName As String
Dim wrdDoc As Word.Document

'''Store the current document path
szCurDir = Application.Options.DefaultFilePath(wdDocumentsPath)
'''If pszInitDir is empty use the default path.




If pszInitDir <> "" Then
ChDir pszInitDir
Else
pszInitDir = szCurDir
End If
'''Initailize and reference the dialog object.
Set dlgFileOpen = Word.Dialogs(wdDialogFileOpen)
With dlgFileOpen
'''Update the dialog object.
.Update

'''Set the filter
.Name = pszFilter
'''Display and execute the dialog.
If .Show() <> False Then
'''If the user didn't cancel...
'''The active document is the one just opened.
Set wrdDoc = ActiveDocument
'''Cheat and use the document object to do the parsing.
'''Return the name of the document.
FileOpenCustom1 = wrdDoc.Name

End If
End With

'''Restore the default path setting.
With Application.Options
If .DefaultFilePath(wdDocumentsPath) <> szCurDir Then
.DefaultFilePath(wdDocumentsPath) = szCurDir
End If
End With
End Function



 
What does the Word document contain? Is it plain text, or formatted text?

Ed Metcalfe.

Please do not feed the trolls.....
 
The document has combination of plain and formatted text on the same page.
 
While it may not be significant,
Code:
Application.Options.DefaultFilePath
does not give an perfectly accurate return on the path of the document. Plus, you don't need that long a statement.
Code:
Dim szCurDir As String
Dim strDocPath As String

szCurDir = Application.Options.DefaultFilePath(wdDocumentsPath)
strDocPath = ActiveDocument.Path

MsgBox szCurDir & vbCrLf & strDocPath
will display - for example -

c:\myfiles\test\vb-test - Option.DefaultFilePath
C:\MyFiles\Test\VB-Test - ActiveDocument.Path

Notice, that Application.Options.DefaultFilePath(wdDocumentsPath) is both longer AND is not case-sensitive.

As an aside, you can get the full path and name by using FullName.
Code:
ActiveDocument.FullName
for my example file returns:

C:\MyFiles\Test\VB-Test\Notes05.doc

As a possible alternative to all that:
Code:
Sub TwoPages()
Dim j As Long
Dim r As Range
Dim ThisDoc As Document
Dim Thatdoc As Document
Dim var

j = 1
Set ThisDoc = ActiveDocument
Set r = ThisDoc.Range(0, 0)
For var = 1 To ThisDoc.Range _
   .Information(wdNumberOfPagesInDocument) / 2

   Set Thatdoc = Documents.Add
      With r
         .MoveEndUntil Cset:=Chr(12)
         .MoveEnd Unit:=wdCharacter, Count:=1
         .MoveEndUntil Cset:=Chr(12)
         .Copy
      End With
      
      Selection.Paste
      Thatdoc.SaveAs _
         FileName:="c:\test\" & j & "-" & j + 1 & _
            " pages.doc"
      Thatdoc.Close
      r.Collapse Direction:=wdCollapseEnd
      r.Move Unit:=wdCharacter, Count:=1
      j = j + 2
Next
End Sub
This will chunk off two pages at a time, making "1-2 pages.doc", "3-4 pages.doc", "5-6 pages.doc", "7-8 pages.doc" etc.

Notice the range keeps moving through the original document - and without ever actually going back and making it Active again - looking for page breaks. It does this with Cset, finding a page break, moving forward one character (into the next page), then finding the next page break. Thus....two pages worth. The range is copied and Selection.Paste.

Selection.Paste can be used because:

Selection applies to the ActiveDocument and THAT is the new Documents.Add. This is then SavedAs and closed. The loop collapses the range (in the original), makes new Document, does its thing with moving the range over the next two pages...copies and pastes.

The range ( r ) can still be moved in the original document independently.

As it stand, it loops for the NumberOfPages / 2. That is, if there is an odd number of pages, the last page will NOT be made into a new document. This could certainly be adjusted.

faq219-2884

Gerry
My paintings and sculpture
 
I completely replaced my code with the above. And the RUN Button does not do anything at all. Nothing happens when i click Run button. Code looks good. Any thoughts?
 
My Code:

On running, a pop up came and i could select the file to divide and save. Nothing happens of clicking Run when i completely replaced the code with the one provided.
Thanks
 
I am new to VBA, so it may be my ignorance. Please help.

Thanks
 
The code , i was able to run. But, it saves sometimes 6 pages, sometimes 5, sometimes 3, sometimes 2 in separate files. Can anyone help.
 
Hard to say. The code operates on the Active Document. The key to the answer is how your pages are broken. Do you have hard page breaks? Sometimes hard page breaks, sometimes not?

faq219-2884

Gerry
My paintings and sculpture
 
Hi fumei

i was able to run the code. how do i change your code to save one page at a time. I think that should resolve my problem. My document shall be saved ok after that.

Thanks


CODE:

Sub TwoPages()
Dim j As Long
Dim r As Range
Dim ThisDoc As Document
Dim Thatdoc As Document
Dim var

j = 1
Set ThisDoc = ActiveDocument
Set r = ThisDoc.Range(0, 0)
For var = 1 To ThisDoc.Range _
.Information(wdNumberOfPagesInDocument) / 2

Set Thatdoc = Documents.Add
With r
.MoveEndUntil Cset:=Chr(12)
.MoveEnd Unit:=wdCharacter, Count:=1
.MoveEndUntil Cset:=Chr(12)
.Copy
End With

Selection.Paste
Thatdoc.SaveAs _
FileName:="c:\test\" & j & "-" & j + 1 & _
" pages.doc"
Thatdoc.Close
r.Collapse Direction:=wdCollapseEnd
r.Move Unit:=wdCharacter, Count:=1
j = j + 2
Next
End Sub
 
1. Get rid of the divide by two. That will iterate through the number of pages.
2. Use just one Cset search for Chr(12)

BTW: please use the code TGML tags when posting code.

Thanks.

faq219-2884

Gerry
My paintings and sculpture
 
Hi Fumei

Thanks for all your help. My code is running perfectly fine the way I wanted. I changed your code a little bit to select the file externally.

I have one more question. When I run the macro, the font size changes from 10 to 12 in the destination documents. How do I keep the same format and font as the source document that I am trying to divide and save?

Thanks once again. Let me know.

 
Hi Buddy

Is there a one or 2 line code that you could help with ?

Required as mentioned before:

When I run the macro, the font size changes from 10 to 12 in the destination documents. How do I keep the same format and font as the source document that I am trying to divide and save?

Thanks for all your help.
 
Hi Buddy

Could you please help with the code, if it is not too much trouble for you??

And i am trying to learn and write code on styles on my end as well meanwhile.

Requirement as mentioned before:
When I run the macro, the font size changes from 10 to 12 in the destination documents. How do I keep the same format and font as the source document that I am trying to divide and save?

Thanks for all your help.
 
I answered you. Use Styles. Learn about Word itself. There is a reason it is doing that.

I would also suggest you look at the FAQ on this site regarding appreciation.

The format issue you have is NOT an issue with code. It is an issue with how Word works. That is....Styles. You could do that with code if you want, but frankly, you need to describe your situation better. Are you using a template, or styles, now?

I am willing to bet your source document has been manually formatted - no Styles. Your new document is going to use Normal.dot (unless...ahem, you use another proper template), in which case your new docs will have the format of your Normal.dot. Normal.dot has a default font size of 12.

And there you go.

faq219-2884

Gerry
My paintings and sculpture
 
hi Buddy

Thanks for letting me know about the FAQ appreciation.

You wrote "The format issue you have is NOT an issue with code. It is an issue with how Word works. That is....Styles. You could do that with code if you want, but frankly, you need to describe your situation better. Are you using a template, or styles, now?

I am willing to bet your source document has been manually formatted - no Styles. Your new document is going to use Normal.dot (unless...ahem, you use another proper template), in which case your new docs will have the format of your Normal.dot. Normal.dot has a default font size of 12."



You are absolutely write that is what is happening. From Manually formatted to normal.dot.

Could i change this via code to copy the same format as source to destination documents on divided files ??? If so, what should i do ?

Sorry, if i am bothering you....and its my ignorance on styles.

Thanks
 
As I suggested. Learn how Word works. It is all very nice to be tossing VBA code around, doing this and doing that, but if you do not have a basic grounding in how the application (Word) actually works, shrug, what can I say? I am not here to teach you basic Word.

The proper way is to make the destination documents have the same format, by using Styles.

Look.

1. New documents use Normal.dot, unless other wise told to use a different template.

2. Normal.dot has default font size of 12.

3. You have manual formatted documents.

4. Copying content from one place to another will use the STYLES of the destination document.

Solution?

A) do not use manual format. Use Styles.

B) make a new template formatted the way you want, and make your new documents use THAT one, not Normal.

C) change Normal.dot.

Of the above, C is the most poor use of Word, but it the easiest and most commonly done. Just remember if you do that, ALL documents created from Normal will have those changes.

You also - again, a VERY poor use of Word - could change the format of your new document "manually", but manually by code.

It is very easy to do.

Make the whole (new) document font size = 10.
Code:
  Selection.Paste
     ThatDoc.Range.Font.Size = 10
     Thatdoc.SaveAs _
         FileName:="c:\test\" & j & "-" & j + 1 & _
            " pages.doc"
      Thatdoc.Close
This is BAD use of Word. BAD BAD BAD.

However, it would work. OR....sigh, you could also use:
Code:
Selection.PasteAndFormat (wdFormatOriginalFormatting)

My suggestion though....learn Word. Learn how to use templates and styles. If you are going to be using Word, in the long run actually knowing the application is...well it is a good idea.

#1 rule: do NOT manually format anything.
#2 rule: do NOT use Normal.dot

I have written thousands of Word documents, some hundreds and hundreds of pages long. NONE of them use Normal.dot, and NONE of them have any manual formatting.

Oh, I know we are all busy, and we do not have time to learn everything. However, I strongly suggest you learn some real basics of Word. You will be happier for it.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top