I'm working on making a macro that will allow users to input an invoice number in a cell (A1). The cell next to it (B1) contains a formula that returns the vendor that invoice came from.
With that, the purpose of the macro is to take the vendor name, use it as a string to locate the folder name, and then use the invoice number as a string to locate the specific file in the folder, and open it. The way our file system is set up is that we have an "invoice folder" which contains vendor folders. Each vendor folder contains invoices named by job date, vendor, and invoice number, so the file names look like this: 2014.04 - Vendor Name - 1234.pdf
Sub Invoice_Search()
'Searches N Drive for specified invoice and opens .pdf
Dim y As Variant, x As Variant, invnumlen As Integer, vendnamelen As Integer
Dim myShell As Object
Dim foldernamefull As String, filenamefull As String
Dim objFS As Variant
Dim objFolder As Variant
Dim objFile As Variant
Const Filepath = "N:\Storage\Projects\Billing\Billing Final\Invoices\"
y = Sheets("Sheet4").Range("A1")
x = Sheets("Sheet4").Range("B1")
invnumlen = Len(Sheets("Sheet4").Range("A1"))
vendnamelen = Len(Sheets("Sheet4").Range("B1"))
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFS.getfolder(Filepath)
For Each objFile In objFolder.Files
If Left(objFile.Name, vendnamelen) = x Then
foldernamefull = objFile.Name
Set myShell = CreateObject("WScript.Shell")
myShell.Run ("Filepath" & "foldernamefull")
Exit For
Else
End If
Next
End Sub
My code isn't complete - I don't have the second half to locate the specific file written, but this should be enough for you to see the logic of what I've coded so far. Since I'm fairly new to VBA, it's cobbled together from different "how-to"s i've found online, so parts may be redundant.
From what I have so far, i expected that the macro, when run, would open up the vendor folder. It's not working so far.
Can anyone help me out?
Bookmarks