Greetings community,
Up until now, whenever I had the need for more comfort with manipulating data in Excel, instead of VBA, I was using VS and VB.NET with Microsoft.office.interop referenced. I want to point out that I don’t have any experience with VSTO,
and that I need few answers on very basic questions.
Out of curiosity, in new project dialog of VS, I selected Excel workbook.
First question Did I get it right the following: after I finish project of this type, something will be installed in Windows, and every Excel file with the same name as the file in the project, would be provided with some additional functionality,
nevertheless where that excel file is saved. And if I take that Excel file from my computer to the other one that does not have my project installed, I would get simple plain Excel file, without any additional functionality.Is this correct?
Well, I named the project ExcelWorkbookProbe, chose .xlsm file type (because I thought I had to) and project was created. I was instantly excited with comfort of possibility of creating ribbon in designer, and things like that, but was
disappointed that some things that were working perfectly with interop, here didn’t want to work.
For the beginning I wanted something simple. I named sheet1 “Startings”, and in the first column intended to write time/date stamp whenever the user opens this document. Nothing fancy really, so I opened ThisWorkbook.vb and added this
piece of code.
Private Sub ThisWorkbook_Startup() Handles Me.Startup
Dim wsS As Worksheet
wsS = Me.Worksheets("Startings")
Dim lRow = wsS.Range("a1").CurrentRegion.Rows.Count + 1
wsS.Range("a" & CStr(lRow)).Value = Now
End Sub
I thought it would work, but in the second line of sub, where I wanted to reference worksheet object variable, it said it cannot cast COM object to it. First few lines of exception description are:
System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Tools.Excel.Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface
with IID '{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Source=ExcelWorkbookProbe
Second question So what do I do? How am I to reference specifically named sheet so I could do something in it. I often have a need to reference several worksheets,
and copy some values from one to the other, considering values from the third sheet.
Please help me accomplishing this basic thing that could be easily done in VBA, so I could dig up further.