Results 1 to 9 of 9

Thread: Saved Worksheet looks like garbage/New problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    Saved Worksheet looks like garbage/New problem

    Hello,

    I attempt to save a Worksheet to the Users PC using the following code (commented out stuff to get the bare-boned code to work), but the output looks like garbage. This is an Excel/VBA app run on a web server, and this button is on a UserForm.

    VB Code:
    1. Private Sub cmdSave_Click()
    2. Dim varFileName As Variant
    3. Dim NumRows As Long, NumCols As Integer
    4. Dim r As Long, c As Integer
    5. Dim data
    6.  
    7. On Error GoTo 300
    8. NumCols = Columns.Count
    9. NumRows = Rows.Count
    10. ChDir "C:\"
    11. varFileName = Application.GetSaveAsFilename("MyReport.txt", "Text Files (*.txt),*.txt," & _
    12. "Print Files(*.prn),*.prn", 1, "MyReport")
    13. If (VarType(varFileName) = vbString) Then
    14.  
    15. Open varFileName For Output As #1
    16. ' For r = 1 To NumRows
    17. '  For c = 1 To NumCols
    18. '  data = Cells(r, c).Value
    19. '  If c <> NumCols Then
    20.    Print #1, data;
    21. '  Else
    22. '   Print #1, data
    23. '  End If
    24. '  Next c
    25. ' Next r
    26. Close #1
    27.  
    28. ActiveWorkbook.SaveAs FileName:=varFileName
    29. Exit Sub
    30. 300:
    31. MsgBox Err.Number & " " & Err.Description
    32. Resume Next
    33. End If
    34. End Sub
    When the User clicks a button, they should be prompted as to where to save the file (this works fine) then I have to save it there.
    Should the line that says, "ActiveWorkbook.SaveAs etc." be "ActiveWorkbook("Worksheet Name").SaveAs etc." ?

    Thank you for any help,
    CJ
    Last edited by CyberJar; Apr 6th, 2004 at 07:10 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    ActiveWorkbook does not take any indexes because it is supossed to be for the active workbook - no need to tell it which
    one. You can use this to specify a different workbook.
    VB Code:
    1. Application.Workbooks("Book1").SaveAs varFileName
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257
    Thanks for your reply.

    I won't index the ActiveWorkbook, but
    can you tell why the output is coming out as garbage?

    Thank you,
    CJ

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I think (without actually seeing the results) that you are now
    saving the file in any type of format. Just writting out certain cell
    contents. Try saving the workbook in something like Excel format.
    Either xlExcel7 or xlCSV.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    Try using a .xls extension to save as an excel format file.

    .txt saves it as space delimited and .prn saves it as tab.delimited which means that unless you are a piece of code looking for data, it's not meant to be pretty. These are used mostly to import or export data between different applications, mostly older programs.
    -----
    #VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP

    I miss my VIC 20.
    Never should have upgraded to my commodore 64. ...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257
    Thanks for the help guys.

    The problem was - I was not using the FIleFormat Property of the SaveAs method. This solved it:

    objWS.SaveAs FileName:=varFileName, FileFormat:=xlTextPrinter

    Then in GetSaveAsFilename I only allow .PRN for FileFilter. My downloaded file is looking good.

    One thing:
    Can I format the file before using SaveAs? Suppose I'd like to make the Font Size smaller so the data can fit on 1 printed page, or Zoom to 50%, do I use PageSetUp ? Seems odd because one associates PageSetup with Printing - not downloading a Worksheet, but the Worksheet *is* for Downloading and possible printing at a later time.

    Please advise,
    CyberJar

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Seems odd because one associates PageSetup with Printing - not downloading a Worksheet
    The page setup is to format the page either for margins or orientation, etc.
    whether it gets printed or not - hence the word page setup. If it
    was print setup (properties) then it would refer to setting up the
    printer, not the page to be printed.

    To answer you last question, you could format the page as you
    wanted it to be for the .SaveAs method and that will be how it will
    be .SaveAs and when you close the workbook just do not save
    your changes so the next time you open it, it will have the
    original page setup and formatting, etc.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257
    I spoke too soon. On further inspection, the SAVEAS method is trying to Save the Worksheet back into the Xls - with a new name. If the Sheet name changes - the app won't run!

    This is unwanted behavior.

    The msdn definition of SaveAs says "Saves changes to the sheet (syntax 1) *in a different file*" This is the behavior I expected.

    Code:
    Private Sub cmdSave_Click()
    Dim varFileName As Variant
    
    On Error GoTo 300
    ChDir "C:\"
    varFileName = Application.GetSaveAsFilename("My Report", "Print File(*.prn),*.prn", , "Save Output")
    If (VarType(varFileName) = vbString) Then
    OWS.SaveAs FileName:=varFileName, FileFormat:=xlTextPrinter
    Exit Sub
    End If
    
    300:
    MsgBox Err.Number & " " & Err.Description
    Resume Next
    End Sub
    So if my Worksheet is called "Output", after the SaveAs it's now called "My Report" - actually taken from GetSaveAsFilename. In debug I can see varFileName is set to "C:\My Report.prn"
    BTW: OWS is an Excel.Worksheet set to the Worksheet Output.

    TIA,
    CJ

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257
    Just noticed that under Tools/Options, there's a "Transition" tab where you can set "Save Excel files as". Mine is set to Microsoft Excel Workbook. All of the FileFormat:= settings are listed.

    Do I need to set this option to "Formatted Text (space delimited) to stop SaveAs from trying to save the Worksheet back into the Workbook with a new name as stated above?

    Thanks,
    CJ

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