+ Reply to Thread
Results 1 to 5 of 5

macro error

  1. #1
    Anthony
    Guest

    macro error

    Hi all,
    I have this code which was very kindly posted in respone to a previous
    question.
    Basicaly I wanted a macro to print a 'word' documnet straight from excel.

    Sub LP_Tags()

    'Dim WDApp As Word.Application
    'Dim WDDoc As Word.Document
    Dim WDApp As Object
    Dim WDDoc As Object
    Dim myDocName As String
    Dim WordWasRunning As Boolean
    Dim testStr As String

    myDocName = "s:\lost property master sheets\sheet3.doc"

    testStr = ""
    On Error Resume Next
    testStr = Dir(myDocName)
    On Error GoTo 0
    If testStr = "" Then
    MsgBox "Word file not found!"
    Exit Sub
    End If

    WordWasRunning = True
    On Error Resume Next
    Set WDApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
    Set WDApp = CreateObject("Word.Application")
    WordWasRunning = False
    End If

    WDApp.Visible = True 'at least for testing!

    Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    WDDoc.PrintOut '.printPreview while testing???
    WDDoc.Close savechanges:=False

    If WordWasRunning Then
    'leave it running
    Else
    WDApp.Quit
    End If

    Set WDDoc = Nothing
    Set WDApp = Nothing

    End Sub



    I have two questions
    1) Can this code be any simpler or shorter ??
    2) when I use this code to produce the macro the word document is opened,
    but before it prints I get this error
    "Word is currently Printing.Quitting will cancle all pending jobs.Do you
    want to quit ? yes/no"

    if I select no then the documnet is printed. Why is this happening? is there
    an error in the code??

    Thanks for your help

    Anthony

  2. #2
    Trevor Shuttleworth
    Guest

    Re: macro error

    Anthony

    1) not really
    2) it's not an error. It's just that the code is too quick and is trying to
    close Word before it has finished preparing and printing the document. You
    could probably get away with putting a short pause in using OnTime

    Regards

    Trevor


    "Anthony" <[email protected]> wrote in message
    news:[email protected]...
    > Hi all,
    > I have this code which was very kindly posted in respone to a previous
    > question.
    > Basicaly I wanted a macro to print a 'word' documnet straight from excel.
    >
    > Sub LP_Tags()
    >
    > 'Dim WDApp As Word.Application
    > 'Dim WDDoc As Word.Document
    > Dim WDApp As Object
    > Dim WDDoc As Object
    > Dim myDocName As String
    > Dim WordWasRunning As Boolean
    > Dim testStr As String
    >
    > myDocName = "s:\lost property master sheets\sheet3.doc"
    >
    > testStr = ""
    > On Error Resume Next
    > testStr = Dir(myDocName)
    > On Error GoTo 0
    > If testStr = "" Then
    > MsgBox "Word file not found!"
    > Exit Sub
    > End If
    >
    > WordWasRunning = True
    > On Error Resume Next
    > Set WDApp = GetObject(, "Word.Application")
    > If Err.Number <> 0 Then
    > Set WDApp = CreateObject("Word.Application")
    > WordWasRunning = False
    > End If
    >
    > WDApp.Visible = True 'at least for testing!
    >
    > Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    > WDDoc.PrintOut '.printPreview while testing???
    > WDDoc.Close savechanges:=False
    >
    > If WordWasRunning Then
    > 'leave it running
    > Else
    > WDApp.Quit
    > End If
    >
    > Set WDDoc = Nothing
    > Set WDApp = Nothing
    >
    > End Sub
    >
    >
    >
    > I have two questions
    > 1) Can this code be any simpler or shorter ??
    > 2) when I use this code to produce the macro the word document is opened,
    > but before it prints I get this error
    > "Word is currently Printing.Quitting will cancle all pending jobs.Do you
    > want to quit ? yes/no"
    >
    > if I select no then the documnet is printed. Why is this happening? is
    > there
    > an error in the code??
    >
    > Thanks for your help
    >
    > Anthony




  3. #3
    Anthony
    Guest

    Re: macro error

    Trevor,

    Thanks ur reply,
    sorry to sound dumb but how do I insert this 'Ontime' ??
    thanks

    "Trevor Shuttleworth" wrote:

    > Anthony
    >
    > 1) not really
    > 2) it's not an error. It's just that the code is too quick and is trying to
    > close Word before it has finished preparing and printing the document. You
    > could probably get away with putting a short pause in using OnTime
    >
    > Regards
    >
    > Trevor
    >
    >
    > "Anthony" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi all,
    > > I have this code which was very kindly posted in respone to a previous
    > > question.
    > > Basicaly I wanted a macro to print a 'word' documnet straight from excel.
    > >
    > > Sub LP_Tags()
    > >
    > > 'Dim WDApp As Word.Application
    > > 'Dim WDDoc As Word.Document
    > > Dim WDApp As Object
    > > Dim WDDoc As Object
    > > Dim myDocName As String
    > > Dim WordWasRunning As Boolean
    > > Dim testStr As String
    > >
    > > myDocName = "s:\lost property master sheets\sheet3.doc"
    > >
    > > testStr = ""
    > > On Error Resume Next
    > > testStr = Dir(myDocName)
    > > On Error GoTo 0
    > > If testStr = "" Then
    > > MsgBox "Word file not found!"
    > > Exit Sub
    > > End If
    > >
    > > WordWasRunning = True
    > > On Error Resume Next
    > > Set WDApp = GetObject(, "Word.Application")
    > > If Err.Number <> 0 Then
    > > Set WDApp = CreateObject("Word.Application")
    > > WordWasRunning = False
    > > End If
    > >
    > > WDApp.Visible = True 'at least for testing!
    > >
    > > Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    > > WDDoc.PrintOut '.printPreview while testing???
    > > WDDoc.Close savechanges:=False
    > >
    > > If WordWasRunning Then
    > > 'leave it running
    > > Else
    > > WDApp.Quit
    > > End If
    > >
    > > Set WDDoc = Nothing
    > > Set WDApp = Nothing
    > >
    > > End Sub
    > >
    > >
    > >
    > > I have two questions
    > > 1) Can this code be any simpler or shorter ??
    > > 2) when I use this code to produce the macro the word document is opened,
    > > but before it prints I get this error
    > > "Word is currently Printing.Quitting will cancle all pending jobs.Do you
    > > want to quit ? yes/no"
    > >
    > > if I select no then the documnet is printed. Why is this happening? is
    > > there
    > > an error in the code??
    > >
    > > Thanks for your help
    > >
    > > Anthony

    >
    >
    >


  4. #4
    Bob Phillips
    Guest

    Re: macro error

    First, declare WDApp as a module level variable, not procedure.

    Change this bit of your code to shown.

    If WordWasRunning Then
    'leave it running
    Else
    Application OnTime Now + Timevalue(0,0,10),"StopWord"
    End If

    Set WDDoc = Nothing
    then add another procedure in the same module

    Sub StopWord()
    WDApp.Quit
    Set WDApp = Nothing
    End Sub

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Anthony" <[email protected]> wrote in message
    news:[email protected]...
    > Trevor,
    >
    > Thanks ur reply,
    > sorry to sound dumb but how do I insert this 'Ontime' ??
    > thanks
    >
    > "Trevor Shuttleworth" wrote:
    >
    > > Anthony
    > >
    > > 1) not really
    > > 2) it's not an error. It's just that the code is too quick and is

    trying to
    > > close Word before it has finished preparing and printing the document.

    You
    > > could probably get away with putting a short pause in using OnTime
    > >
    > > Regards
    > >
    > > Trevor
    > >
    > >
    > > "Anthony" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > Hi all,
    > > > I have this code which was very kindly posted in respone to a previous
    > > > question.
    > > > Basicaly I wanted a macro to print a 'word' documnet straight from

    excel.
    > > >
    > > > Sub LP_Tags()
    > > >
    > > > 'Dim WDApp As Word.Application
    > > > 'Dim WDDoc As Word.Document
    > > > Dim WDApp As Object
    > > > Dim WDDoc As Object
    > > > Dim myDocName As String
    > > > Dim WordWasRunning As Boolean
    > > > Dim testStr As String
    > > >
    > > > myDocName = "s:\lost property master sheets\sheet3.doc"
    > > >
    > > > testStr = ""
    > > > On Error Resume Next
    > > > testStr = Dir(myDocName)
    > > > On Error GoTo 0
    > > > If testStr = "" Then
    > > > MsgBox "Word file not found!"
    > > > Exit Sub
    > > > End If
    > > >
    > > > WordWasRunning = True
    > > > On Error Resume Next
    > > > Set WDApp = GetObject(, "Word.Application")
    > > > If Err.Number <> 0 Then
    > > > Set WDApp = CreateObject("Word.Application")
    > > > WordWasRunning = False
    > > > End If
    > > >
    > > > WDApp.Visible = True 'at least for testing!
    > > >
    > > > Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    > > > WDDoc.PrintOut '.printPreview while testing???
    > > > WDDoc.Close savechanges:=False
    > > >
    > > > If WordWasRunning Then
    > > > 'leave it running
    > > > Else
    > > > WDApp.Quit
    > > > End If
    > > >
    > > > Set WDDoc = Nothing
    > > > Set WDApp = Nothing
    > > >
    > > > End Sub
    > > >
    > > >
    > > >
    > > > I have two questions
    > > > 1) Can this code be any simpler or shorter ??
    > > > 2) when I use this code to produce the macro the word document is

    opened,
    > > > but before it prints I get this error
    > > > "Word is currently Printing.Quitting will cancle all pending jobs.Do

    you
    > > > want to quit ? yes/no"
    > > >
    > > > if I select no then the documnet is printed. Why is this happening? is
    > > > there
    > > > an error in the code??
    > > >
    > > > Thanks for your help
    > > >
    > > > Anthony

    > >
    > >
    > >




  5. #5
    Dave Peterson
    Guest

    Re: macro error

    There's an option MSWord that allows background printing.

    I've turned this off -- it helps in macros like this, and in earlier versions of
    MSWord, it fixed some problems when you print a document that has lots of
    intense graphics. (I'm not sure if newer versions of MSWord have solved this
    graphics problem, though. I just toggle this setting off.)

    In MSWord:
    Tools|Options|Print tab|Uncheck Background Printing.

    ======
    I personally find the code very simple and short <vbg>.

    If you want you could remove the check to verify the existence of the file and
    just open that word document in its own instance of MSWord.

    Option Explicit
    Sub LP_Tags()

    Dim WDApp As Object
    Dim WDDoc As Object
    Dim myDocName As String

    myDocName = "s:\lost property master sheets\sheet3.doc"

    Set WDApp = CreateObject("Word.Application")
    WDApp.Visible = True 'at least for testing!

    Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    WDDoc.PrintOut '.printPreview while testing???
    WDDoc.Close savechanges:=False

    WDApp.Quit

    Set WDDoc = Nothing
    Set WDApp = Nothing

    End Sub

    You could remove/comment this line:
    WDApp.Visible = True 'at least for testing!

    I wouldn't do any of these changes--but I would change that background printing
    in MSWord.






    Anthony wrote:
    >
    > Hi all,
    > I have this code which was very kindly posted in respone to a previous
    > question.
    > Basicaly I wanted a macro to print a 'word' documnet straight from excel.
    >
    > Sub LP_Tags()
    >
    > 'Dim WDApp As Word.Application
    > 'Dim WDDoc As Word.Document
    > Dim WDApp As Object
    > Dim WDDoc As Object
    > Dim myDocName As String
    > Dim WordWasRunning As Boolean
    > Dim testStr As String
    >
    > myDocName = "s:\lost property master sheets\sheet3.doc"
    >
    > testStr = ""
    > On Error Resume Next
    > testStr = Dir(myDocName)
    > On Error GoTo 0
    > If testStr = "" Then
    > MsgBox "Word file not found!"
    > Exit Sub
    > End If
    >
    > WordWasRunning = True
    > On Error Resume Next
    > Set WDApp = GetObject(, "Word.Application")
    > If Err.Number <> 0 Then
    > Set WDApp = CreateObject("Word.Application")
    > WordWasRunning = False
    > End If
    >
    > WDApp.Visible = True 'at least for testing!
    >
    > Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    > WDDoc.PrintOut '.printPreview while testing???
    > WDDoc.Close savechanges:=False
    >
    > If WordWasRunning Then
    > 'leave it running
    > Else
    > WDApp.Quit
    > End If
    >
    > Set WDDoc = Nothing
    > Set WDApp = Nothing
    >
    > End Sub
    >
    > I have two questions
    > 1) Can this code be any simpler or shorter ??
    > 2) when I use this code to produce the macro the word document is opened,
    > but before it prints I get this error
    > "Word is currently Printing.Quitting will cancle all pending jobs.Do you
    > want to quit ? yes/no"
    >
    > if I select no then the documnet is printed. Why is this happening? is there
    > an error in the code??
    >
    > Thanks for your help
    >
    > Anthony


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1