Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to append ".txt" to a string

Status
Not open for further replies.

rk0000

Technical User
Mar 15, 2002
33
0
0
IN
I need to write data to a text file. I am able to open the save file dialog box. I need to append the .txt for file name if the user has not given the extension
the code i have is :
Dim lngFormHwnd As Long
Dim lngAppInstance As Long
Dim strInitDir As String
Dim strFileFilter As String
Dim strFileName As String
Dim lngResult As String
Dim strTitle As String
Dim mlngFileNum As Long
mlngFileNum = FreeFile
strTitle = "Save File"

strFileName = "" & String(256, 0)

lngResult = objDialog.ExportDialog(lngFormHwnd, lngAppInstance, strInitDir, strFileFilter, strFileName, strTitle)


Public Function ExportDialog(lngFormHwnd As Long, lngAppInstance As Long, strInitDir As String, strFileFilter As String, strFileName As String, strTitle As String) As String
Dim Ofn As OPENFILENAME
Dim txtPosition As Integer
With Ofn
.lStructSize = Len(Ofn)
.hwndOwner = lngFormHwnd
.hInstance = lngAppInstance
.lpstrFilter = strFileFilter
.nFilterIndex = 1
.lpstrFile = strFileName
.nMaxFile = Len(Ofn.lpstrFile) - 1
.lpstrFileTitle = Ofn.lpstrFile
.nMaxFileTitle = Ofn.nMaxFile
.lpstrInitialDir = strInitDir
.lpstrTitle = strTitle
.Flags = 0
End With


If GetSaveFileName(Ofn) = 0 Then
mstrFileName = "none"
mblnStatus = False
Else
mstrFileName = Trim(Ofn.lpstrFile)
mstrFileTitle = Trim(Ofn.lpstrFileTitle)
mblnStatus = True
End If

txtPosition = InStr(RTrim(LTrim(mstrFileName)), ".txt")

If txtPosition Then
ExportDialog = RTrim(LTrim(mstrFileName))
Else
ExportDialog = RTrim(LTrim(mstrFileName)) & ".txt"
End If

End Function

I am not able to append the .txt string at the end of mstrFileName
"C:\Documents and Settings\ramakris.k\My Documents\test12367....(till total chars are 256)"
Why this  chars are coming? Ltrim and Rtrim are also not working!

Thnaks and regards
Rama
 

Is there a particular reason why you initiallize
strFileName = "" & String(256, 0) like this ?

I would just do :
strFileName = ""

Maybe I am missing something here - but it should be enough - and probably solve your problem ?
 
Looking at the declarations, and the structure, it would appear that you are using an API call. If that is the case then the String(256,0) would be to allocate enough space for the FileName. Again, if this is the case, the square boxes are ASCII zero characters.

Based on my assumption above:

mstrFileName = Left(mstrFileName,InStr(mstrFileName,Chr(0))-1) & ".txt"

should do what you require.

hope this helps.

 
Hi,
mstrFileName = Left(mstrFileName,InStr(mstrFileName,Chr(0))-1) & ".txt"

is doing th ejob well.
Thanks a lot.......
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top