469,646 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Option Strict On related problem.

Hello
After reading yesterday's thread on performance of C# and
vb.net i've just turn it on my solution and spent 2 hours
correcting the errors :)

I have a problem i don't how to fix i have a class that
export data to a Excel Workbook.
I get 2 types of errors : Option Strict On disallows
implicit conversions from 'System.Object'
to 'Excel.Worksheet' and Option Strict On disallows late
binding.

This how the excel objects are defined :
Dim objPrograma As New Excel.Application
Dim objLivro As Excel.Workbook = objPrograma.Workbooks.Add
Dim objFolha As Excel.Worksheet = objPrograma.ActiveSheet

The following raise errors:
objLivro.Sheets("Folha1").name = "PortuguÍs"
objLivro.Sheets("Folha2").delete()

And also:
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()

For example the following works ok:
objFolha.Range("a4").VerticalAlignment =
Excel.XlHAlign.xlHAlignCenter
objFolha.Range("a4").HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter
Any suggestion how to solve this matter, would be most
welcome thanks!

Kind Regards
Jorge Cavalheiro
Nov 20 '05 #1
3 1514
Jorge,
After reading yesterday's thread on performance of C# and
vb.net i've just turn it on my solution and spent 2 hours
correcting the errors :)
Glad you made the change - it's always a good idea to switch "Option Strict
On" all the time. Apart from performace gains because the compiler can
optimise better, it should also give you a better understanding of how
different types convert to and from other types. The better you know how
this process works, the more optimzations you can use in your own code
because you can tweak your design.
The following raise errors:
objLivro.Sheets("Folha1").name = "PortuguÍs"
objLivro.Sheets("Folha2").delete()
As mentioned, Option Strict ON does not allow late binding. I'm not
familliar with Excel's object model, but I suspect that
"objLivro.Sheets("Folha1")" returns an Object, not a specific type.

Try wrapping the call in a call to "ctype" or "directcast" like this

---------------

directCast(objLivro.Sheets("Folha1"), Excel.WorkSheet).name = "PortuguÍs"
directCast(objLivro.Sheets("Folha2"), Excel.WorkSheet).delete()

---------------
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()


Try the following
---------------

directCast(objLivro.Sheets("English"), Excel.WorkSheet).activate()
objFolha = directCast(objPrograma.ActiveSheet, Excel.WorkSheet)

---------------

Obviously, change the type"Excel.WorkSheet" to whatever type you are
expecting. I would also consider using a With Block or a local variable to
hold a strongly typed reference to the objcts you are accessing - this way
you cut down the number of calls to DirectCast

HTH,

Trev.
Nov 20 '05 #2
"Jorge Cavalheiro" <de*******@yahoo.com> schrieb

I have a problem i don't how to fix i have a class that
export data to a Excel Workbook.
I get 2 types of errors : Option Strict On disallows
implicit conversions from 'System.Object'
to 'Excel.Worksheet' and Option Strict On disallows late
binding.

This how the excel objects are defined :
Dim objPrograma As New Excel.Application
Dim objLivro As Excel.Workbook = objPrograma.Workbooks.Add
Dim objFolha As Excel.Worksheet = objPrograma.ActiveSheet

The following raise errors:
objLivro.Sheets("Folha1").name = "PortuguÍs"
I assume Sheets only contains Sheet objects:

Directcast(objLivro.Sheets("Folha1"), Excel.Sheet).name = "PortuguÍs"
objLivro.Sheets("Folha2").delete()
Directcast(objLivro.Sheets("Folha2"), Excel.Sheet).delete

And also:
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()

Use directcast also.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Thanks its working fine now

Kind Regards
Jorge
-----Original Message-----
"Jorge Cavalheiro" <de*******@yahoo.com> schrieb

I have a problem i don't how to fix i have a class that
export data to a Excel Workbook.
I get 2 types of errors : Option Strict On disallows
implicit conversions from 'System.Object'
to 'Excel.Worksheet' and Option Strict On disallows late binding.

This how the excel objects are defined :
Dim objPrograma As New Excel.Application
Dim objLivro As Excel.Workbook = objPrograma.Workbooks.Add Dim objFolha As Excel.Worksheet = objPrograma.ActiveSheet
The following raise errors:
objLivro.Sheets("Folha1").name = "PortuguÍs"
I assume Sheets only contains Sheet objects:

Directcast(objLivro.Sheets("Folha1"), Excel.Sheet).name

= "PortuguÍs"
objLivro.Sheets("Folha2").delete()


Directcast(objLivro.Sheets("Folha2"), Excel.Sheet).delete

And also:
objLivro.Sheets("English").activate()
objFolha = objPrograma.ActiveSheet()

Use directcast also.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

.

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Microsoft News | last post: by
13 posts views Thread by Cor | last post: by
11 posts views Thread by Daylor | last post: by
15 posts views Thread by guy | last post: by
13 posts views Thread by C. Moya | last post: by
6 posts views Thread by Rob | last post: by
4 posts views Thread by rebeccatre | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.