help! on saving files macro

B

brad b

Hi, Here's what I have and what I need to do with it

I have several thousand pages of stuff in one document,
and each page begins with raw text , for example:

Date : 6/19/04 Time: 8:30
File : adsigyag3

What I need to do is separate each page into its own
document, instead of all the documents being congested
into one huge document.

So I am trying to create a macro that copies the text of
one page and saves it in a new document named whatever it
specifies (in this case adsigyag3) and then moves to the
next page to do the same. But the problem is, because I
use copy/paste to get the filename (copy the text
adsigyag3, and paste it into the "save as" field), it
works only for that page, and not the other thousand
pages. I noticed the visual basic editor explicitly
states the filename as ActiveDocument.SaveAs
FileName:="adsigyag3.doc". I don't want this, I want it
to save it to whatever is on the clipboard. I found out
the clipboard's contents are stored in wdPasteDefault,
but I don't know how to implement this. Could anyone help
me on this?

And furthermore, multiple consecutive pages have the same
filename specified, and I would like to put all of those
pages in the same document. Since my macro copies one
page and tries to save it, the next page would be
overwritten by the 2nd page with the same filename. Is
there a way I can make the macro do something like this:

copy filename (well not literally its filename).
store filename in a variable?
compare the variable to the pages after it (for matching
text).
if it finds another occurrence it can copy that page too.
if not, it just saves the one page.

Any help would be greatly appreciated.
 
D

Doug Robbins - Word MVP

This has not been tested, but I think it will do what you want

' splitter Macro
' Macro created 16-08-98 by Doug Robbins to save each page of a document
' modified 20-06-2004 for Brad B aka Fat Turtle
'
Dim Pages As Long, Counter As Long, DocName As String, DocName2 As String
Dim Source As Document, target As Document, Flag As Boolean
Set Source = ActiveDocument
Selection.HomeKey Unit:=wdStory
Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
Counter = 0
While Counter < Pages
Flag = True
Counter = Counter + 1
DocName = Source.Paragraphs(2).Range.Text
DocName = Mid(DocName, 7)
Set target = Documents.Add
target.Range.FormattedText =
Source.Bookmarks("\page").Range.FormattedText
Source.Bookmarks("\Page").Range.Cut
While Flag = True
DocName1 = Source.Paragraphs(2).Range.Text
If Mid(DocName1, 7) = DocName Then
Counter = Counter + 1
target.Range.FormattedText.Insert
AfterSource.Bookmarks("\page").Range.FormattedText
Else
Flag = False
End If
Wend
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

--
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
 
B

brad b

Awesome job, I see how your code works.

However, for some reason, when I attempt to run it I get a 'Run-Time Error 5941': The request member of the collection does not exist.

Then it highlights the line "target.Range.FormattedText = Source.Bookmarks("\page").Range.FormattedText"

I don't see how it is causing errors to occur. I'm a little dumbfounded as to how I should avert this problem.
 
D

Doug Robbins - Word MVP

Hi Brad,

I don't have time to go into this now, but I think it might be something to
do with trying to use the "\page" bookmark when the document is not visible.
Here is the original macro that I modified that might help you sort it out:

' splitter Macro

' Macro created 16-08-98 by Doug Robbins to save each page of a document

' as a separate file with the name Page#.DOC

'

Selection.HomeKey Unit:=wdStory

Pages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)

Counter = 0

While Counter < Pages

Counter = Counter + 1

DocName = "Page" & Format(Counter)

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

Documents.Add

Selection.Paste

ActiveDocument.SaveAs FileName:=DocName, FileFormat:= _

wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _

True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _

False, SaveNativePictureFormat:=False, SaveFormsData:=False, _

SaveAsAOCELetter:=False

ActiveWindow.Close

Wend


--
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
brad b said:
Awesome job, I see how your code works.

However, for some reason, when I attempt to run it I get a 'Run-Time Error
5941': The request member of the collection does not exist.
Then it highlights the line "target.Range.FormattedText = Source.Bookmarks("\page").Range.FormattedText"

I don't see how it is causing errors to occur. I'm a little dumbfounded as
to how I should avert this problem.
 

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