Results 1 to 10 of 10

Thread: save as problem

  1. #1
    scoutt
    Guest
    could someone help me with this API call for save as

    Code:
    Private Type OPENFILENAME  _
    	
      lStructSize As Long 
      hwndOwner As Long 
      hInstance As Long 
      lpstrFilter As String 
      lpstrCustomFilter As String  _
    	
      nMaxCustFilter As Long 
      nFilterIndex As Long 
      lpstrFile As String 
      nMaxFile As Long 
      lpstrFileTitle As String 
      nMaxFileTitle As Long 
      lpstrInitialDir As String 
      lpstrTitle As String 
      Flags As Long  _
    	
      nFileOffset As Integer 
      nFileExtension As Integer 
      lpstrDefExt As String 
      lCustData As Long 
      lpfnHook As Long 
      lpTemplateName As String 
    End Type 
     
    Private Declare Function  _
    	GetSaveFileName Lib "comdlg32.dll"  _
    	_
    Alias "GetSaveFileNameA" (pOpenfilename As  _
    	OPENFILENAME) As Long  _
    	
     
    Private Sub Command1_Click() 
    Dim ofn As OPENFILENAME  _
    	
    ofn.lStructSize = Len(ofn) 
    ofn.hwndOwner = Form1.hWnd 
    ofn.hInstance = App.hInstance 
    ofn.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + _
      "*.txt" + Chr$(0) + "Rich Text Files (*.rtf)" + Chr$(0) _
      + "*.rtf" + Chr$(0) 
    ofn.lpstrFile = Space$(254) 
    ofn.nMaxFile = 255 
    ofn.lpstrFileTitle = Space$(254) 
    ofn.nMaxFileTitle = 255 
    ofn.lpstrInitialDir = CurDir 
    ofn.lpstrTitle = "Dialog Title" 
    ofn.Flags = 0 
    Dim A 
    A = GetSaveFileName(ofn) 
     
    If (A) Then 
    MsgBox "File to Save: " + Trim$(ofn.lpstrFile) 
    'FileSave Stuff Here 
    Else 
    MsgBox "Cancel was pressed" 
    End If 
     
    End Sub  
    
    
    
    'Code improved by vBulletin Tool (Save as...)
    I am not sure what to put in the fileSave stuff here. I tried to use print command but it acts like it saves it just doesn't save anything to my hard drive. I don't know anything about API calls.

    Scoutt

  2. #2
    Megatron
    Guest
    I think you would put something like:
    Code:
    Open "MyFile" For Output As #1
    Print #1, Text1
    Close #1

  3. #3
    scoutt
    Guest
    thanks Megatron, I will try that when I get home.

    I will let you know if it works or not.

    Scoutt

  4. #4
    scoutt
    Guest
    Well I played around with it and I couldn't get it to save as a .txt file. I put the code it you suggested and it didn't work.

    Scoutt

  5. #5
    Megatron
    Guest
    MsgBox Trim$(ofn.lpstrFile) <-- Tell me what outputs here.

  6. #6
    scoutt
    Guest
    ok that message said just the file name, no extension. I tried this
    Code:
    MsgBox "File Saved To: " + Trim$(ofn.lpstrFile) & ".txt"
    and it still didn't have an extension on it. just the file name.
    would it hae anyhting to do with this line

    ofn.lpstrFileTitle = Space$(254)

    I am not sure what the space$(254) means.

    Scoutt

  7. #7
    Tygur
    Guest
    I was just looking at this and, from what you're saying, it looks like there's a null in that string. Also, I'd be checking to see if the user put the .txt in himself instead of just blindly throwing it in.

    Instead of just this line:
    ofn.lpstrFile = ofn.lpstrFile & ".txt"

    Try putting this in:
    Code:
    ofn.lpstrFile = Left(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr$(0)) - 1)
    If Right$(LCase$(ofn.lpstrFile), 4) <> ".txt" Then
    ofn.lpstrFile = ofn.lpstrFile & ".txt"
    End If

  8. #8
    Megatron
    Guest
    This will trim it
    Code:
    FileName = Instr(ofn.lpstrFile, vbNullChar - 1)
    I believe the extension is added by VB, but if not then you can add it yourself like so:
    Code:
    FileName = Instr(ofn.lpstrFile, vbNullChar - 1) & ".txt"

  9. #9
    Tygur
    Guest
    Megatron, that doesn't work. You forgot the Left function.

    And how can VB add the extension? We're using the API for the common dialog box.

    Also, what's wrong with the code I posted? (Aside from using Chr$(0) instead of vbNullChar. I forgot the name of the constant when I was writing it.)

  10. #10
    scoutt
    Guest
    Thanks Tygur, that did the trick. it now saves it as a .txt file. thanks Megatron for your help to.

    Scoutt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width