Link to home
Start Free TrialLog in
Avatar of rama_krishna
rama_krishna

asked on

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 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!
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.