vba 我收到运行时错误 462

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3635289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:04:05  来源:igfitidea点击:

I am getting run time error 462

excelms-accessvba

提问by tksy

HI,

你好,

I have the following piece of code in Access.

我在 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.

我有两个问题。每次运行代码时,我都会在 Charts.add 行收到运行时错误 462(远程服务器机器不存在或不可用)。

I know that I am not using the objexcel property correctly but I am not sure where I am going wrong.

我知道我没有正确使用 objexcel 属性,但我不确定我哪里出错了。

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?

同样在代码运行后,即使 excel 关闭。该进程在后台运行,这会干扰代码的下一次运行。如何关闭 excel 并将其从任务管理器进程中删除?

回答by Fink

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.

错误 462 通常意味着某些内容不正确,即使消息有点神秘。

回答by JohnZaj

As you your question

正如你的问题

How do I close excel and get rid of it from task manager processes also?

如何关闭 excel 并将其从任务管理器进程中删除?

You should be able to use the Application.Quitcommand 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.Quitcommand. Another alternative method is to delegate this to a shell command: Shell "taskkill /f /im excel.exe".

Application.Quit只要您不是从 Excel 以外的其他应用程序运行子程序,您就应该能够在代码末尾使用该命令。或者,您应该能够执行objexcel.Quit命令。另一种替代方法是将其委托给 shell 命令:Shell "taskkill /f /im excel.exe".

I hope this helps. Did you get a working piece of code yet?

我希望这有帮助。你有没有得到一段有效的代码?

回答by Kevin Ross

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

让我知道这是否能解决问题