How to print an external file?

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

Hi,

use automation to print the relevent document based on the extention of
the document.

dim xwordobj As Word.Application
xwordobj.Documents.Open ("c:\myfile.doc")
xwordobj.ActiveDocument.PrintOut (False)
Do While xwordobj.BackgroundPrintingStatus > 0
DoEvents
Loop
xwordobj.Application.Quit (True)
Set xwordobj = Nothing

you will have to set a reference to the word type library in your
access database.

Excel uses a similar system to print, and adobe has a type library as
well.

Hope it helps

Regards

Alex
 
Sorry missed a bit


Set xwordobj = CreateObject("Word.Application")
after the dim xwordobj as Word.Application line

Alex
 
Unfortunately, there isn't one approach that's guaranteed to work for all
files, since each file uses a different program to print it, and each
program can require different instructions to make it work.
 
Probably better to use Late Binding, so that you don't need to set a
reference. In that way, it won't matter what version of Word they have on
their machine (as long as the version has the same methods):

dim xwordobj As Object
Set xwordobj = CreateObject("Word.Application")
xwordobj.Documents.Open ("c:\myfile.doc")
xwordobj.ActiveDocument.PrintOut (False)
Do While xwordobj.BackgroundPrintingStatus > 0
DoEvents
Loop
xwordobj.Application.Quit (True)
Set xwordobj = Nothing

FWIW, if you are using Early Binding (so that you do have a reference set),
I'd recommend using

Set xwordobj = New Word.Application

rather than

Set xwordobj = CreateObject("Word.Application")
 
It would be great if I could at least find the approach to print a .pdf
file. Is there any source to read and learn about this feature?
Million thanks
 
Hi Doug,

On the point of references yes totally agree, using the
createobject/late binding method is much better when there is the
chance of multiple versions of the called apps e.g. word, nothing worse
than those compile errors because of the wrong versions of word/excel,
also with the late binding you can wrap your code in an error handler
much easer than early binding.

Regards

Alex
 
Hi,
Just in case you haven't found a sample yet, here's a sub that should print any document:

Public Sub PrintThisFile(hWnd As Long, FileName As String)
Dim X As Long
X = ShellExecute(FormName, "Print", FileName, vbNullString, 0&, SW_SHOWNORMAL)
End SubYou can call it from a form like:PrintThisFile Me.hWnd, "C:\somedoc.pdf"Also, put this code in the Declarations section of a
standard module:Public Const SW_SHOWNORMAL = 1Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal
hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal
nShowCmd As Long) As Long
I have not tested this out!!
 
Shell Chr(34) & "FullPathToAcrobat.exe" & Chr(34) & " /p /h " & _
Chr(34) & "FullPathToPDFFile" & Chr(34), vbMinimizedFocus

will print to the default printer. Remove the "/h " if you want to present
the Print Dialog to the user.
 
Hi
I just don't know how to thank you for your great help. And I hope you don't
mind if I add one more question. I followed your instructions and it worked
perfectly. I just didn't understand what hWind was. To find out what it does
I omitted it from your code and used it this way:

-------------------------------------------------------------------------------
Public Sub PrintThisFile(FileName As String)
Dim X As Long
X = ShellExecute(FormName, "Print", FileName, vbNullString, 0&,
SW_SHOWNORMAL)
end sub

PrintThisFile "C:\somedoc.pdf"
--------------------------------------------------------------------------------
Still the code prints the file without any difference.


Thank you very much for any kind of explanation

with regards
Ghalamkari
 
Thank you for your sample code. I was able to print a pdf file using your
suggested sample.

I appreciate your help.
 
Hi Alex and million thanks for your solution. Now I'm able to print all the
file types I was looking for. I appreciate your time.


with Regards
 
Any kind of help/hint is much appreciated for solving this problem.

I have a table in a access database which contains the name of different
parts of a huge machine and the drawings that concern that mechanical part.
I'm trying to print some .pdf - .xls and .doc files when a user clicks
"Print" button on a search result form. All files are saved to specific
folders in a drive.

here is an example:
the user searches for this part : ABCDE and a list of all the drawings and
documents of this part is shown. Now I'm trying to print ABCDE.pdf and
ABCDE.xls and ABCDE.doc by clicking a button.

Thanks for any possible advice.
 
I'm so sorry. I didn't intend to stay on the top. It seems I have a problem
with my time settings.
 
Back
Top