Results 1 to 10 of 10
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Files Open or Held (VB6)

    <P ID="edit" class=small>(Edited by D Willett on 12-Jun-04 11:52. Solution Found, see end of post)</P>Error 70 Permission denied
    After printing a series of documents which thanks to a previous post works OK.
    I have a delete command button for the user to remove the files just printed.
    Somewhere along the printing process, the document is still open or held (something like an ldb file) and prevents the docs from being deleted.
    The print code is :
    <pre>Private Sub cmdPrint_Click()
    Me.cmdAll.Enabled = False
    Me.cmdDelete.Enabled = False
    Me.cmdNone.Enabled = False
    Me.cmdSendFiles.Enabled = False
    Me.cmdPrint.Enabled = False

    Dim N As Integer
    Dim strPath As String
    Dim oWord As Object
    Dim oDoc As Object

    strPath = "C:MM-Utilities"
    Set oWord = CreateObject("Word.Application")

    For N = 0 To lstDocs.ListCount - 1
    If lstDocs.Selected(N) Then
    Me.txtInfo.Text = "Printing " & lstDocs.List(N) & " Please Wait !!"

    If oWord Is Nothing Then
    MsgBox "Word Could Not Be Started.", vbExclamation, "Word Error"
    Exit Sub
    On Error GoTo ErrHandler
    End If

    Set oDoc = oWord.Documents.Add(strPath & lstDocs.List(N))
    oWord.Visible = False
    oDoc.printout True
    oDoc.Close True
    End If
    Next N

    ExitHandler:
    On Error Resume Next
    oDoc.Close False
    Set oDoc = Nothing
    oWord.quit False
    Set oWord = Nothing
    oWord.quit True
    Me.txtInfo.Text = ""
    Me.cmdAll.Enabled = True
    Me.cmdDelete.Enabled = True
    Me.cmdNone.Enabled = True
    Me.cmdSendFiles.Enabled = True
    Me.cmdPrint.Enabled = True
    Unload Me
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub</pre>


    And the delete code is:

    <pre>Private Sub cmdDelete_Click()
    Dim fso As New Scripting.FileSystemObject

    Dim N As Integer
    Dim strPath As String

    strPath = "C:MM-Utilities"

    Set fso = New FileSystemObject
    For N = 0 To lstDocs.ListCount - 1
    If lstDocs.Selected(N) Then
    fso.DeleteFile strPath & lstDocs.List(N)
    End If
    Next N

    Set fso = Nothing
    Me.lstDocs.Clear
    Call Command1_Click

    End Sub</pre>


    Am I closing the doc and Word correctly ?

    ************************************************** *******************
    <font color=blue>Solution Found in Exit Handler, also changed On Error Resume Next.:

    <pre>ExitHandler:
    On Error GoTo ErrHandler
    Set oDoc = Nothing
    oWord.quit False
    Set oWord = Nothing
    Me.txtInfo.Text = ""
    Me.cmdAll.Enabled = True
    Me.cmdDelete.Enabled = True
    Me.cmdNone.Enabled = True
    Me.cmdSendFiles.Enabled = True
    Me.cmdPrint.Enabled = True
    Unload Me
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub</pre>

    </font color=blue>

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

    Re: Files Open or Held (VB6)

    Dave,

    Using On Error GoTo ErrHandler in the ExitHandler section is dangerous. If one of the statements in the ExitHandler section causes an error, you may get stuck in an endless loop

    ExitHandler > error occurs > ErrorHandler > Resume ExitHandler > ExitHandler > error occurs > ErrorHandler > ...

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Files Open or Held (VB6)

    Thanks for the extra info Hans

    I'll make adjustments accordingly.

  4. #4
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Files Open or Held (VB6)

    Hans
    Just a quick observation here.

    The above code works great with more than one item selected from the list.
    Where only one is selected, the doc sometimes doesn't print until another is selected and the code envoked again.
    Can you see something untoward in the code.
    I used debug.print to see what was happening and also temporarily commented Resume Next, no errors though !

    I don't think I need to use Printer.EndDoc because a printer isn't declared either by a common dialog or any other source.
    Would I be better using a CD with cd.ShowPrinter etc ?

    If so, what would be the most efficient way to use the CD without throwing it into the loop.
    I tried that yesterday and the CD showed for each item in the list, but if I put it outside of the loop, the CD would show regardless of whether an item was selected or not !

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

    Re: Files Open or Held (VB6)

    Dave,

    I don't know why printing would wait. As an experiment, change <code>oDoc.PrintOut True</code> to <code>oDoc.PrintOut False</code> to see if that makes a difference. It means that the code will pause until the document has been printed, instead of continuing.

    Don't bother with the Printer object or common dialogs or something like that. You are automating Word to print the documents, not the VB6 Printer object, and the common dialog has nothing to do with Word.

  6. #6
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Files Open or Held (VB6)

    Hans
    That works a treat !!

    Whats the difference between

    <pre>.Printout <font color=blue>True</font color=blue></pre>


    &

    <pre>.Printout <font color=blue>False</font color=blue></pre>


    I would have thought the latter would prevent printing ? <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

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

    Re: Files Open or Held (VB6)

    The first argument to PrintOut determines whether printing takes place in the background or not. If the argument is True, the print job is relegated to the backgorund, and the code continues on its merry way. Perhaps in your case, the background print job got too low priority. If the argument is False, the code waits patiently until the print job has finished, and only then continues; in other words, the print job gets high prioirity.

  8. #8
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Files Open or Held (VB6)

    Well thats brilliant.

    This now gives me a brief print dialog also just before printing.

    On another machine with Office97, why does word open and prompt to save the doc after it is printed ?

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

    Re: Files Open or Held (VB6)

    There are several situations in which Word will prompt to save a document, for example if the document contains fields that automatically update themselves, such as a field displaying the current date or time. In a previous thread, you mentioned problems with pag emargins; perhaps Word 97 changes the margins, and thus considers the document changed. But if you close the document with False as first argument (oDoc.Close False), you shouldn't get a prompt.

  10. #10
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Files Open or Held (VB6)

    Hans

    You saved my bacon.
    I was just going out of the door to update the head office machines when I stumbled across the problem.


    Thanks for the help. <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

Posting Permissions

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