Refrencing field in List Box

Rince

New member
Local time
Tomorrow, 07:00
Joined
Nov 8, 2007
Messages
6
Hi, I have what i believe to be a simple problem (for real programmers). So any help would be greatly appriciated. I have some code that will open a word document as follows:

Private Sub cmdOpenWord_Click()
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Set wordApp = New Word.Application
With wordApp
.Visible = True
Set wordDoc = .Documents.Open("G:\Computair\21584-rm-pack.doc", , False)
End With

End Sub

This works a treat but i would like to open a document based on a list box selection. My listbox is called lstQuotes and the field i want to reference is called OrderNo.

So in place of "21584" i would like to reference the number from my list box selection.

Also without pushing too can I create an if statement that would open the document "NoDrawing.doc" (same location) if the select numbers document does not exist.

Any help on this would be great.
Thanking you.

Sean
 
Try

Set wordDoc = .Documents.Open("G:\Computair\" & Me.ListboxName.Column(x) & "-rm-pack.doc", , False)

where x is the column containing the value. For the other, you can use the Dir() function to test if that's a valid path, and open the other doc as appropriate.
 
See if this works for you:

Code:
Private Sub cmdOpenWord_Click()
    
    On Error Resume Next
    
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    
    Set wordApp = New Word.Application
    
    With wordApp
        .Visible = True
        Set wordDoc = .Documents.Open("G:\Computair\" & CStr(Me.OrderNo) & "-rm-pack.doc", , False)
        
        'Check if document exists:
        If Err.Number = 5273 Or Err.Number = 5174 Then
            Set wordDoc = .Documents.Open("G:\Computair\NoDrawing.doc", , False)
            Exit Sub
            
        Else
            GoTo ErrorHandler
            
        End If
    
    End With
    
ErrorHandler:
    If Err.Number <> 0 Then
        MsgBox "Error: " & Err.Number & vbNewLine & Err.Description, vbCritical
        Exit Sub
    End If
End Sub
 
Thanks alot guys that code worked perfectly.

Sean
 

Users who are viewing this thread

Back
Top Bottom