Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5464|回复: 10

[已解决]打开只读的excel

[复制链接]
发表于 2012-11-23 22:43 | 显示全部楼层 |阅读模式
怎样用VBA语句打开只读的excel表格,(不想人工点一下只读),求高手。
最佳答案
2012-11-24 21:16
本帖最后由 hwc2ycy 于 2012-11-24 23:38 编辑

Dim itemlist1 As Workbook
on error resume next
Set itemlist1 = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & "east_Iitemlist1.xls", ReadOnly:=True
if itemlist1 is nothing then msgbox "工作簿打开失败":exit sub
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-11-23 22:55 | 显示全部楼层
Workbooks.Open 方法
全部显示
全部隐藏
打开一个工作簿。
语法

表达式.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad)

表达式   一个代表 Workbooks 对象的变量。

ReadOnly 可选 Variant 如果为 True,则以只读模式打开工作簿。
回复

使用道具 举报

发表于 2012-11-23 22:57 | 显示全部楼层
  1. Workbooks.Open Filename:=工作簿名, ReadOnly:=True
复制代码
回复

使用道具 举报

 楼主| 发表于 2012-11-24 21:12 | 显示全部楼层
hwc2ycy 发表于 2012-11-23 22:57

我这样写可以吗?
Dim itemlist1 As Workbook
Set itemlist1 = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & "east_Iitemlist1.xls"), ReadOnly:=True
回复

使用道具 举报

发表于 2012-11-24 21:16 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hwc2ycy 于 2012-11-24 23:38 编辑

Dim itemlist1 As Workbook
on error resume next
Set itemlist1 = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & "east_Iitemlist1.xls", ReadOnly:=True
if itemlist1 is nothing then msgbox "工作簿打开失败":exit sub
回复

使用道具 举报

 楼主| 发表于 2012-11-24 22:48 | 显示全部楼层
hwc2ycy 发表于 2012-11-24 21:16
Dim itemlist1 As Workbook
on error resume next
Set itemlist1 = Workbooks.Open(ThisWorkbook.Path &  ...

最后一句什么意思啊没看明白
回复

使用道具 举报

发表于 2012-11-24 23:39 | 显示全部楼层
写错了,应该是nothing的
检测文件是否打开用的。
回复

使用道具 举报

 楼主| 发表于 2012-11-25 13:14 | 显示全部楼层
hwc2ycy 发表于 2012-11-24 23:39
写错了,应该是nothing的
检测文件是否打开用的。

我应该用不到吧,肯定是关闭的excel,因为别人设密码所以只要能只读打开就好了
回复

使用道具 举报

发表于 2012-11-25 13:46 | 显示全部楼层
写代码,不光是解决眼前的情况。一些未知的意外情况还是考虑周全比较好。
回复

使用道具 举报

 楼主| 发表于 2012-11-25 16:26 | 显示全部楼层
hwc2ycy 发表于 2012-11-25 13:46
写代码,不光是解决眼前的情况。一些未知的意外情况还是考虑周全比较好。

我怎么写对吗?如果对的话在什么情况下会运行到"MsgBox "工作簿打开失败":"这句话
Dim itemlist1 As Workbook
On Error Resume Next
Set itemlist1 = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & "east_Iitemlist.xls", ReadOnly:=True)
If itemlist1 Is Nothing Then
MsgBox "工作簿打开失败":
Exit Sub
End If
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-24 12:11 , Processed in 0.169872 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表