Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - export to excel with vbscript not preserving formats

Reply
 
Old 10-06-2009   #1 (permalink)
Brian Hman


 
 

export to excel with vbscript not preserving formats

I'm using the following script in a web page (so this may be the wrong
newsgroup) to open excel and dump data from that webpage:

<script language="VBScript">
<!--
Dim strSQL : strSQL = "SELECT XXXXXX from XXXXXX where XXXXX"
Dim connect
Set connect = CreateObject("ADODB.Connection")
connect.Open "XXXXXXXXXX"
Dim recordset
Set recordset = connect.Execute(strSQL)
Dim app
Set app = CreateObject("Excel.Application")
app.Visible = true
Dim oBook
Set oBook = app.Workbooks.Add
Dim oSheets
Set oSheets = oBook.Sheets
Dim oSheet
Set oSheet = oSheets(1)
RsToExcel oSheet,recordset
Sub RsToExcel(mySheet,rs)
Dim fieldNames,i
rs.MoveFirst
Redim fieldNames(rs.Fields.Count - 1)
For i=0 To rs.Fields.Count -1
fieldNames(i)=rs.Fields(i).Name
Next
mySheet.Range(mySheet.Cells(1,1),mySheet.Cells(1,rs.Fields.Count)).Value=fieldNames
For i=1 To rs.Fields.Count
mySheet.Columns(i).AutoFit
Next
mySheet.Cells.CopyFromRecordSet rs
mySheet.Rows(1).Insert
-->
</script>

The problem here is that when I look at the data in excel some of the
columns that are decimal are coming across as dates. Should I be doing
something in the vbscript to ensure that the columns' formats are preserved
from sql server? I'm not really sure how to proceed.

Brian Hman

My System SpecsSystem Spec
Old 10-07-2009   #2 (permalink)
mr_unreliable


 
 

Re: export to excel with vbscript not preserving formats

Brian Hman wrote:
Quote:

> The problem here is that when I look at the data in excel some of the
> columns that are decimal are coming across as dates. Should I be doing
> something in the vbscript to ensure that the columns'
hi Brian,

Yes, I think you should "do something".

Take a look at microsoft's "Text or number converted to
unintended number format in Excel", found here:

http://support.microsoft.com/kb/214233

It explains what could be causing unintended formatting.

There is also a suggestion(s) as to how to explicitly set
the formatting you want. That can be done is script,
as well as "manually". If you are uncertain as to how
to do it, turn on your macro recorder and then go through
the manual process. Save the macro, and look at the (vba)
code. It should be fairly easy to convert vba to vbs.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Vbscript to Search Excel VB Script
VB :Export datagrid to excel .NET General
export grid to excel .NET General
Export to Excel truncates numbers .NET General
new to excel and vbscript - need help VB Script


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46