Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printing a textfile (XP)

    One of my macros outputs a summary of actions performed on the current word document to a textfile in a certain directory.

    What i want to do is to print this automatically, as at the moment i have that directory folder open and then rightclick-print the newly created file.

    I have done the following ...

    Sub temp()
    Dim d As String, o As Variant
    d = "c:temp1text1.txt"
    o = Shell("C:WINDOWSNOTEPAD.EXE " & d, 1)
    SendKeys "^p"
    End Sub

    ....but was wondering if there was a way to reference the textfile DIRECTLY as an object then call a print method? Any ideas?

    something like

    dim o as object
    set o = textfile.txt
    o.print
    set = nothing

    ? Cheers
    Thanks,

    pmatz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a textfile (XP)

    You can use the Windows API function ShellExecute to print a file.

    Put the following declarations at the top of the module:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Private 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

    Private Const SW_SHOWNORMAL As Long = 1

    <img src=/w3timages/blueline.gif width=33% height=2>

    The following function uses ShellExecute to print a file:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Function PrintFile(strFilename As String) As Boolean
    Dim lngResult As Long
    lngResult = ShellExecute(0, "Print", strFilename, 0&, 0&, SW_SHOWMAXIMIZED)
    If lngResult <= 32 Then
    MsgBox "Couldn't print the file.", vbExclamation
    Else
    Printfile = True
    End If
    End Function

    <img src=/w3timages/blueline.gif width=33% height=2>

    Call it in your code as follows:

    Dim blnResult As Boolean
    Dim d As String
    d = "c:temp1text1.txt"
    blnResult = PrintFile(d)

    If the file was printed, blnResult will be True.

  3. #3
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a textfile (XP)

    Thanks Hans, thats the kind of thing i was looking for. Im not that up on Windows API's (yet!!) but this function is great.

    I think that the 'SW_SHOWMAXIMIZED' in the function should be the SW_SHOWNORMAL defined at the top yes?

    If I then ( after printing the file) wanted to move the textfile ito another folder, in the same directory, eg 'Bin' then how would you recommend that? I know I can do it through DOS commands, so will have a look at that, but not sure if you can run those from VBA. Is there a "Move" call to the API?

    Cheers
    Thanks,

    pmatz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a textfile (XP)

    Sorry, use SW_SHOWNORMAL instead of SW_SHOWMAXIMIZED. I mixed two versions together without checking.

    The VBA instruction Name will move a file if the new name you specify contains a different path:

    Name "c:temp1text1.txt" As "c:temp1bintext1.txt"

  5. #5
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a textfile (XP)

    Thanks, why doesn't 'Name' show up in the object browser (even under VBA library) or in the Help?
    Thanks,

    pmatz

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a textfile (XP)

    Name is not in the Object browser because it is not associated to an object, i.e. it is not a method. It is a very "old" instruction that already existed in Basic before it became Visual Basic.

    It can be found in the online help for VBA, but - as with so many subjects - not easily.
    - Put the insertion point in an empty line in a module
    - Press F1.
    - If necessary, activate the Contents tab of the help window.
    - Expand Instructions, then M-Z.
    - Click on Name.

  7. #7
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a textfile (XP)

    <img src=/S/coffeetime.gif border=0 alt=coffeetime width=32 height=48> Thanks Hans, a whole heap of helpful help in there, you certainly have a thorough knowledge of VBA etc.

    I found that I had to use Name to move the file before i printed it, as i got an error saying the file could not be found to print if i did it the other way round! Thats fine though.

    Cheers <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    Thanks,

    pmatz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •