Use First Line as File Name and Save Each Page As a Seperate File

R

Rashid Khan

Hello Group,
I have seen and copied the following macro from the NG. It is suppose to
save each page as a seperate file using the first line of the page as file
name.

This macro works fine but it does not save the first line of my document as
a file name. My document has the first line as

1) SI-797-066-MOL-001_001

2) SI-797-066-MOL-001_002

3) SI-797-066-MOL-002_001

4) SI-797-066-MOL-002_002

............ so on so forth

The result I get after running the macro is
1) SI
2) SI1
3) SI2
4) SI3
....... so on

What am I missing?
The macro seems to take the first two characters only (eg. SI in this case)
and increment it.

What I need is the complete first line to be cut and saved with that name as
a word document file.

Any help and suggestions would be greatly appreciated.

R Khan

Dim i As Integer
Dim iNumPages As Integer
Dim sCurrentDoc As String

sCurrentDoc = ActiveDocument.Name
'Get the current document's name
iNumPages = ActiveDocument.ComputeStatistics(wdStatisticPages)
'Get the number of pages
For i = 1 To iNumPages
'For as many times as there are pages
Selection.GoTo What:=wdGoToPage, which:=wdGoToAbsolute, Count:=i
'Goto pagenumber i
Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
'Select this page
Selection.Copy
'Copy the selection
Documents.Add
'Add a new document
Selection.Paste
'Paste
ActiveDocument.SaveAs , FileFormat:=wdFormatText
'Save the new document using first line as file name
Selection.HomeKey Unit:=wdStory
' Go to Top of Page
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
' Highlight first line
Selection.Cut
' Delete first line
ActiveDocument.SaveAs , FileFormat:=wdFormatText
' Save File Again
ActiveDocument.Close
'Close this new document
Documents(sCurrentDoc).Activate
'Activate the document where we started from
Next i
End Sub
 
D

Doug Robbins - Word MVP

No where are you actually setting the filename to be the first line of the
document.

Use the following:

Dim Pages As Long, DocName As String, Source As Document, Target As
Document, SourceName as string
Set Source = ActiveDocument
SourceName = Source.FullName
Selection.HomeKey Unit:=wdStory
Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
counter = 0
While counter < Pages
counter = counter + 1
Source.Bookmarks("\Page").Range.Cut
Set Target = Documents.Add
Target.Range.Paste
DocName = Target.Bookmarks("\line").Range.Text
Target.Bookmarks("\line").Range.Delete
Target.SaveAs FileName:=DocName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
Target.Close
Wend
Source.Close wdDoNotSaveChanges
Documents.Open SourceName


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hi Doug,
The macro gives the following error:

Run-time error '5487':
Word cannot complete the save due to a file permission error.
(C:\...\SI-797-066-MOL-003_002.doc) and the following lines are highlighted

Target.SaveAs FileName:=DocName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

then if I stop and re run the macro it gives the following error:
Run-time error '4605'
"This method or property is not available because the object is empty"

When I press the debug button the following line is highlighted:

Source.Bookmarks("\Page").Range.Cut

What can be the problem? Please help me out.

Thanks

Rashid
 
D

Doug Robbins - Word MVP

Hi Rashid,

Can you manually save a document with the filename
C:\...\SI-797-066-MOL-003_002.doc? Where does the ...\ come from?

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hi Doug,
Yes I can manually save the file and C:\1st level\second level\third
level\<Filename> is the path where the file will be saved.

I also changed the File Location in the Option of MS Word to the path
mentioned above.. But it still gives an error.

I have typed the file name at the top of each and every page and I wish to
save each page with the name given at the top of each page. Hope I am clear
now.

Thanks

Rashid
 
D

Doug Robbins - Word MVP

Hi Rashid,

Try this version:

Dim Pages As Long, docname As String, docnamerange As Range, Source As
Document, Target As Document, SourceName As String
Set Source = ActiveDocument
SourceName = Source.FullName
Selection.HomeKey Unit:=wdStory
Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
counter = 0
While counter < Pages
counter = counter + 1
Source.Bookmarks("\Page").Range.Cut
Set Target = Documents.Add
Target.Range.Paste
Set docnamerange = Target.Range
docnamerange.Collapse wdCollapseStart
docname = docnamerange.Paragraphs(1).Range.Text
docnamerange.Paragraphs(1).Range.Delete
Target.SaveAs FileName:=docname, FileFormat:=wdFormatDocument,
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
Target.Close
Wend
Source.Close wdDoNotSaveChanges
Documents.Open SourceName


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hello Doug,
I am sorry the new version macro does not work as desired.
It still highlight the following line:
Target.SaveAs FileName:=docname, FileFormat:=wdFormatDocument,
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

The document is about 250+ pages with each
page having the first line as the filename
desired by me.

There is a page break between each page.

Please help me out

Rashid
 
D

Doug Robbins - Word MVP

Hi Rashid,

If you put

MsgBox docname

after the line

docname = docnamerange.Paragraphs(1).Range.Text

what appears in the message box.

I assume that none of the following appears in red font in the vbe

Target.SaveAs FileName:=docname, FileFormat:=wdFormatDocument,
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hi Doug,
I have added the MsgBox docname and it gives me the file name desired by me
but after that gives and error and highlights the
Target.Save..............till the end

The following lines appears in Red in VBEDocument, Target As Document, SourceName As String

and also this line:

Target.SaveAs FileName:=docname, FileFormat:=wdFormatDocument,
LockComments:=False, Password:="", AddToRecentFiles:=True,
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False,
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

I added an underscore ( _ ) to correct the red colour.. but still the macro
gives the filename in the MsgBox and highlights the following
Target.SaveAs FileName:=docname, .....till SaveAsAOCLetter=False

What am I missing?
Thanks for your time and help

Rashid
 
D

Doug Robbins - Word MVP

Hi Rashid,

Try just using

Target.SaveAs FileName:=docname

If that gives an error, there is some problem with the name that is being
used to save the document.


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hi Doug,
This also does not work. I tried to change the file name also.. but it
highlights the following line"

Target.SaveAs FileName:=docname

When I run the macro it shows me the file name in the Message Box and then
gives the following message:
Run-time error '5487'
Word cannot complete the save due to a file permission error and at the
bottom shows the path (C:\...\<filename>)

One more thing I noticed that it cuts the top page from the document and
shows the file name as Document1.... and so on.

Can this give u a clue?
The original macro which I submitted at first used to create the Files
separately but the names where S1, S2, S3 and so on.
Please help me out.
Rashid
 
R

Rashid Khan

Hi Doug,
One more thing I noticed, The macro runs if remove the hypens from the
filename
for eg
If I change the file name from
SI-797-066-MOL-002_001
SI-797-066-MOL-002_002
to
SI797066MOL002001
SI797066MOL002002

what is work around for this problem now.

I want the filenames with 'hypens' and other problem is that
matter of each physical page is divided into two pages in MS Word and
therefore the underscore in last of the filename denotes the 001 (first
page) and 002 (second page)

In other words for eg I have a 100 page document and each group of
2 page is to be named <filename>_001 and <filename>_002.

Can u suggest anything now?
Thanks a lot once again
Rashid
 
D

Doug Robbins - Word MVP

Hi Rashid,

The Document1 etc. doesn't mean anything as that is what would be displayed
in the top border until the file is saved.

I can use

ActiveDocument.SaveAs "SI-797-066-MOL-002_001"

to save a document.

What happens if you run a macro containing that command on another document.
Does it save OK or give you the same error message?
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hi Dough
1) The Document1 etc. doesn't mean anything as that is what would be
displayed in the top border until the file is saved.
Yes you are right.

2) ActiveDocument.SaveAs "SI-797-066-MOL-002_001"
Where to put this line.

3) What happens if you run a macro containing that command on another
document. Does it save OK or give you the same error message?
See my further posting. The macro runs if I remove the hyphens
from the filename!

Is there a command to see the macro in action Step-by-Step. Long
back in Lotus 1-2-3 it used to display each step the macro was executing. I
mean can I see the macro operating Step-by-Step on
the target file outside the VBE?

Rashid
 
D

Doug Robbins - Word MVP

Create a new macro containing the code. For example

Sub a()
ActiveDocument.SaveAs "SI-797-066-MOL-002_001"
End Sub

Then just run that macro by selecting it via Tools>Macro>Macros.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hello Doug,
I run the macro. It works fine but the
Filename is SI-797-066-MOL-002_001 for the complete document.

I need to split the whole document (about +200 pages) into
separate file of 200 pages. Each page should be named
as SI-797-066-MOL-002_001, SI-797-066-MOL-002_002
....... so on so forth.

Rashid
 
D

Doug Robbins - Word MVP

If you can save a document using that macro with that filename, then I don't
think that there is anything wrong with the macro that splits the document
and tries the save each page with a separate filename.

The error message that you are getting talks about "a file permission error"
and I believe that it must be some problem with the location to which you
are trying to save the document.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

Rashid Khan

Hi Doug,
Sorry I was out and therefore could not reply back.
I could not figure out the problem re "file permission error" (may be
something to do with my Windows XP).

However, as a work around I used the Find and Replace command of MS Word to
remove the 'hyphens' and "underscore" from the file name and run the macro
... and it runs fine.

Then I have used Magic File Renamer to get back the hyphens and the
underscore in the Filename.

Thanks a lot for all the time and help u have given to solve my problem.

Thank u very much.
Rashid
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top