Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code Help Needed Please! 1

Status
Not open for further replies.

adscrims

Programmer
Aug 21, 2001
24
GB
I have a command button on my form. At present it takes me to a word document and also some of the fields on my form are merged with this document. Does anybody know how to get the document to print then save itself in a pre defined folder as the documents heading eg"Ref No.12332" and then close down and reopen my database. All this must be a result of the click of this button only.
Thanks in advance Andy.
ps please help you dont know how much trouble you would be getting me out of alot of trouble.s-)
 
Code:
Private Sub cmdCalProc_Click()
'This sub opens, prints and saves a document. It also populates a bookmark based
'on a text box in your form
'Dim objects and variables
Dim DBS

Dim RST
Dim objWord As Object
Dim File
File = Me.txtCal_Proc
Set DBS = CurrentDb
'Grab the Document name
File = "Your Path and File"
'Launch word
Set objWord = New Word.Application
With objWord
'Open the file
.Documents.Open FileName:=Chr(34) & File & Chr(34)
'Make it visible
.Visible = True
'Populate a bookmark
.ActiveDocument.Bookmarks.Item("Item").Range.Text = " " & me.txtItem 'this is a text box
 .ActiveWindow.PrintOut Range:=wdPrintFromTo, From:="1", To:="3", print pages one to three
.ActiveDocument.SaveAs FileName:="MyDoc.doc", FileFormat:=wdFormat
End With
'Maximize the window
objWord.Application.WindowState = wdWindowStateMaximize
End Sub
Tyrone Lumley
augerinn@gte.net
 
OK. I fixed it up a little to work specifically for your application. You'll need a command button called cmdPrint. Post this code on the click event of cndPrint. You're word document will need one bookmark, titled Ref. This will automatically be populated by the text box Assessment Refference Number (Note, I would change that name to txtRef). The document is printed, and saved in the \departments\\IT\Work\Work\Templates Assessment Forms\ directory as whatever value is stored in the [Assessment Refference Number] text box.


Code:
Private Sub cmdPrint_Click()
'This sub opens, prints and saves a document. It also populates a bookmark based on a text box in your form
'Dim objects and variables
Dim objWord As Object
Dim strFile as string
Dim strSaveAsFile as string
'Grab the Document name
strFile = "UNIVERSAL5\departments\\IT\Work\Work\Templates Assessment Forms\Temp For Workplace risk Assessment.doc"
'Launch word
Set objWord = New Word.Application
With objWord
    'Open the file
    .Documents.Open FileName:=Chr(34) & File & Chr(34)
    'Make it visible
    .Visible = True
    'Populate a bookmark
    .ActiveDocument.Bookmarks.Item("Ref").Range.Text = " " & me.[Assessment Refference Number]  'this is a text box
    .ActiveDocument.PrintOut 
     strSaveAsFile = chr(34) & "UNIVERSAL5\departments\\IT\Work\Work\Templates Assessment Forms\" & me.[Assessment Refference Number] & chr(34)
    .ActiveDocument.SaveAs FileName:=strSaveAsFile, FileFormat:=wdFormat
   .quit
End With
Set objWord = Nothing
End Sub
Tyrone Lumley
augerinn@gte.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top