热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

运行时错误91excelvba,对象未设置-runtimeerror91excelvba,Objectnotset

whatiswrongwiththefollowingcode?,EverytimeIrunitIgetaRun-TimeError91,Objectvaria

what is wrong with the following code?, Every time I run it I get a "Run-Time Error 91, Object variable or with black variable not set"

以下代码出了什么问题?,每次运行它都会出现“运行时错误91,对象变量或黑色变量未设置”

Private Sub Document_Open()

Dim workBook As workBook
Application.ScreenUpdating = True
Set workBook = Workbooks.Open("Z:\Credit_Check_DB.xls", True, True)
txtCompany1.Value = workBook.Worksheets("Sheet2").Range("A1").Formula
txtCompany2.Value = workBook.Worksheets("Sheet2").Range("A1").Formula
txtCityState1.Value = workBook.Worksheets("Sheet2").Range("C1").Formula
txtCityState2.Value = workBook.Worksheets("Sheet2").Range("C1").Formula
txtDate1.Value = workBook.Worksheets("Sheet2").Range("F1").Value
txtAddress1.Value = workBook.Worksheets("Sheet2").Range("B1").Formula
txtZip1.Value = workBook.Worksheets("Sheet2").Range("D1").Formula
txtPO.Value = "Purchase Order#:    " & workBook.Worksheets("Sheet2").Range("I1").Formula
txtRec.Value = workBook.Worksheets("Sheet2").Range("K1").Formula
workBook.Close False
Set workBook = Nothing
Application.ScreenUpdating = True
Close_Excel
End Sub

Private Sub Close_Excel() 'closes excel application.

Dim Excel As Excel.Application
Dim ExcelOpened As Boolean

ExcelOpened = False
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
If Excel Is Nothing Then
    Set Excel = New Excel.Application
    ExcelOpened = True
End If
On Error GoTo 0

With Excel
    If ExcelOpened Then
        .Visible = True
        .Workbooks.Add
    End If
    .ActiveWorkbook.Close False  ***<-***!!!!!Debugger points to here!!!!!******
    .Quit
End With

End Sub

any idea what is wrong with my code? I am basically pulling information from Excel into word.

知道我的代码有什么问题吗?我基本上把Excel中的信息拉到了单词中。

1 个解决方案

#1


0  

Maybe Excel does not point to any Excel application (something went wrong, but you skipped the error), so ActiveWorkbook points to nothing. You should put On Error GoTo 0 immediately after GetObject.

也许Excel没有指向任何Excel应用程序(出错了,但是你跳过了错误),所以ActiveWorkbook指向什么都没有。你应该在GetObject之后立即放置On Error GoTo 0。


推荐阅读
author-avatar
权利阳成_278
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有