create new document from existing

C

Chris Joyce

Problem ,
123 documents , each document has 30-60 pages , each page has a ID (
equipment ID ) number on it ( equipment reports ) and only 1 report for each
ID per document.
In all there will be 5-7 pages ( different report types ) for each ID (530
ID in total )

I would like to collect all 5-11 pages for each ID and create a new document
for each and print it .
I have all the ID's in a MS access DB , I have been able to open and find ID
and print the page , but when working through all 75 documents it seems to
crash .
I have not been able to select pages into a new document yet .

Any hints ?

Just a shame I've been given the problam at the wrong end and was unable to
collect data in a better way :-(

Chris
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Chris Joyce > écrivait :
In this message, < Chris Joyce > wrote:

|| Problem ,
|| 123 documents , each document has 30-60 pages , each page has a ID (
|| equipment ID ) number on it ( equipment reports ) and only 1 report for
each
|| ID per document.
|| In all there will be 5-7 pages ( different report types ) for each ID
(530
|| ID in total )
||
|| I would like to collect all 5-11 pages for each ID and create a new
document
|| for each and print it .
|| I have all the ID's in a MS access DB , I have been able to open and find
ID
|| and print the page , but when working through all 75 documents it seems
to
|| crash .
|| I have not been able to select pages into a new document yet .
||
|| Any hints ?
||
|| Just a shame I've been given the problam at the wrong end and was unable
to
|| collect data in a better way :-(
||

Posting the relevant code would be most helpful.
Also, debug your code and tell us on which line it crashes.
Word version would also be nice.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chris Joyce

The error I get is 'Object variable or With block variable not set'
it seems to be a problam with the 'With Selection.Find' .
I have hard coded the document name and text to find, but I'm sure that not
part of the problam .

I have not been able to select the page the resault if found on and copy it
to paste it into a new document.

The doucment opens and the find works most of the time , it seems to fail on
large documents ( word not ready in time ? )

I wonder if it might be better to do the hole thing from MSWord not MSAccess
?

Chris


'' Cut from MSAccess
Dim wdApp As Word.Application
Set wdApp = New Word.Application
wdApp.Visible = True

' Set document manualy for debug
' wdApp.Documents.Open FileName:=CurrentProject.Path & "\" &
Me.DocList.value & ".doc", ReadOnly:=True

wdApp.Documents.Open FileName:=CurrentProject.Path & "\" &
"Section1_Review1.doc", ReadOnly:=True
wdApp.Documents(1).Activate

' use static ID for debug
' .Text = "ID:" & Me.ToolID.Value

wdApp.Selection.Find.ClearFormatting
With Selection.Find
.Text = "ID:62676"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wdApp.Selection.Find.Execute

wdApp.Application.PrintOut FileName:="", Range:=wdPrintCurrentPage,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True,
PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0,
_
PrintZoomPaperHeight:=0
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Chris Joyce > écrivait :
In this message, < Chris Joyce > wrote:

|| The error I get is 'Object variable or With block variable not set'
|| it seems to be a problam with the 'With Selection.Find' .
|| I have hard coded the document name and text to find, but I'm sure that
not
|| part of the problam .
||
|| I have not been able to select the page the resault if found on and copy
it
|| to paste it into a new document.
||
|| The doucment opens and the find works most of the time , it seems to fail
on
|| large documents ( word not ready in time ? )
||
|| I wonder if it might be better to do the hole thing from MSWord not
MSAccess
|| ?
||

As your code stands right now, it will not do the search and replace
correctly. Also, you will get a message when you close the document because
even though it is opened as read-only, changes were made, it will ask if you
want to save as...

Play around with the following code to see if it can help you get the
find/search working, if so, then write back for other problems related to
your page selection.

'_______________________________________
Dim wdApp As Word.Application
Dim wdDoc As Document

Set wdApp = New Word.Application
'Is it really necessary to make the doc visible?
' wdApp.Visible = True

Set wdDoc = wdApp.Documents.Open(FileName:="D:\Test.doc",
ReadOnly:=True)

With wdDoc.Range.Find
.Text = "Test"
.Replacement.Text = "New Test"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

wdDoc.Close wdDoNotSaveChanges
wdApp.Quit

Set wdDoc = Nothing
Set wdApp = Nothing
'_______________________________________


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chris Joyce

I Don't want to find and replace ,
but find , select page and copy to a new document ( or add to a exixting
one )

I'll play with it some more

Chris
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Chris Joyce > écrivait :
In this message, < Chris Joyce > wrote:

|| I Don't want to find and replace ,
|| but find , select page and copy to a new document ( or add to a exixting
|| one )
||

Sorry about that. In your code I saw that you had a
".Replacement.Text = "" "
line... So I thought that you anted to remove the found text...
OK. But you will still have to use my code, just remove the
.Replacement
line and the
"Replace:=wdReplaceAll"
switch on the
".Execute"
line. Your code could cause problem if you already have an active instance
of Word running.

Now, if you find a page, you have to do something like:

'_______________________________________
Dim DocRange As Range
Dim PageNum As Long
Dim PageRge As Range

Set DocRange = wdDoc.Range

PageNum = 0

With DocRange.Find
.Text = "Test"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute
DocRange.Select
'This is to make sure that we do not copy the same
'page many times if the searched text is found more
'than once on the same page
With Selection
If Not PageNum = .Information(wdActiveEndPageNumber) Then
PageNum = .Information(wdActiveEndPageNumber)
Set PageRge = .Bookmarks("\Page").Range
'Something like
'Newdoc.Range.InsertAfter PageRge.Text
'Where NewDoc is of course the destination document
End If
End With
Loop
End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Chris Joyce

It seems that I'm almost there,

It opens, finds and will copy the text of the document , save and close

only thing, it dropps the table and format of the original document

each 'Review' document is a different format , so I can't use a template , I
need to copy the layout and format as well

any pointers anyone ?

I had to make a few small changes so it runs from a form in MSAccess


Thanks in advanced

Chris


Private Sub Collect_Reports_Click()

Dim wdApp As Word.Application
Dim FindMe As String
Dim DocRange As Range
Dim PageNum As Long
Dim PageRge As Range

' will base on recordset when all working
FindMe = "SYS-652612"

Set wdApp = New Word.Application
wdApp.Visible = True

' cover.doc contains a few pages that cover review system
' need to add dynamic select of cover documents based on system ID
' will be part of recordset
wdApp.Documents.Open FileName:=CurrentProject.Path & "\" & "cover.doc"

' add start of loop to open and search many review doc's
' but test with one only till all ok
wdApp.Documents.Open FileName:=CurrentProject.Path & "\" & "Review 2.doc",
ReadOnly:=True
wdApp.Documents(1).Activate

Set DocRange = wdApp.ActiveDocument.Range

PageNum = 0

With DocRange.Find
.Text = FindMe
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute
DocRange.Select
'This is to make sure that we do not copy the same
'page many times if the searched text is found more
'than once on the same page
With Selection
If Not PageNum = DocRange.Information(wdActiveEndPageNumber)
Then
PageNum = DocRange.Information(wdActiveEndPageNumber)

Set PageRge = DocRange.Bookmarks("\Page").Range

'Documents(2) is the system's full report document
wdApp.Documents(2).Range.InsertAfter PageRge.Text
End If
End With
Loop
End With

' Close src Document
wdApp.Documents(1).Close

' end loop for multi reviews here

' Save then close
wdApp.Documents(1).SaveAs (CurrentProject.Path & "\" & FindMe)
wdApp.Documents(1).Close
wdApp.Quit
'_______________________________________

End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Chris Joyce > écrivait :
In this message, < Chris Joyce > wrote:

|| It seems that I'm almost there,
||
|| It opens, finds and will copy the text of the document , save and close
||
|| only thing, it dropps the table and format of the original document
||
|| each 'Review' document is a different format , so I can't use a template
, I
|| need to copy the layout and format as well
||
|| any pointers anyone ?
||
|| I had to make a few small changes so it runs from a form in MSAccess
||
||

Try something like:

PageRge.Copy

With wdApp.Documents(2).Range
.Collapse wdCollapseEnd
.Range.Paste
.InsertBreak wdSectionBreakNextPage 'or wdPageBreak
End With

and when you are done you will have to remove the last break, or live with
an extra blank page!

Sometimes, I find that it is easier to place the line that inserts the break
before the line that does the pasting. Then I check if the paste is the
first one (I use a boolean variable for that), if so I do not insert the
break. This way I do not have to go back to remove the last break.

But to each his own!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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