I am getting run time error 462 - excel

HI,
I have the following piece of code in Access.
Dim objSht As excel.Worksheet
Dim objexcel As New excel.Application
Dim wbexcel As excel.Workbook
Dim wbExists As Boolean
Dim objRange As excel.Range
Dim isFileAlreadyPresent As Boolean
Set objexcel = CreateObject("excel.Application")
Set wbexcel = objexcel.Workbooks.Open(file_name)
Set objSht = wbexcel.Worksheets(table_name)
isFileAlreadyPresent = True
objSht.Activate
objSht.Range(Range_para).Select
Charts.Add
ActiveChart.chartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets(table_name).Range(Range_para), _
PlotBy:= xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.HasLegend = False
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.text = CHart_title
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
If isFileAlreadyPresent = True Then
wbexcel.Save
Else
wbexcel.SaveAs (file_name)
End If
objexcel.Visible = True
wbexcel.Close
I am having two problems. Every second time I run the code I get an run time error 462 (The remote server machine does not exist or is unavailable ) at line Charts.add.
I know that I am not using the objexcel property correctly but I am not sure where I am going wrong.
Also after the code is run, even though excel closes. The process runs in the background and this interferes with the next run of the code. How do I close excel and get rid of it from task manager processes also?

I think you will need to create the chart object like this, since your using late binding it won't know what "Charts" is unless you call it from the parent object.
objexcel.Charts.Add
Error 462 usually means something isn't qualified right, even though the message is sort of cryptic.

Just as a quick fix trying setting everything you declare/use to nothing at the end of your code to ensure that nothing is left open or active.
Set objSht = Nothing
Let me know if that fixes the problem

As you your question
How do I close excel and get rid of it
from task manager processes also?
You should be able to use the Application.Quit command at the end of your code as long as you aren't running the sub from another application other than Excel. Or, you should be able to do an objexcel.Quit command. Another alternative method is to delegate this to a shell command: Shell "taskkill /f /im excel.exe".
I hope this helps. Did you get a working piece of code yet?

Related

How to set FreezePanes property of the Windows class?

I heavily rely on "Microsoft Project". Most of my colleagues don't have "Microsoft Project" so I export the project schedule to Excel using VBA.
Sometimes, I encounter this error message
"unable to set FreezePanes property of the windows class"
Part of the VBA code is as follows.
Dim xlSheet As Excel.Worksheet
xlwindow.Split = True
xlwindow.SplitColumn = AbsoluteColumnPos - 1
xlwindow.SplitRow = 3
xlwindow.FreezePanes = True ' **
I have no idea how to fix this error, but it's always gone at some point.
I can check whether this is specific to the migration from Project to Excel by testing with this VBA code in Microsoft Word.
Sub Excel_Test()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Add
xlApp.Activesheet.Range("B3") = 5
With xlApp.ActiveWindow
.Split = True
.SplitRow = 4
.SplitColumn = 11
.FreezePanes = True
End With
End Sub
The same error message
"unable to set freezepanes property of the windows class"
shows up all the time.
Firstly, you shouldn't need this line:
xlwindow.Split = True
Secondly, you might try to unfreeze first, as I'm not 100% certain that your code is complete, and therefore what might be going on around what you've posted here, and whether it's possible that a freeze is already operational. In other words, try:
xlwindow.FreezePanes = False
xlwindow.SplitColumn = AbsoluteColumnPos - 1
xlwindow.SplitRow = 3
xlwindow.FreezePanes = True

VB Script Runtime Error: Object Required: 'Active Cell'

Trying to run the following VBScript:
Set ObjExcel = createobject("Excel.Application")
Set objWb = objExcel.Workbooks.Open("C:\test.xlsx")
Set objSheet = objWb.Worksheets("Sheet1")
objSheet.Cells(1,1).Select
With ActiveCell.Font
.Bold = True
.Italic = True
End With
Throwing an error as follows:
Line: 7
Char: 1
Error: Object Required: 'ActiveCell'
Codes: 800A01A8
Source: Microsoft VBScript runtime error
So it ain't linking ActiveCell. I have spent hours on this and searched a lot of similar issue's online with no luck, any suggestions? Thanks in advance.
".ActiveCell" (and all those other Active*s) are concepts for the interactive use of Excel. It means the Cell the user just clicked - probably in error, just wait, till he hit the one he meant.
If you automate Excel you are getting rid of the user and therefore the need to keep track of his maniac clicks. Now a PROGRAM asks another PROGRAM to do the necessary tasks directly. No need to simulate a user's click (.Select) and search all over place which Cell was activated.
The PROGRAM just says: "Please -
With objSheet.Cells(1,1).Font
.Bold = True
.Italic = True
End With
thank you so much".
Try this:
Set ObjExcel = createobject("Excel.Application")
ObjExcel.Visible=true 'you can remove this line as per your requirement
Set objWb = objExcel.Workbooks.Open("C:\test.xlsx")
Set objSheet = objWb.Worksheets("Sheet1")
objSheet.Cells(1,1).Select
Set reqCell = ObjExcel.ActiveCell 'Setting reference to the active cell
With reqCell.Font
.Bold = True
.Italic = True
End With

UFT: Excel Process Keeps Running and freezes execution

I have an web automation framework in UFT where I've to update the customized excel report. During the report editing part of code, I am facing a strange issue related to excel process/vba object. I have this code stored in function library (.qfl) inside a function and it is called numerous times. The problem is, sometimes the UFT execution freezes and I see a running process of EXCEL.exe. Nothing happens on the screen for hours until I manually kill the process.
On error resume next
Dim objExcel, objWorkbook, objWorkSheet
Set objExcel = createobject("excel.application")
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open("SheetLocation")
Set objWorkSheet = objWorkbook.WorkSheets("SheetName")
intStepRow = objWorkSheet.Cells.Find("WC_01").Row
objWorkSheet.Cells(intStepRow, 7).Value = "SomeValue"
objWorkSheet.Cells(intStepRow, 8).Value = "SomeValue"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close True
objExcel.Quit()
Set objWorkSheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
On error goto 0
I tried putting a code at the end of function to kill the process but No luck. I also tried setting excel.Visible and .Display to true to see where exactly things go wrong but I don't see anything. I also tried removing the error handling but same, no luck.
Edit:
After enabling logging for each line, I found that below line is freezing the execution
Set objWorkbook = objExcel.Workbooks.Open("SheetLocation")
Thanks in advance for your time and suggestions :)
alternatively i find it interesting, could you try using the objExcel.quit() after you realese the object i.e. after you set wroksheet,workbook, excel = nothing.
e.g.
Set objWorkSheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
objExcel.Quit()

Business Objects Launch from Excel

I am in the process of creating a Business Objects macro for myself using VBA. I have found this code and others similar across the forums everywhere. However upon compiling this in VBA, everything just starts to freeze at the Set BoApp stage. Am I missing something in creating the object? Does the BusinessObjects.application have to say something else that is specific to my Business Objects directory?
Any help is appreciated!
Sub Open_Reports()
Dim BoApp As Object
Application.DisplayAlerts = False
Application.Wait (Now + TimeValue("1:00:00"))
On Error Resume Next
Set BoApp = CreateObject("BusinessObjects.application")
With BoApp
.Visible = True
.LoginAs "username", "password", , "DVBOCEN-APP01"
.documents.Open ("Q:\MI Reporting (BAU)\BAU Daily\Operational Reports\All reports\Sales - Advisor - Daily.rep")
With .Activedocument
.Refresh
.Close
End With
.Application.Quit
End With
Set BoApp = Nothing
ThisWorkbook.Saved = True
.Quit
End Sub
Not sure if it'll help, since it should work either way, but try:
Dim BoApp As busobj.Application
and
Set BoApp = New busobj.Application
I'm assuming you're able to successfully launch the DeskI application manually, and that you've added the BusinessObjects x Object Library as a reference?

Access VBA: working with an existing excel workbook (Run-Time error 9, if file is already open)

I'm writing a macro in Access that (hopefully) will:
create an Excel worksheet
set up and format it based on information in the Access database
after user input, will feed entered data into an existing Excel master file
Opening the blank sheet etc. is working absolutely fine, but I'm stuck trying to set the existing master file up as a variable:
Sub XLData_EnterSurvey()
Dim appXL As Excel.Application
Dim wbXLnew, wbXLcore As Excel.Workbook
Dim wsXL As Excel.Worksheet
Dim wbXLname As String
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
wbXLname = "G:\[*full reference to file*].xlsm"
IsWBOpen = fnIsWBOpen(wbXLname)
'separate function (Boolean), using 'attempt to open file and lock it' method
'from Microsoft site.
If IsWBOpen = False Then
Set wbXLcore = appXL.Workbooks.Open(wbXLname, True, False)
'open file and set as variable.
ElseIf IsWBOpen = True Then
wbXLcore = appXL.Workbooks("ResultsOverall.xlsm") 'ERROR HERE.
'file is already open, so just set as variable.
End If
Debug.Print wbXLcore.Name
Debug.Print IsWBOpen
Set appXL = Nothing
End Sub
When the file is closed, this works perfectly. However, when it's open I get:
Run-Time error '9':
Subscript out of range
I'm only just starting to teach myself VBA (very trial and error!) and nothing else I've seen in answers here / Google quite seems to fit the problem, so I'm a bit lost...
Considering that it works fine when the file is closed, I suspect I've just made some silly error in referring to the file - perhaps something to do with the 'createobject' bit and different excel instances??
Any suggestions would be much appreciated! Thanks
Thank you #StevenWalker
Here's the working code:
Sub XLData_EnterSurvey()
Dim appXL As Excel.Application
Dim wbXLnew As Excel.Workbook, wbXLcore As Excel.Workbook
Dim wsXL As Excel.Worksheet
On Error GoTo Handler
Set appXL = GetObject(, "Excel.Application")
appXL.Visible = True
Dim wbXLname As String
wbXLname = "G:\ [...] .xlsm"
IsWBOpen = fnIsWBOpen(wbXLname)
If IsWBOpen = False Then
Set wbXLcore = appXL.Workbooks.Open(wbXLname, True, False)
ElseIf IsWBOpen = True Then
Set wbXLcore = appXL.Workbooks("ResultsOverall.xlsm")
End If
Set appXL = Nothing
'-------------------Error handling------------------
Exit Sub
' For if excel is not yet open.
Handler:
Set appXL = CreateObject("Excel.Application")
Err.Clear
Resume Next
End Sub
Sorry I'm on my phone so I can't go in to too much detail or do much with the code but at a glance I think you might need to add an error handler so that if the file is already open, a different line of code is executed.
Add 'On error go to handler' (before creating the excel object) and at the bottom
Of your code add 'handler:'. In the error handler, use get object rather than create object.
You will have to ensure you use exit sub before the error handler or it will run the handler every time you run the code.
You can see an example of what I mean here: How to insert chart or graph into body of Outlook mail
Although please note in this example it's the other way round (if error 'getting' outlook, then create it).
Example in link:
Set myOutlook = GetObject(, "Outlook.Application")
Set myMessage = myOutlook.CreateItem(olMailItem)
rest of code here
Exit Sub
'If Outlook is not open, open it
Handler:
Set myOutlook = CreateObject("Outlook.Application")
Err.Clear
Resume Next
End sub
If you move the appXL.Workbooks statement to the debugging window, you will find that the names of the items in that collection are without extension.
So in your case, I'm guessing the line should read:
wbXLcore = appXL.Workbooks("ResultsOverall")

Resources