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
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:emMp3KoG...@TK2MSFTNGP11.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:eJoSK8oG...@tk2msftngp13.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:eMB5Nd0...@tk2msftngp13.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:#dYp401G...@TK2MSFTNGP10.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:%23onAhd6...@TK2MSFTNGP11.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:O5t2508G...@TK2MSFTNGP10.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:uV0HX7AH...@TK2MSFTNGP11.phx.gbl...
The following lines appears in Red in VBE
>> Dim Pages As Long, docname As String, docnamerange As Range, Source As
Document, 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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:urb7NZFH...@tk2msftngp13.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:%23X%23O8PHH...@tk2msftngp13.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:uR2e1AI...@TK2MSFTNGP12.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:eYQo3VX...@TK2MSFTNGP10.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:eYQo3VX...@TK2MSFTNGP10.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:ObXc$YcHEH...@TK2MSFTNGP12.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:%23Bk7Drf...@TK2MSFTNGP09.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:#ymQbxgH...@TK2MSFTNGP10.phx.gbl...
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
"Rashid Khan" <prkh...@hotmail.com> wrote in message
news:uzULozkH...@tk2msftngp13.phx.gbl...
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
"Doug Robbins - Word MVP" <d...@NOmvpsSPAM.org> wrote in message
news:echDGqrH...@TK2MSFTNGP11.phx.gbl...