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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print a word document to file

Status
Not open for further replies.

rcraig12

Technical User
Jan 22, 2003
40
GB
Hi guys,

This one is breaking me up. Can I or can I not tell word via vbscript to print to file. I want the prn file to go to the next wrapper code section I have written to convert to pdf using Ghostscripts ps2pdf.

I can convert no bother to PDF but I can't get the printer to print to file.

Any thoughts would be greatly appreciated
 
Hello rcraig12,

The printout method under document has the parameter to set print to file.
[tt]Sub PrintOut([Background], [Append], [Range], [OutputFileName], [From], [To], [Item], [Copies], [Pages], [PageType], [PrintToFile], [Collate], [FileName], [ActivePrinterMacGX], [ManualDuplexPrint], [PrintZoomColumn], [PrintZoomRow], [PrintZoomPaperWidth], [PrintZoomPaperHeight][/tt]
For instance.
Code:
docfilespec="d:\test\abc.doc"
prnfilespec="d:\test\def.prn"

set owd=createobject("word.application")
set odoc=owd.documents.open(docfilespec)
owd.options.printbackground=false
odoc.printout ,,,prnfilespec
odoc.close : set odoc=nothing
owd.quit : set owd=nothing
(Do even need to bother the [printtofile] parameter, normally.)
If you need to convert a word doc to pdf, you can print directly to pdf using pdf995 with omniformat. Check out the free download link out there.

regards - tsuji
 
Thanks for that. I had eventually worked it out by analysing the word object model within word.

Didn't know you didn't need to set the PrintToFile flag though.

Cheers for the advice

regards - Robert


I will finish the full script and publish it here for others soon.

thanks again
 
Heres the full script

Code:
'###############################################################################
'  
'  Description: prn to pdf wrapper script using Ghostscript
'               Version Ghostscript 8.13
' 
'
'  NB: You need to add a two entries to the PATH environment variable via the 
'      My Computer Icon
'
'  They are C:\gs\gs8.13\bin & C:\gs\gs8.13\lib
' 
'###############################################################################

' Constants

  Const WdPrintAllDocument = 0
  Const WdDoNotSaveChanges = 0

' Variables 

  Dim strResult, strPath, intT
    
  ' Main Code
  
  strResult = DocToPrn ("Test.Doc", "Test.Prn")
  strResult = PrnToPDF ("test.prn", "test.pdf")
   
  WScript.Quit(0) ' Script ends here
  
  Public Function DocToPrn( strDocFile, strPRNFile )

  Dim objFSO 
  Dim objWord 
  Dim objWordDoc 
  Dim objWordDocs 
  Dim strPrevPrinter 
  
  
  Set objWSH = CreateObject("WSCript.Shell")
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objWord = CreateObject("Word.Application")
  Set objWordDocs = objWord.Documents
  
  strPath = objWSH.CurrentDirectory
  
  bShowDebug = True
  
  strTempFile = strPath & "\" & strPRNFile

  strDocFile = objFSO.GetAbsolutePathName(strDocFile)

  strFolder = objFSO.GetParentFolderName(strDocFile)

  If Len(strPRNFile)=0 Then
    strPRNFile = objFSO.GetBaseName(strDocFile) + ".pdf"
  End If

  If Len(objFSO.GetParentFolderName(strPRNFile))=0 Then
    strPRNFile = sFolder + "\" + strPRNFile
  End If

  ' Remember current active printer
  strPrevPrinter = objWord.ActivePrinter

  objWord.ActivePrinter = "PSPrinter" ' Name of printer for ps printed output.

  ' Open the Word document
  
  Set objWordDoc = objWordDocs.Open(strDocFile)

  
  objWord.ActiveDocument.PrintOut False , , , strTempFile , , , , , , , True

  objWordDoc.Close WdDoNotSaveChanges
  objWord.ActivePrinter = strPrevPrinter
  objWord.Quit WdDoNotSaveChanges
  
  Set objWord = Nothing
  Set objFSO = Nothing

End Function
  
  Public Function PrnToPDF (strFilename, strPDFFilename)

  ' Create a shell object

  Set objWSH = CreateObject("WSCript.Shell")
   
  strCmd = "cmd /c C:\gs\gs8.13\lib\ps2pdf "  & strFilename &  " " & strPDFFilename
  
  WScript.Echo strCmd
  
  Set objRun = objWSH.Exec(strCMD)
  
  strCount = 0
  
  Do While objRun.Status = 0 
    
    For intT = 1 to 50 
     WScript.StdOut.Write Chr(8)
    Next 
    WScript.StdOut.Write "Create Mode: " & strPDFFilename & " = "
    WScript.StdOut.Write strCount & " Cycles to create"
    strCount = strCount + 1
  Loop
 
  End Function

Any feedback is welcomed as I am going to expand on this.

regards

Robert
 
I guess you should replace this:
strPRNFile = objFSO.GetBaseName(strDocFile) + ".pdf"
By this:
strPRNFile = objFSO.GetBaseName(strDocFile) + ".prn"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top