|
-
Apr 5th, 2004, 12:17 PM
#1
Thread Starter
Hyperactive Member
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:
Private Sub cmdSave_Click()
Dim varFileName As Variant
Dim NumRows As Long, NumCols As Integer
Dim r As Long, c As Integer
Dim data
On Error GoTo 300
NumCols = Columns.Count
NumRows = Rows.Count
ChDir "C:\"
varFileName = Application.GetSaveAsFilename("MyReport.txt", "Text Files (*.txt),*.txt," & _
"Print Files(*.prn),*.prn", 1, "MyReport")
If (VarType(varFileName) = vbString) Then
Open varFileName For Output As #1
' For r = 1 To NumRows
' For c = 1 To NumCols
' data = Cells(r, c).Value
' If c <> NumCols Then
Print #1, data;
' Else
' Print #1, data
' End If
' Next c
' Next r
Close #1
ActiveWorkbook.SaveAs FileName:=varFileName
Exit Sub
300:
MsgBox Err.Number & " " & Err.Description
Resume Next
End If
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.
-
Apr 5th, 2004, 12:35 PM
#2
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 5th, 2004, 12:50 PM
#3
Thread Starter
Hyperactive Member
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
-
Apr 5th, 2004, 01:27 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 5th, 2004, 04:32 PM
#5
Lively Member
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. ...
-
Apr 6th, 2004, 12:29 AM
#6
Thread Starter
Hyperactive Member
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
-
Apr 6th, 2004, 10:32 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Apr 6th, 2004, 07:24 PM
#8
Thread Starter
Hyperactive Member
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
-
Apr 6th, 2004, 07:55 PM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|