what do you think is wrong with the commented line in my vb.net code below? it returns the error message "member not found". thank you.
Public Function tae(ByVal SourcePath As String)
Dim oxlApp As Excel.Application
Dim oxlBook As Excel.Workbook
Dim oxlSheet As Excel.Worksheet
oxlApp = CType(CreateObject("Excel.Application"), Excel.Application)
oxlBook = CType(oxlApp.Workbooks.Open(SourcePath), Excel.Workbook) //somethings wrong here
oxlSheet = CType(oxlBook.Worksheets(1), Excel.Worksheet)
oxlApp.Workbooks.Close()
oxlApp.Quit()
End Function
i tried to add the following references and its now ok:
ms excel x object library
ms office x object library
visual basic for applications
Related
To create an instance of excel using VB.Net, we can write the following piece of code:
Imports System
Imports Microsoft.Office.Interop.Excel
Imports Word = Microsoft.Office.Interop.Word
Module Program
'declaration statements
Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oWB As Workbook
Dim oSheet As Worksheet
Dim oWord As Word.Application
Dim oDoc As Word.Document
Sub Main(args As String())
'Console.WriteLine("Hello World!")
oXL = CreateObject("Excel.Application")
oXL.Visible = True
oXL.DisplayAlerts = True
Console.WriteLine("Type returned by CreateObject: {0}", oXL.GetType())
oWB = oXL.Workbooks.Add
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
Console.ReadLine()
oXL.Quit() 'Not getting the warning
oWord.Quit(False) 'Not getting the warning
End Sub
End Module
Till here all is good. However I also came across the following statement in the MSDN Link:
Use the Application property to return the Application object.
I checked and found that the APPLICATION property is a member of _Application interface, the usage criteria of which as per this link is mentioned below.
Use this primary interface only when the method you want to use shares the same name as an event of the COM object
However this statement is not clear to me. So can someone please explain (if possible with an example) when should I use the Application property of the _Application interface to return an Application object? Also is the object returned by the Application property any different from the one being returned by createObject in the statement oXL = CreateObject("Excel.Application")?
I am trying to create an Excel Add-in using Vb.Net. I've started an Excel 2007 Add-in Project in VS2010. Sadly, I am not good with vb.net; I am more a VB6 developer in this regard, and my ThisAddin.vb code is:
Public Class ThisAddin
Private Sub ThisAddIn_Startup() Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
' test function; simple
Public Function getRowCount() As Long
Dim thisWB As Workbook = Me.Application.ThisWorkbook
Dim activWS As Worksheet
activWS = thisWB.ActiveSheet
Return activWS.UsedRange.Rows.Count
End Function
End Class
I've also added a Ribbon item (via Add New Item... menu option) in designer mode (not xml) - and then add a button. Then I go to code and try to call the function and I get this error when using:
MsgBox(Globals.ThisAddIn.getRowCount())
Which I got from this link: Calling a procedure within another class
To be honest, I've been trying a myriad things and I've been getting so many errors. I've been looking online as well for a tutorial on creating my own Excel Addin from scratch with no real luck. I would like not to use Add-In-Express since that's a third party app and I have to create an Excel add-in for my company from scratch.
Does anyone have an idea on how I can create a vb.net coded Excel Addin (2007) that I can use as a template or guide? I've tried several and many rely on Add-In-express and I really cannot go that way. I have a lot of VBA code (natural VBA so it's in a module in an my excel files' VBA/Developer section) and I think I can translate those from VBA/VB6 to VB.Net format so that's not my concern. It is really about getting to code my own Excel Addin in VB.Net. Any help would really be great. Thank you.
*note: I would also like not to have to ask coworkers (or do myself) to just add to the quick access toolbar the functions and subs I've created since that's really not a solution, considering that those buttons will be there when they create or open another workbook. Essentially, I've got to create my own excel addin in vb.net. Thank you once again.
The issue has to do with the definitions in Microsoft.Office.Tools.Excel and Microsoft.Office.Interop.Excel. To code an "Interop" version you could use this:
Public Function getRowCount() As Long
Dim thisWB As Excel.Workbook = Application.ActiveWorkbook
Dim activWS As Excel.Worksheet = CType(thisWB.ActiveSheet, Excel.Worksheet)
Return activWS.UsedRange.Rows.Count
End Function
To extend the functionality of the Native objects and use VSTO, you could do it like this:
Public Function getRowCount() As Long
Dim NativeWorkbook As Excel.Workbook = Application.ActiveWorkbook
Dim NativeWorksheet As Excel.Worksheet = CType(NativeWorkbook.ActiveSheet, Excel.Worksheet)
Dim thisWB As Workbook = Nothing
Dim activWS As Worksheet = Nothing
If NativeWorkbook IsNot Nothing Then
thisWB = Globals.Factory.GetVstoObject(NativeWorkbook)
End If
If NativeWorksheet IsNot Nothing Then
activWS = Globals.Factory.GetVstoObject(NativeWorksheet)
End If
Return activWS.UsedRange.Rows.Count
End Function
This is a function you can put in ThisAddin.vb that will create a new Worksheet. Note that this function names the Worksheet and adds it to the end.
Public Function AddWorkSheet(sheetName As String) As Worksheet
Dim wk = Application.ActiveWorkbook
Dim ws As Worksheet = Nothing
Try
ws = CType(wk.Sheets.Add(, wk.Sheets(wk.Sheets.Count)), Worksheet)
ws.Name = sheetName
Catch ex As Exception
Throw
Finally
AddWorkSheet = ws
End Try
End Function
To use this outside of ThisAddin.vb you could do something like this:
Dim ws As Excel.Worksheet
Dim newSheetName As String
.
'
ws = Globals.ThisAddIn.AddWorkSheet(newSheetName)
The following is inside an SSIS script task using VB.net
I have read a bit into this error, and it seems that something is wrong with this line:
worksheet = CType(workbook.Sheets(4), Excel.Worksheet)
My code is below. I think it will be simple for the more experienced folks here.
text:
Public Sub Main()
Dim excel As New Excel.Application
Dim filename As String = "S:\UK\Clients\Direct\xxxxxxx.xls"
excel.Visible = True
excel.DisplayAlerts = False
Dim workbook As Excel.Workbook
workbook = excel.Workbooks.Open(filename, , False) 'True = ReadOnly
Dim worksheet As Excel.Worksheet
worksheet = CType(workbook.Sheets(4), Excel.Worksheet)
worksheet.Rows(1).delete()
Dts.TaskResult = ScriptResults.Success
End Sub
add Option Strict Off at the top of your code, This will allow late binding.
To know Early vs. Late Binding , refer here
you can replace this statement
worksheet.Rows(1).delete()
with
Dim rw As Excel.Range
rw = CType(worksheet.Rows(1), Excel.Range)
rw.Delete()
And set Option Strict On at the top
All your code will be early bound. Hope this helps
I'm trying to open the Excel file using VBA in Powerpoint 2010 with the help of following code.
Private Sub CommandButton1_Click()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
Set xlApp = Nothing
Range("A8").Value = "Hello"
End
But I'm getting the following error.
Compile Error
User Defined type not defined.
Am I missing something. Can anyone share the sample piece of code to open an excel file, change a cell value and close Excel file in Powerpoint 2007 and 2010 using VBA.
I have searched a lot and tried different pieces of code, but getting the same error everytime. :(
Thanks in advance. :)
Have you added a reference to the Excel Object Model? That would save you having to use the late bound objects (and you get the benefit of having the Intellisense help when you are coding).
You need to go to Tools -> References and check the "Microsoft Excel v.x Object Library" (I think that number changes depending on the version of office you are using.
Your code should work if you do that, you should also remove the
CreateObject("Excel.Application")
line and replace it with
Set xlApp = new Excel.Application
And move the
Set xlApp = nothing
line to the end of your subroutine.
The rest of your code looks fine to me.
Late binding code would be this
Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Open("C:\lol\Book1.xlsx", True, False)
xlWorkbook.sheets(1).Range("A8").Value = "Hello"
Set xlApp = Nothing
Set xlWorkbook = Nothing
End Sub
It's better to use early binding though.
I am getting an error when I run this app in VS 2010 (it works fine in VS 2008)
Private Sub GenerateInvoice()
Dim emptyObject As Object = System.Reflection.Missing.Value
Dim wordApp As New Word.Application
wordApp.Visible = True
Dim InvoiceDoc As New Word.Document
InvoiceDoc = wordApp.Documents.Add(InvoicePath, emptyObject, emptyObject, emptyObject)
Dim totalFields As Integer = 0
For Each mergeField As Word.Field In InvoiceDoc.Fields
The error occurs at the For Each line
"Object reference not set to an
instance of an object."
Am I missing something here?
Maybe the InvoicePath used in the instance run via VS2010 is invalid and so the call to Documents.Add fails?
Are you running both VS2010 and VS2008 on the same machine? And is the InvoicePath set to the exact same path in both instances?
Try
Dim InvoiceDoc As Word.Document
wordApp.Documents.Add(InvoicePath, emptyObject, emptyObject, emptyObject)
InvoiceDoc=wordApp.ActiveDocument