[RESOLVED] Best method to append filename
I have a program that produces custom rtf files. These files are emailed and a copy is sent to a folder called "Emailed Documents"
The fileName is in this format:
Customer Lastname Customer Address Document Type ("Proposal").rtf or
Doe 1256 Oak Ave Invoice.rtf This works good so far. Now I want to expand on this and allow another "Invoice" to be sent and not overwrite the original, and have copies of both or maybe even 3 or more. So for the second Invoice it would be:
Doe 1256 Oak Ave Invoice[2].rtf
3rd invoice:
Doe 1256 Oak Ave Invoice[3].rtf
etc
This name appending would have to be done before emailing as to not send duplicate attachements. heres where i need help.
Pull all the files into a listbox and code against that or an array or ?
Then to determine how to search and append the file name. Split ?
Right ? Mid? or ?
Re: Best method to append filename
Code:
Dim strFile As String
Dim strNewFileName As String
Dim intFileNbr As Integer
intFileNbr = 2
strNewFileName = "Doe 1256 Oak Ave Invoice.rtf" ' This needs the path prepended to it
strFile = Dir$(strNewFileName)
Do While Len(strFile)
DoEvents
strFile = Dir$
strNewFileName = strNewFileName & "[" & intFileNbr & "]"
intFileNbr = intFileNbr + 1
Loop
MsgBox strNewFileName
Re: Best method to append filename
I think a simple loop with the Dir$() function can do the trick.
Note, you should ensure that your file name does not include invalid filename characters
Code:
Private Sub Command1_Click()
Dim myFile As String
myFile = GetUniqueFileName("C:\Windows\Temp", "Doe 1256 Oak Ave Invoice.rtf")
Open myFile For Output As #1
Close #1
End Sub
Private Function GetUniqueFileName(ByVal Path As String, ByVal FileNameBase As String) As String
Dim uniqueID As Long
Dim sFile As String
Dim sExt As Long, iExt As Integer
If Right$(Path, 1) <> "\" Then Path = Path & "\"
sFile = Dir$(Path & FileNameBase, vbArchive)
If sFile = vbNullString Then
GetUniqueFileName = Path & FileNameBase
Else
iExt = InStrRev(FileNameBase, ".")
If iExt = 0 Then
FileNameBase = FileNameBase & "[#]"
Else
FileNameBase = Left$(FileNameBase, iExt - 1) & "[#]" & Mid$(FileNameBase, iExt)
End If
Do
uniqueID = uniqueID + 1
sFile = Dir$(Path & Replace$(FileNameBase, "#", uniqueID), vbArchive)
Loop Until sFile = vbNullString
GetUniqueFileName = Path & Replace$(FileNameBase, "#", uniqueID)
End If
End Function
Re: Best method to append filename
I usually use this:
Code:
Option Explicit
Private Sub Command1_Click()
Dim myFile As String
myFile = UniqueFullFileName("C:\Windows\Temp", "Doe 1256 Oak Ave Invoice", ".rtf")
Debug.Print myFile
Open myFile For Output As #1
Close #1
End Sub
Function UniqueFullFileName(sPath As String, sBaseName As String, sExt As String) As String
Dim i As Long
'-- Don't care, sPath may or may not ending with "\"
UniqueFullFileName = sPath & "\" & sBaseName & sExt
Do While Len(Dir$(UniqueFullFileName, 55))
'-- 55 = vbArchive Or vbDirectory Or vbHidden Or vbReadOnly Or vbSystem
i = i + 1
UniqueFullFileName = sPath & "\" & sBaseName & "[" & i & "]" & sExt
Loop
End Function
Re: Best method to append filename
Thanks guys thats easier than i thought. With a little modifaction they will work as i am passing the whole path with filename. Martin a small thing the .rtf was not at the end as person recieving the document can just click it and be opened with Word or WordPad
Re: Best method to append filename
Quote:
Originally Posted by isnoend07
... Martin a small thing the .rtf was not at the end....
Oh, yes, you're right. Sorry about that.