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

使用VBScript以独占模式打开Excel文件-OpenanExcelfileinexclusivemodeusingVBScript

Ihaveasimplequestion,butIvesearchedforthisandcouldntfindanyhelpfultopics..我有一个简单的问

I have a simple question, but I've searched for this and couldn't find any helpful topics..

我有一个简单的问题,但我搜索了这个,找不到任何有用的主题..

I'm working on a Vbscript that opens an Excel file and modify a few stuff in it.. so I'm using this code:

我正在开发一个Vbscript,它打开一个Excel文件并修改其中的一些内容..所以我使用这段代码:

    Set objXLApp = CreateObject("Excel.Application")

    objXLApp.Visible = False
    objXLApp.DisplayAlerts = False

    Set objXLWb = objXLApp.Workbooks.Open(FilePath)

Now, what I want to do is to open the Excel file using a way that locks the file and prevents the user from opening it while it's open by the script (until it's closed).

现在,我想要做的是使用锁定文件的方式打开Excel文件,并防止用户在脚本打开时打开它(直到它关闭)。

Update:

更新:

I think the problem is somehow related to the Excel instances, I tried to do the following (while the file is open by the script):

我认为问题与Excel实例有某种关系,我尝试执行以下操作(当文件由脚本打开时):

  • When I manually open the file (while it's open by the script) they're both become a single instance.
  • 当我手动打开文件(当它由脚本打开时),它们都成为一个单独的实例。
  • When I open any other Excel file they're both also become a single instance!!! And the original file (opened by the script) becomes visible!
  • 当我打开任何其他Excel文件时,它们也都变成了单个实例!并且原始文件(由脚本打开)变得可见!

Now this is weird because I'm using CreateObject("Excel.Application") and not GetObject(, "Excel.Application")

现在这很奇怪,因为我使用的是CreateObject(“Excel.Application”)而不是GetObject(,“Excel.Application”)

2 个解决方案

#1


2  

There is registry key HKEY_CLASSES_ROOT\Excel.Sheet.8\shell\Open\command on Win 7 Excel 2010 for me with default value "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" /dde. The command line /dde switch enables DDE (Dynamic Data Exchange mechanism - an ancient Win 3.0 interprocess communication method) that forces Excel to start in a single instance. I've tried to remove that switch and opened workbooks, but to no avail. BTW, if you don't have a permission to edit the registry, or you intend to distribute your script to someone who doesn't, that is not a way. Also have tried this answer, but it doesn't work for Win 7 Office 2010.

在Win 7 Excel 2010上有我的注册表项HKEY_CLASSES_ROOT \ Excel.Sheet.8 \ shell \ Open \命令,默认值为“C:\ Program Files \ Microsoft Office \ Office14 \ EXCEL.EXE”/ dde。命令行/ dde开关启用DDE(动态数据交换机制 - 一种古老的Win 3.0进程间通信方法),它强制Excel在单个实例中启动。我试图删除该开关并打开工作簿,但无济于事。顺便说一句,如果您没有编辑注册表的权限,或者您打算将脚本分发给没有编辑注册表的人,那么这不是一种方法。也试过这个答案,但它不适用于Win 7 Office 2010。

I've tested test.xlsm file with DDE enabled. When user opens a file, actually it is just reopened in existing instance that make it visible. If any changes has been already made by the script, then Excel alerts:

我已经在启用了DDE的情况下测试了test.xlsm文件。当用户打开文件时,实际上它只是在现有实例中重新打开,使其可见。如果脚本已经进行了任何更改,则Excel会发出警报:

changes to be discarded

Anyway write-access is given for the user. After that when the script saves the file, another alert appears:

无论如何,为用户提供了写访问权限。之后,当脚本保存文件时,会出现另一个警报:

file already exists

Some time ago I created a script that worked with Excel application, and encountered the same issue with Win 7 Excel 2010 as you are describing. I noticed that if there were several Excel application instances created with CreateObject() within script, then Excel file opened by user always used exactly the first created instance. I've solved the issue by creating two invisible instances of Excel application, let's say dummy and target. In outline the algorithm for a script is as follows:

前段时间我创建了一个与Excel应用程序一起使用的脚本,并且遇到了与您描述的Win 7 Excel 2010相同的问题。我注意到如果在脚本中使用CreateObject()创建了多个Excel应用程序实例,则用户打开的Excel文件始终使用的是第一个创建的实例。我已经通过创建两个不可见的Excel应用程序实例解决了这个问题,让我们说假人和目标。概括地说,脚本的算法如下:

  1. Create dummy instance first, no need to add a workbook. After that the dummy instance is exposured an Excel file to be opened by user within it.
  2. 首先创建虚拟实例,无需添加工作簿。之后,虚拟实例将暴露一个Excel文件,由用户在其中打开。
  3. Create target instance.
  4. 创建目标实例。
  5. Quit dummy instance.
  6. 退出虚拟实例。
  7. Open target workbook, modify and save it.
  8. 打开目标工作簿,修改并保存。
  9. Quit target instance.
  10. 退出目标实例。

Consider the below code that illustrates a possible way to implement what you need:

请考虑以下代码,该代码说明了实现所需内容的可能方法:

' target file path
sPath = "C:\Users\DELL\Desktop\test.xlsm"
' create dummy instance
Set oExcelAppDummy = CreateObject("Excel.Application")
' create target instance
Set oExcelApp = CreateObject("Excel.Application")
' quit dummy instance
oExcelAppDummy.Quit
' open target workbook
With oExcelApp
    .Visible = False
    .DisplayAlerts = False
    Set oWB = .Workbooks.Open(sPath)
End With
' make some changes and save
Set oWS = oWB.Sheets(1)
oWS.Cells(1, 1).Value = Now()
oWB.Save
' give additional time for test
MsgBox "Try to open test.xlsm, OK to end the script"
' close target workbook
oWB.Close
' quit target instance
oExcelApp.Quit

Trying open the file you will get desired output:

尝试打开文件,您将获得所需的输出:

open read-only and notify

And the notification after the script ends:

脚本结束后的通知:

open read-write

#2


2  

That is strange that you aren't getting a message as below:

奇怪的是你没有得到如下信息:

enter image description here

One possible method would be

一种可能的方法是

  • to change the file attributes at the start and end of the code, the version below makes the file readonly and hidden
  • 要更改代码开头和结尾的文件属性,下面的版本会使文件只读和隐藏
  • make your changes
  • 做出你的改变
  • save the file with a different name
  • 使用其他名称保存文件
  • change the attributes back
  • 更改属性
  • rename the changed file to the original name
  • 将更改的文件重命名为原始名称

code

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objXLApp = CreateObject("Excel.Application")

filePath = "C:\Temp\MyFile.xlsm"
filePath2 = "C:\Temp\MyFile1.xlsm"

set objFile = objFSO.GetFile(filePath)
objFile.Attributes = 3

objXLApp.Visible = False
objXLApp.DisplayAlerts = False

Set objxlWB = objXLApp.Workbooks.Open(filePath)
'do stuff
objxlWB.saveas filePath2
objxlWB.Close
objXLApp.Quit
set objXLApp = Nothing

objFile.Attributes = 32
objFile.Delete
objFSO.MoveFile filePath2, filePath

推荐阅读
  • Iamtryingtocreateanarrayofstructinstanceslikethis:我试图创建一个这样的struct实例数组:letinstallers: ... [详细]
  • 本文介绍了某点评网的搜索策略,包括名称和地址的匹配策略,模糊匹配的方法以及不同口音和拼音的近似发音。同时提供了一些例子来说明这些策略的应用。 ... [详细]
  • 关于打印机的问题,在网上找了好久都找不到自动打印的代码!!!现在我把我找到的发布出来共享一下(这个是不会弹出打印提示的,直接打印的;但浏览器但设置一下)页面上写上:<objectidWebB ... [详细]
  • 特需要使用集合的时候,无法找到VBScript中的Collection对象;到处找不到,那就自己写一个吧!注:1.需要VBScript5.0或更高版本,使用Class及 ... [详细]
  • <tablecellspacing0cellpadding0>&l ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • python3 nmap函数简介及使用方法
    本文介绍了python3 nmap函数的简介及使用方法,python-nmap是一个使用nmap进行端口扫描的python库,它可以生成nmap扫描报告,并帮助系统管理员进行自动化扫描任务和生成报告。同时,它也支持nmap脚本输出。文章详细介绍了python-nmap的几个py文件的功能和用途,包括__init__.py、nmap.py和test.py。__init__.py主要导入基本信息,nmap.py用于调用nmap的功能进行扫描,test.py用于测试是否可以利用nmap的扫描功能。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
author-avatar
军军CJJ_317
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有