I have got following codes from this microsoft page.
https://msdn.microsoft.com/en-us/library/e9waz863(v=vs.90).aspx
' Add Option Strict Off to the top of your program.
Option Strict Off
.
Private Sub getExcel()
Dim fileName As String = "c:\vb\test.xls"
If Not My.Computer.FileSystem.FileExists(fileName) Then
MsgBox(fileName & " does not exist")
Exit Sub
End If
' Set the object variable to refer to the file you want to use.
Dim excelObj As Object = GetObject(fileName)
' Show Excel through its Application property.
excelObj.Application.Visible = True
' Show the window containing the file.
Dim winCount As Integer = excelObj.Parent.Windows.Count()
excelObj.Parent.Windows(winCount).Visible = True
' Insert additional code to manipulate the test.xls file here.
' ...
excelObj = Nothing
End Sub
Everything is okey when Option Strict Off
Everything is not okey when Option Strict On
So, how to solve that errors when Option Strict On?
Be careful! I want to get specific excel file from the same excel instance.
+1 for striving to use Option Strict On. :)
However, the code is using what is known as Late Binding and that requires you have Option Strict Off. You can however, minimize the scope of Option Strict Off by creating a new code file and using a Partial Class definition to contain the code that needs Late Binding.
You might be able to get by using the VB CallByName function with Option Strict On, but that would get ugly real quick and probably be very slow.
There also is an advanced technique using native API's that is called COM reflection that should work with Option Strict On. This technique is described in the article: [Basic Instincts - Inspecting COM Objects with Reflection].(https://msdn.microsoft.com/en-us/magazine/dd347981.aspx).
The typical method for early binding (Option Strict On) is to add a reference to the Excel primary interop assembly. This technique has some detractors as well, but is by far the easiest method.
Edit: The following demonstrates how to use the Excel PIA's and early binding to open a workbook directly similar to the OP's original code.
Dim wbPath As String = "*** replace with path to your workbook ***"
Dim wb As Excel.Workbook = CType(GetObject(wbPath), Excel.Workbook)
'or
'Dim wb As Excel.Workbook = CType(Marshal.BindToMoniker(wbPath), Excel.Workbook)
Dim app As Excel.Application = wb.Application
app.Visible = True
wb.Windows(1).Visible = True
As a side note, there is no need to depend on the Interop.Excel early binding, if you know that it works. You can change the Error Notifications to Warning and still compile and run.
' Imports Microsoft.Office.Interop.Excel
Dim fileName = "c:\vb\test.xls"
If Not IO.File.Exists(fileName) Then MsgBox(fileName & " does not exist") : Exit Sub
Dim obj = GetObject(fileName, "Excel.Application")
Dim wb = TryCast(obj, Workbook)
wb.Application.Visible = True
wb.Windows(1).Visible = True
Related
I'm new to programming in VBA or object-oriented programming. I'm trying to get this code to work in the VBA compiler of Arena by Rockwell Automation.
I sometimes get Error 424: 'Object required', and after debugging I'm referred to the XL.Quit at the end. Sometimes I've got an error related to the use of ActiveEntity. I don't know if I'm wrong getting the Excel file to work or if it's something do to with accessing the entity attributes of Arena anymore.
Can you spot the error? Or do you have a suggestion on how to find it myself?
Public Sub ModelLogic_RunBeginSimulation()
Dim m As Model
Set m = ThisDocument.Model
Dim s As SIMAN
Set s = m.SIMAN
Dim FileToOpen As String
Dim ArenaDir As String
Set XL = GetObject("", "Excel.Application")
ArenaDir = Mid(m.FullName, 1, Len(m.FullName) - Len(m.Name))
FileToOpen = ArenaDir & "data.xlsx"
XL.Workbooks.Open FileToOpen
End Sub
Public Sub VBA_Block_1_Fire()
Dim RNEG_deltaw As Double, Z_deltaw As Double, RPOS_deltaw As Double
Dim R_SI As Double, Z_SI As Double, S_SI As Double
Dim SI As Double, NumContenedor As Integer, Wo As Double, W_1 As Double, deltaW As Double
NumContenedor = s.EntityAttribute(s.ActiveEntity,s.SymbolNumber("NumContenedor"))
SI = XL.Workbooks(1).Worksheets(1).Cells(NumContenedor, 3).value
s.EntityAttribute(s.ActiveEntity, s.SymbolNumber("deltaW")) = SI
End Sub
Public Sub ModelLogic_RunEnd()
XL.Quit
End Sub
The XL Object is declared as procedure-level and therefore doesn't exist in the scope of another procedure.
If you want a variable or object to be visible to all procedures, declare it at the beginning of the module, perhaps like:
Public XL as Object
That being said, you didn't declare XL it in the other procedure either. Add this line to the very top of this module (and every module, ever):
Option Explicit
This will force you to properly declare and handle your variables, objects, properties, etc, and help prevent issues like this.
One more thing: I bet you've run this code at least a few times, right?
Save your work and then REBOOT, right away. Just trust me.
More information:
MSDN : Understanding Scope and Visibility
When Are You Required To Set Objects To Nothing?
Jon Skeet : Writing the Perfect Question
Assuming XL is declared at module level, I believe the error relates to this line:
Set XL = GetObject("", "Excel.Application")
This requires Excel to be running. It appears when ModelLogic_RunEnd() is fired, tries to call the .Quit() method of Excel, but XL is nothing.
Try changing it to CreateObject() and see if that helps:
Set XL = 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)
I am trying to prompt the user when he tries to close a workbook like this
Private Sub Workbook_BeforeClose(Cancel as Boolean)
MsgBox "Changes has been detected. Do you want to export the data ?"
End Sub
I know that this code need to be placed in ThisWorkbook module.
Is there a way to do that from my custom module ? I need to add this functionality to multiple workbooks used by my client as a part of up-gradation, which is usually done by replacing old modules with new modules.
You can use the Application.VBE object and Workbook.VBProject to modify a file's VBA. Note however that it requires that the Excel performing the upgrade has to have the setting "Trust access to the VBA project" toggled on (it can be found in the Trust center under the tab Macro settings). When not needed anymore, it's an option best left off though for security reasons.
There is a way how you can Import the ThisWorkbook Module. I wrote some Code for that a long Time ago.
So how does it work.
First you have to Export the ThisWorkbook Module. Right click on the Module and Export.
Save the ThisWorkbook.cls on the Server where you have your other Module's or send it with the Modules (Like how you do the Upgrade of the other Modules)
Open the ThisWorkbook.cls File with a Editor (Like Notepad++)
And Delete The First Rows. They Look like This.
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Execute the UpdateThisDocument Subrutine.
The Only Question how have to answer yourself is how you will Execute The Code ^^ (I wrote en Extern Updater that Executed the Code)
Sub UpdateThisDocument()
Dim ModuleName As String
ModuleName = "DieseArbeitsmappe"
Dim aDoc As Workbook
Set aDoc = ThisWorkbook
Dim strPath As String
'Put here the Path to the Folder where the cls File of This Workbook is.
strPath = "C:\Users\z002mkvy\Desktop\"
With aDoc
If ModuleExists(ModuleName) = True Then
Call clsLoeschen
End If
'
With ThisWorkbook.VBProject
.VBComponents(ModuleName).CodeModule.AddFromFile _
strPath & "\DieseArbeitsmappe.cls"
Fehler:
End With
End With
End Sub
Private Function ModuleExists(ModuleName As String) _
As Boolean
On Error Resume Next
ModuleExists = Len(ThisWorkbook.VBProject _
.VBComponents(ModuleName).Name) <> 0
End Function
Private Sub clsLoeschen()
Dim modcls
Dim awcl As Integer
On Error Resume Next
Set modcls = ThisWorkbook.VBProject.VBComponents.Item("DieseArbeitsmappe")
awcl = modcls.CodeModule.CountOfLines
modcls.CodeModule.DeleteLines 1, awcl
Set modcls = Nothing
End Sub
I hope This can Help you
I'm learning Visual Basic for Applications, and I'm a little unsure of when to use procedures. For example, is a procedure needed every time an object variable is created? Or can an object variable be created without being executed as a procedure?
Could I have just
Dim wkbInventory As Workbook
Set wkbInventory = Application.Workbooks("name.xlsm")
Thank you!
A object variable can be declared outside a procedure. So the
Dim wkbInventory As Workbook
can be in the Declarations (see https://msdn.microsoft.com/en-us/library/dd897495%28v=office.12%29.aspx#odc_ac2007_bk_BeginningAccess2007VBA_Chapt2_CreatingModules) .
The scope of the variable is dependent on where the declaration happens. See http://www.cpearson.com/Excel/Scope.aspx.
But a variable can't be instantiated outside a procedure or function. So the
Set wkbInventory = Application.Workbooks("name.xlsm")
must be inside the Procedures within a sub procedure or a function.
Almost all of the work in VBA happens inside of macros. You can declare variables outside of macros (although this changes the variable's scope compared to declaring it inside a macro) and specify some compiler options but that's about it.
This is OK:
Option Explicit
' Declaration
Dim c As Collection
Sub foo()
Set c = New Collection
c.Add "hello", "world"
MsgBox c.Count
End Sub
whereas this will generate a compile error - "Invalid outside procedure":
Option Explicit
' Declaration
Dim c As Collection
' This causes an error
Set c = New Collection
Sub foo()
c.Add "hello", "world"
MsgBox c.Count
End Sub
If you use implicit creation with your variable declaration (Dim x As New y) then that works outside of a macro. Not all objects can be created that way though. This code works:
Option Explicit
' Declaration with implicit creation
Dim c As New Collection
Sub foo()
c.Add "hello", "world"
MsgBox c.Count
End Sub
whereas this code doesn't work (you'll get error 429 - ActiveX component can't create object):
Option Explicit
' Declaration with implicit creation
Dim w As New Workbook
Sub foo()
MsgBox w.FullName
End Sub
First recommendation I would make is to put Option Explicit at the top of your code(above all subs and functions). That will help you know when things are not correct.
Dim wkbInventory As Workbook Set wkbInventory = Application.Workbooks("name.xlsm")
Will not work. You will have to do
Dim wkbInventory As Workbook
Set wkbInventory = Application.Workbooks("name.xlsm")
FYI, there is late binding versus early binding that might help you (see also this thread for a an overview) . This is something to learn unrelated to procedures so I'm just providing as an if you already din't know...
You can early bind
' Set reference to 'Microsoft Excel 8.0 Object Library' in
' the Project|References dialog (or Tools|References for VB4 or VBA).
' Declare the object as an early-bound object
Dim oExcel As Excel.Application
Set oExcel = CreateObject("Excel.Application")
' The Visible property is called via the v-table
oExcel.Visible = True
' Good practice to explicitly set object variables to "Nothing" when
' done but not mandatory (especially not for local references).
Set oExcel = Nothing
Or late bind
' No reference to a type library is needed to use late binding.
' As long as the object supports IDispatch, the method can
' be dynamically located and invoked at run-time.
' Declare the object as a late-bound object
Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")
' The Visible property is called via IDispatch
oExcel.Visible = True
' Good practice to explicitly set object variables to "Nothing" when
' done but not mandatory (especially not for local references).
Set oExcel = Nothing
I am trying to automatically add a reference while running a sub.
But I get "User defined-type not defined" for project As VBAProject
What's wrong?
Sub tester()
Dim SolverPath As String
SolverPath = Application.LibraryPath & "\SOLVER\SOLVER.XLA"
Dim wb As Workbook
Set wb = ThisWorkbook
Dim project As VBAProject
project = wb.VBProject
wb.project.References.AddFromFile SolverPath
End Sub
VBAProject is actually a special type of library, not a variable type. If you hit F2 to bring up the Object Browser, you can select it from the library dropdown. So you can't create variables of type VBAProject. However, it's not necessary to, nor for ThisWorkbook, unless you just want a shorter alias. Just use the workbook's VBProject property to gain access to the current project instance and add/remove components. Your code could be simplified to:
Sub tester()
Dim SolverPath As String
SolverPath = Application.LibraryPath & "\SOLVER\SOLVER.XLA"
ThisWorkbook.VBProject.References.AddFromFile SolverPath
End Sub