Unable to append string ".txt" to another string

I need to write database 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
I have the following VBA code :
    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 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
        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))
    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!
Who is Participating?
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
Hi rama_krishna,

They are character 0 because you have filled your string with null chars: strFilename = "" & String(256,0)

Rather than using rtrim etc do:

mstrFileName = Left(mstrFileName,instr(mstrFileName,Chr(0))-1)

Before testing for .txt and adding it, after the end if and before txtPosition = Instr......

Tim Cottee
Also I believe the maximum number of characters in a filename is 255, not 256.
I've done some playing and found the maximum no of characters in the filename before the extension is 240.  I believe Windows XP allows up to 15 characters for the extension and with the period, that would make 256 characters for the name excluding the path.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.