inserting a word document into the current word doc thru code

T

Ted

Hi all, i build an application that opens up a word document and prints it
to the printer if the checkbox is selected. i'd like to instead insert the
document to the end of the document that is already open or copy the form
and paste it at the end. what ever makes more sense. can anyone point me in
the right directions?


For Each ctrl In Me![sfmStateForms].Form.Controls
If TypeOf ctrl Is CheckBox And ctrl.Tag = "Mandatory" Then
If ctrl.Value = True Then
Err.Number = 0
On Error GoTo notloaded2
Set objWord = GetObject(, "Word.Application")
notloaded2:
If Err.Number <> 0 Then
Set objWord = CreateObject("Word.Application")
End If
objWord.Visible = True
objWord.Documents.Open FileName:="L:\Glencoe\Forms\" &
Right(ctrl.Name, Len(ctrl.Name) - 3) & ".doc"
objWord.ActiveDocument.PrintOut
objWord.ActiveDocument.Close SaveChanges:=0
Set objWord = Nothing
PauseCode 'Pauses the code for 1 second to see if printouts will
come out in order
End If
End If
Next

TIA
Ted
 
D

Doug Robbins - Word MVP

Something like:

Dim TargetDoc As Document, SourceDoc As Document
Dim objWord As Word.Application
Dim Target As Range
For Each ctrl In Me![sfmStateForms].Form.Controls
If TypeOf ctrl Is CheckBox And ctrl.Tag = "Mandatory" Then
If ctrl.Value = True Then
Err.Number = 0
On Error GoTo notloaded2
Set objWord = GetObject(, "Word.Application")
notloaded2:
If Err.Number <> 0 Then
Set objWord = CreateObject("Word.Application")
End If
objWord.Visible = True
Set TargetDoc = objWord.ActiveDocument
Set SourceDoc =
objWord.Documents.Open(FileName:="L:\Glencoe\Forms\" & _
Right(ctrl.Name, Len(ctrl.Name) - 3) & ".doc")
Set Target = TargetDoc.Range
Target.Collapse wdCollapseEnd
Target.Range = SourceDoc.Range
End If
End If
Next


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Ted said:
btw that code was written in Access in case that wasn't clear

Ted said:
Hi all, i build an application that opens up a word document and prints
it to the printer if the checkbox is selected. i'd like to instead insert
the document to the end of the document that is already open or copy the
form and paste it at the end. what ever makes more sense. can anyone
point me in the right directions?


For Each ctrl In Me![sfmStateForms].Form.Controls
If TypeOf ctrl Is CheckBox And ctrl.Tag = "Mandatory" Then
If ctrl.Value = True Then
Err.Number = 0
On Error GoTo notloaded2
Set objWord = GetObject(, "Word.Application")
notloaded2:
If Err.Number <> 0 Then
Set objWord = CreateObject("Word.Application")
End If
objWord.Visible = True
objWord.Documents.Open FileName:="L:\Glencoe\Forms\" &
Right(ctrl.Name, Len(ctrl.Name) - 3) & ".doc"
objWord.ActiveDocument.PrintOut
objWord.ActiveDocument.Close SaveChanges:=0
Set objWord = Nothing
PauseCode 'Pauses the code for 1 second to see if printouts
will come out in order
End If
End If
Next

TIA
Ted
 
T

Ted

Thank you Doug! i'll give it a try

Doug Robbins - Word MVP said:
Something like:

Dim TargetDoc As Document, SourceDoc As Document
Dim objWord As Word.Application
Dim Target As Range
For Each ctrl In Me![sfmStateForms].Form.Controls
If TypeOf ctrl Is CheckBox And ctrl.Tag = "Mandatory" Then
If ctrl.Value = True Then
Err.Number = 0
On Error GoTo notloaded2
Set objWord = GetObject(, "Word.Application")
notloaded2:
If Err.Number <> 0 Then
Set objWord = CreateObject("Word.Application")
End If
objWord.Visible = True
Set TargetDoc = objWord.ActiveDocument
Set SourceDoc =
objWord.Documents.Open(FileName:="L:\Glencoe\Forms\" & _
Right(ctrl.Name, Len(ctrl.Name) - 3) & ".doc")
Set Target = TargetDoc.Range
Target.Collapse wdCollapseEnd
Target.Range = SourceDoc.Range
End If
End If
Next


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Ted said:
btw that code was written in Access in case that wasn't clear

Ted said:
Hi all, i build an application that opens up a word document and prints
it to the printer if the checkbox is selected. i'd like to instead
insert the document to the end of the document that is already open or
copy the form and paste it at the end. what ever makes more sense. can
anyone point me in the right directions?


For Each ctrl In Me![sfmStateForms].Form.Controls
If TypeOf ctrl Is CheckBox And ctrl.Tag = "Mandatory" Then
If ctrl.Value = True Then
Err.Number = 0
On Error GoTo notloaded2
Set objWord = GetObject(, "Word.Application")
notloaded2:
If Err.Number <> 0 Then
Set objWord = CreateObject("Word.Application")
End If
objWord.Visible = True
objWord.Documents.Open FileName:="L:\Glencoe\Forms\" &
Right(ctrl.Name, Len(ctrl.Name) - 3) & ".doc"
objWord.ActiveDocument.PrintOut
objWord.ActiveDocument.Close SaveChanges:=0
Set objWord = Nothing
PauseCode 'Pauses the code for 1 second to see if printouts
will come out in order
End If
End If
Next

TIA
Ted
 

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