Code not working as it should

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,280
Office Version
  1. 2007
Platform
  1. Windows
Morning,

I am using the code supplied below.
Currently with my userform open when the button OPEN INVOICE is pressed the code opens up the invoice which is saved on the pc by its invoice number which is shown in the userform invoice number field.

If N/A is in the field then i see the message box no problem.
If say 123 is shown in the field then invoice 123 is then opened & now seen no problem.

This is the issue i have come across, should 150 be in the invoice number field BUT the invoice 150 for some reason had been deleted from the pc upon pressing the OPEN INVOICE button nothing happens,it should at least advise me that no invoice was not found.

Many thanks



Code:
Private Sub OpenInvoice_Click()    If IsNumeric(txtInvoiceNumber.Value) Then
        CreateObject("Shell.Application").Open "C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES\" & txtInvoiceNumber.Value & ".pdf"
    Else
        MsgBox "Invoice N/A For This Customer"
    End If


End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Maybe this ;
Code:
Private Sub OpenInvoice_Click()

    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES\"

    If IsNumeric(txtInvoiceNumber.Value) And Len(Dir(FILE_PATH & txtInvoiceNumber.Value & ".pdf")) Then
        CreateObject("Shell.Application").Open (FILE_PATH & txtInvoiceNumber.Value & ".pdf")
    Else
        MsgBox "Invoice N/A For This Customer"
    End If


End Sub
 
Upvote 0
Many thanks frined it worked well.

Question for you please.
Is it possible to have 2 different messages ?

If N/A is shown in invoice number files show the current message "Invoice N/A For this Customer"

If an invoice number is shown but invoice had been deleted from Pc then show a different message of "Invoice Deleted From Pc"

Many thanks
Sorry it was an afterthought after my first post.
 
Upvote 0
Many thanks frined it worked well.

Question for you please.
Is it possible to have 2 different messages ?

If N/A is shown in invoice number files show the current message "Invoice N/A For this Customer"

If an invoice number is shown but invoice had been deleted from Pc then show a different message of "Invoice Deleted From Pc"

Many thanks
Sorry it was an afterthought after my first post.

No worries.

See if this works :
Code:
Private Sub OpenInvoice_Click()

    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES\"
    
    If txtInvoiceNumber = "N/A" Or Len(txtInvoiceNumber) = 0 Then
        MsgBox "Invoice N/A For This Customer"
    ElseIf Len(Dir(FILE_PATH & txtInvoiceNumber.Value & ".pdf")) = 0 Then
        MsgBox "Invoice Deleted From Pc"
    Else
        CreateObject("Shell.Application").Open (FILE_PATH & txtInvoiceNumber.Value & ".pdf")
    End If
End Sub
 
Upvote 0
Hi,
The following supplied code is in use at present.

Currently is just has an OK box of which closes once clicked.
I was looking for the option to have two custom boxes.

One box would be ok & to close just like the present.

The other box would read say Go To Folder etc,of which when clicked it would take you to the folder in question of which is C:\Users\Ian\Desktop\Invoices

I did not find anywhere these custom option boxes so could you advise what you would do in a situation like this please.


Code:
Private Sub OpenInvoice_Click()    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES\"
    
    If txtInvoiceNumber = "N/A" Or Len(txtInvoiceNumber) = 0 Then
        MsgBox "Invoice N/A For This Customer"
    ElseIf Len(Dir(FILE_PATH & txtInvoiceNumber.Value & ".pdf")) = 0 Then
        MsgBox "This Invoice Is Not In The Dr Copy Invoices Folder", vbCritical, "Warning Invoice Is Missing"
    Else
        CreateObject("Shell.Application").Open (FILE_PATH & txtInvoiceNumber.Value & ".pdf")
    End If


End Sub
 
Upvote 0
Hi,
The following supplied code is in use at present.

Currently is just has an OK box of which closes once clicked.
I was looking for the option to have two custom boxes.

One box would be ok & to close just like the present.

The other box would read say Go To Folder etc,of which when clicked it would take you to the folder in question of which is C:\Users\Ian\Desktop\Invoices

I did not find anywhere these custom option boxes so could you advise what you would do in a situation like this please.


Code:
Private Sub OpenInvoice_Click()    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES\"
    
    If txtInvoiceNumber = "N/A" Or Len(txtInvoiceNumber) = 0 Then
        MsgBox "Invoice N/A For This Customer"
    ElseIf Len(Dir(FILE_PATH & txtInvoiceNumber.Value & ".pdf")) = 0 Then
        MsgBox "This Invoice Is Not In The Dr Copy Invoices Folder", vbCritical, "Warning Invoice Is Missing"
    Else
        CreateObject("Shell.Application").Open (FILE_PATH & txtInvoiceNumber.Value & ".pdf")
    End If


End Sub

What happens after the user has been taken to the target folder ? Will the user select an invoice file ? and if so will he just open it ?

If the above is what you want then you could do something like this :

Add a second button and assign the following routine to it :
Code:
Sub OpenInvoicesFolder()

        Const FILE_PATH As String = "C:\Users\Ian\Desktop\Invoices\"
    
        CreateObject("Shell.Application").Open (FILE_PATH)
End Sub
 
Upvote 0
Morning,
I have been thinking of this and think we need to do it like this "but i wasnt sure how to change your code without breaking it"

So when an invoice number is not found we should see a msgbox with YES & NO

Title
Warning Invoice is Missing.

Text
Would You Like To Open The Folder ?

NO
Would just close the msgbox.

YES
Would open the folder path "C:\Users\Ian\Desktop\Invoices"

Once you are taken to the folder this would be for you to see what the issue is.
Then you take action from there.

 
Upvote 0
Morning,
I have been thinking of this and think we need to do it like this "but i wasnt sure how to change your code without breaking it"

So when an invoice number is not found we should see a msgbox with YES & NO

Title
Warning Invoice is Missing.

Text
Would You Like To Open The Folder ?

NO
Would just close the msgbox.

YES
Would open the folder path "C:\Users\Ian\Desktop\Invoices"

Once you are taken to the folder this would be for you to see what the issue is.
Then you take action from there.


Try this and see if it is what you want :
Code:
Private Sub OpenInvoice_Click()

    Const FILE_PATH As String = "C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES\"
    
    If txtInvoiceNumber = "N/A" Or Len(txtInvoiceNumber) = 0 Then
        MsgBox "Invoice N/A For This Customer"
    Else
        If Len(Dir(FILE_PATH & txtInvoiceNumber.Value & ".pdf")) = 0 Then
            If MsgBox("Would You Like To Open The Folder ?", vbCritical + vbYesNo, "Warning Invoice is Missing.") = vbYes Then
                CreateObject("Shell.Application").Open ([COLOR=#574123]"C:\Users\Ian\Desktop\Invoices\"[/COLOR])
            End If
        Else
            CreateObject("Shell.Application").Open (FILE_PATH & txtInvoiceNumber.Value & ".pdf")
        End If
    End If
End Sub

I have noticed that in your initial post the invoice folder was :
"C:\Users\Ian\Desktop\REMOTES ETC\DR COPY INVOICES"
Then the invoice folder changed to :
"C:\Users\Ian\Desktop\Invoices"

If this was just an omission on your part, you should be able to easily change the folder paths in the above code.

 
Upvote 0

Forum statistics

Threads
1,216,566
Messages
6,131,437
Members
449,652
Latest member
ylsteve

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