1.11M Members

Split .doc, with a parameterized name

 
0
 

Hi all,

I am a PHP programmer with no knowledge on VB, so I apologize for my intrusion :).

What I need is to split a .doc file with N pages into N different documents, using the first word of each page as the name of the documents. In php it's been kind of an impossible mission but I found this threat where it explains how to do it by means of a word macro

vbaexpress.com/kb/getarticle.php?kb_id=727

I found the lines where the macro gets the name of each generated document and saves it:

strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document

However I can't even do the (apparently) very little modification needed to change this so the file is saved with the first word of the page as its name.

Anybody would be so kind to help me with this?

Thanks in advance

 
0
 

You need to completely understand the .doc format in order to split a document properly. Do you understand the headers and all?

 
0
 

Walt, no need to understand the header and all, as this can be done with automation...

lamakan, all you need to do is to add a reference to word (project>references) and copy the code you referenced. However, you will need to add a few more variables so that you can open the document in question as the code you referenced is from within word...

Use your friends (yahoo, google, ask, answers, bing) and search for vb6 automating word, vb6 open word doc, or whatever else you can think of to find more examples...

Good Luck

 
0
 

Thanxs vb5prgrmr, I will take a look at vb6 automating word. In the meantime given the code below:

Option Explicit 'This goes in the Declarations section of your code module.
'Hopefully it is already there because you have ticked the 'Require Variable Declaration' _
checkbox. (Tools/Options, Editor tab.)


Sub SplitIntoPages()
Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iPageCount As Integer
Dim strNewFileName As String

Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
flicker a bit.
Set docMultiple = ActiveDocument 'Work on the active document _
(the one currently containing the Selection)
Set rngPage = docMultiple.Range 'instantiate the range object
iCurrentPage = 1
'get the document's page count
iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)
Do Until iCurrentPage > iPageCount
If iCurrentPage = iPageCount Then
rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
Else
'Find the beginning of the next page
'Must use the Selection object. The Range.Goto method will not work on a page
Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1
'Set the end of the range to the point between the pages
rngPage.End = Selection.Start
End If
rngPage.Copy 'copy the page into the Windows clipboard
Set docSingle = Documents.Add 'create a new document
docSingle.Range.Paste 'paste the clipboard contents to the new document
'remove any manual page break to prevent a second blank
docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
'build a new sequentially-numbered file name based on the original multi-paged file name and path
strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document
iCurrentPage = iCurrentPage + 1 'move to the next page
docSingle.Close 'close the new document
rngPage.Collapse wdCollapseEnd 'go to the next page
Loop 'go to the top of the do loop
Application.ScreenUpdating = True 'restore the screen updating

'Destroy the objects.
Set docMultiple = Nothing
Set docSingle = Nothing
Set rngPage = Nothing
End Sub

I guess there must be an easy way to get the first word of each page and assign it to strNewFileName. I have never worked with vb and probably will never do and, as most programmers, the lack of time is a common thing in my daily work. So anybody could give me a hand on this
and tell me how I can do it?

Thanks!

 
0
 

The answer is right there in your code... (when posting code please use the [ code]code goes here[ /code] tage (without the spaces))

rngPage.Copy 'copy the page into the [B]Windows clipboard[/B]

So all you need to do is to look up clipboard by typing it in and pressing F1. However, by using the first word of each page you may overwrite previous pages and/or may never be able to display the pages in the correct order once again. I would suggest that you use the original file name appended with "pg" & format(countervariable, "0000").

Good Luck

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article