Got a Macro to open a word document from Excel......but

PSelman

Board Regular
Joined
May 13, 2004
Messages
50
Can anyone suggest HOW to:

Make it just print the document instead of opening it?

or, just open THAT document, not two.

Thanks


Sub Compliments_Slips()
Dim inputCheck As Integer
inputCheck = MsgBox("Please select the PRINT option from MS Word to obtain the Compliment Slip's, then CLOSE down Word. Do NOT Save.", vbOKCancel)
If inputCheck = vbCancel Then
Exit Sub
End If
Shell "C:\Program Files\Microsoft Office\Office\WINWORD /automation", vbMaximizedFocus

AppActivate "Microsoft Word"
Set appword = GetObject(, "word.application")
With appword
.Documents.Add "C:\White Rose\Templates\WRS Compliments Slips.doc"
End With
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
PSelman

Try this

Code:
Sub PrintWordDoc()

    Dim wdApp As Object
    Dim wdDoc As Object
    Dim lInput As Long
    Dim sFname As String
    
    sFname = "C:\****\uno.doc"
    lInput = MsgBox("OK to print", vbOKCancel)
    
    If lInput = vbOK Then
    
        Set wdApp = CreateObject("Word.Application")
    
        Set wdDoc = wdApp.documents.Add(sFname)
        wdDoc.PrintOut
        Application.Wait Now + TimeValue("00:00:02")
        wdDoc.Close False
        wdApp.Quit
        
        Set wdDoc = Nothing
        Set wdApp = Nothing
    End If

End Sub
 
Upvote 0
An alternate to this which would apply to Printing anything
would be to use the Shellexercute API. This just simulates
right click > Print

Code:
Option Explicit

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_HIDE = 0
Private Const strFilePath As String = "C:\White Rose\Templates\WRS Compliments Slips.doc"

Sub WordPrint()
Dim retVal As Long
    
retVal = ShellExecute(0, "Print", strFilePath, 0, 0, SW_HIDE)

If retVal < 32 Then
    '// there are Error codes for this..left out
    MsgBox "An Error occured...could not print"
End If

End Sub
 
Upvote 0
Could someone please tell me how to do the printing code for a powerpoint application? :hungry:

Yaro

Sub PrintWordDoc()

Dim wdApp As Object
Dim wdDoc As Object
Dim lInput As Long
Dim sFname As String

sFname = "E:\mypowerpointfile.ppt"
lInput = MsgBox("OK to print", vbOKCancel)

If lInput = vbOK Then

Set wdApp = CreateObject("Word.Application")

Set wdDoc = wdApp.documents.add(sFname)
wdDoc.PrintOut
Application.Wait Now + TimeValue("00:00:02")
wdDoc.Close False
wdApp.Quit

Set wdDoc = Nothing
Set wdApp = Nothing
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top