[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-11
7
Medium Priority
?
1,545 Views
Last Modified: 2012-05-05
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


0
Comment
Question by:ksm
7 Comments
 
LVL 13

Expert Comment

by:jmundsack
ID: 16658822
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
 
LVL 35

Expert Comment

by:mvidas
ID: 16658834
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
 

Author Comment

by:ksm
ID: 16659091
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 16659374
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
 
LVL 1

Expert Comment

by:tushar_comp
ID: 16666334

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
 

Author Comment

by:ksm
ID: 16746405
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
 

Author Comment

by:ksm
ID: 16746492
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question