How To Format Cell as Text Or Date Using Office Web Components Spreadsheet Component?

Hi:

I'm using Office Web Component to generate spreadsheets.

Example, though VB6

Dim objS as object
Set objS = CreateObject("owc.spreadsheet")

'Build Header
objS.cells(1, 1).Value = "Project"
objS.cells(1, 2).Value = "Tasks Completed"
objS.cells(1, 2).Value = "Last Status Date"

'Add a row of data
objS.cells(2, 1).Value = "MyProject"
objS.cells(2, 2).Value = "5/6" 'Want this to appear in the spreadsheet as a fraction.
objS.cells(2, 2).Value = "2006-05-11 10:21 am" 'Want this to appear as a date and time

'Save
objS.activesheet.Export "c:\temp\myspreadsheet.xls", 0

The problem is that my "5/6" value is converted to a date: 6/May.  I want to it remain a fraction: 5/6.  How can I format the column or cell to be text?  I don't want to put an apostrophe in front of the value because a program needs to read the values and import them and I don't want it importing '5/6 instead of 5/6.

The problem with my date is that it's being converted to a number.  I want this to remain a date.

Thanks!
Kevin


ksmAsked:
Who is Participating?
 
mvidasConnect With a Mentor Commented:
Looking at your code, it looks like you need to set the cells(2,3) to "@" as well, but if it didn't work for 5/6 it probably wouldn't work for the date/time either.  
What about just doing:

objS.cells(2, 2).Value = "'5/6" 'Want this to appear in the spreadsheet as a fraction.
objS.cells(2, 3).Value = "'2006-05-11 10:21 am" 'Want this to appear as a date and time

Putting the ' symbol before the data will force it to be text.  Everything I've just read about OWC says the "@" should work, but obviously it does not.  Hopefully putting the "'" beforehand will work.

Matt
0
 
jmundsackCommented:
Try this before you insert the value:

objS.cells(2,2).NumberFormat = "@"

HTH-Jon

P.S. I find it very useful, when I'm not sure how to code something in an Excel script, to record a macro in Excel, doing what I want to accomplish, and then looking at the VB code that gets generated by the recording.

0
 
mvidasCommented:
Hi Kevin,

I want to begin this by saying I don't know the office web component object.  But based on the code you've I'm guessing this will work

If you just want the 5/6 to appear as a fraction, try adding the line (before the .Value line):
 objS.Cells(2, 2).NumberFormat = "# ???/???"
which will format it as a fraction of up to 3 digits.  If you want it to be just text, try:
 objS.Cells(2, 2).NumberFormat = "@"

Likewise for the date/time, try this before the .value line:
 objS.Cells(2, 3).NumberFormat = "yyyy-mm-dd hh:mm AM/PM"

If you want the entire column formatted those ways, try:
 objS.Columns(2).NumberFormat = "@"
 objS.Columns(3).NumberFormat = "yyyy-mm-dd hh:mm AM/PM"

Again, make sure those are run before the .Value lines are added.  These are just using the Excel object as a basis, so I do apologize if it is not the correct OWC syntax.

Matt
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ksmAuthor Commented:
Hi:

I had tried using objS.cells(2,2).NumberFormat = "@" but it didn't work.  I didn't even receive an error.  Here's the part of the output that contains the data, before opening in Excel:

<table class=wc4911275 x:str>
 <col width="64">
 <col width="64">
 <col width="64">
 <tr height="17">
  <td>Project</td>
  <td>Tasks Completed</td>
  <td>Last Status Date</td>
 </tr>
 <tr height="17">
  <td>MyProject</td>
  <td class=wcC814275 x:num="38843">6/May</td>
  <td class=wc0634275 x:num="38848.431250000001">5/11/2006 10:21</td>
 </tr>
</table>

5/6 is still being converted to 6/May.

Here's the procedure in its entirety (with the typos above (such as column number) corrected):

Sub CreateTestSpreadSheet()

Dim objS As Object
Set objS = CreateObject("owc.spreadsheet")

'Build Header
objS.cells(1, 1).Value = "Project"
objS.cells(1, 2).Value = "Tasks Completed"
objS.cells(1, 3).Value = "Last Status Date"

'Add a row of data
objS.cells(2, 1).Value = "MyProject"
objS.cells(2, 2).NumberFormat = "@"
objS.cells(2, 2).Value = "5/6" 'Want this to appear in the spreadsheet as a fraction.
objS.cells(2, 3).Value = "2006-05-11 10:21 am" 'Want this to appear as a date and time

'Save
objS.activesheet.Export "c:\temp\myspreadsheet.xls", 0

End Sub

I do not see anything wrong.  Setting NumberFormat = "# ???/???" also didn't seem to do anything.

Kevin

0
 
tushar_compCommented:

hi
try this out,
i think u will get what u want
her i am using now() function to get current time and date v
u can store ur value in date type variable
define it first
like
dim d as date
then set the value to it
 
the code is given below copy , paste it in ur code and done ....

objS.cells(2, 1).Value = "MyProject"
objS.cells(2, 1).Value = "" & " 5 / 6 " & "" 'Want this to appear in the spreadsheet as
objS.cells(2, 1).Value = "" & Now() & ""    'Want this to appear as a date and time


if its useful
let me know

Tushar
0
 
ksmAuthor Commented:
tushar:

objS.cells(2, 1).Value = "" & " 5 / 6 " & "" 

that didn't work.  Excel still interpreted the value as May 6 when I opened the file.  Thanks, though.
0
 
ksmAuthor Commented:
mvidas:

I think have to use your method.  I didn't want to use an apostrophe but it's the only thing that seems to work for the fraction (5/6).  Formatting the date in the form of yyyy-mm-dd hh:nn ampm seems to get the date to show up OK as well.

Thanks.
Kevin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.