VBA防止在将包对象(XML)读入ADODB流时进行键盘输入? [英] VBA to Prevent Keyboard Input While a Package Object (XML) is Read into ADODB Stream?

查看:95
本文介绍了VBA防止在将包对象(XML)读入ADODB流时进行键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序可以打开并读取以前嵌入在PowerPoint演示文稿中的XML文档或Word文档.为了读取此对象(xmlFile as Object),我必须这样做:

I am developing an application which opens and reads an XML document previously embedded in a PowerPoint presentation, or a Word document. In order to read this object (xmlFile as Object) I have to do:

xmlFile.OLEFormat.DoVerb 1

这将打开包对象,我还有另一个子例程,该子例程获取Notepad.exe的打开实例,并将其内容读取到ADODB流中.

This opens the package object, and I have another subroutine that gets the open instance of Notepad.exe, and reads its contents in to ADODB stream.

此过程的示例可在Google文档中找到:

XML_Test.pptm .

在此过程中,Notepad.exe会在几秒钟内获得焦点,并且无意中的击键可能会导致不良结果或读取XML数据时出错.

During this process there is a few seconds window where the Notepad.exe gains focus, and an inadvertent keystroke may cause undesired results or error reading the XML data.

我正在寻找以下两项之一:

I am looking for one of two things:

  1. 这两种方法都可以防止用户在执行此操作时无意中输入(通过键盘/鼠标/等).最好是诸如以下的MouseKeyboardTest子例程之类的不能控制用户机器的东西.或者,
  2. 将XML数据提取到字符串变量中的更好方法.
  1. Either a method to prevent the user from inadvertently inputting (via keyboard/mouse/etc) while this operation is being performed. Preferably something that does not take control of the user's machine like MouseKeyboardTest subroutine, below. Or,
  2. A better method of extracting the XML data into a string variable.

对于#1::这是我发现的功能,我很乐意使用.我对采取这种对用户系统的控制持谨慎态度. ##我还可以使用其他方法吗?##

For #1: this is the function that I found, which I am leery of using. I am wary of taking this sort of control of the users system. ##Are there any other methods that I might use?##

Private Declare Function BlockInput Lib "USER32.dll" (ByVal fBlockIt As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub MouseKeyboardTest() 'both keyboard and mouse blocked

    BlockInput True ' Turns off Keyboard and Mouse
'   Routine goes here
    Sleep 5000 ' Optional coding
    BlockInput False ' Turns on Keyboard and Mouse

End Sub

对于#2::有一些背景知识,但问题似乎是无法使用DoVerb 1以外的任何方法可靠地提取嵌入式对象.由于我正在处理的应用程序(记事本)中未保存的文档不受我的VBA技能的影响,因此这似乎是唯一的方法.完整背景,在这里:

For #2: Some background, but the issue seems to be the inability to extract the embedded object reliably using any method other than DoVerb 1. Since I am dealing with an unsaved document in an application (Notepad) that is immune to my VBA skillz, this seems to be the only way to do this. Full background on that, here:

从PowerPoint VBA中提取OLEObject(XML文档)

推荐答案

我的理解是,您首先控制了如何将XML文件嵌入到PowerPoint演示文稿中.在这里,我不太了解为什么您选择将所需的数据保留为嵌入式对象的内容.

My understanding is that you have control over how XML file gets embedded into PowerPoint presentation in the first place. Here I do not quite understand why you chose to keep the data you need as contents of an embedded object.

可以肯定的是,取回这些内容并不是一件容易的事.实际上,只要没有(简单甚至中等难度)的方法来调用VBA中的QueryInterface和使用IPersist*接口,只有一种方法可以获取嵌入式对象的内容.该方法涉及以下步骤:

To be sure, the task of getting those contents back is not a piece of cake. Actually, as long as there is no (simple or even moderately difficult) way to call QueryInterface and use IPersist* interfaces from VBA, there is just one way to get to contents of embedded object. The way involves following steps:

  1. 激活嵌入式对象.您为此使用了OLEFormat.DoVerb 1.更好的方法是调用OLEFormat.Activate,但这与您的特定问题无关.
  2. 使用嵌入式对象的编程模型来执行有用的操作,例如获取内容,保存或公开任何内容. Notepad.exe没有公开这样的编程模型,因此您求助于WinAPI这是最佳选择.
  1. Activate an embedded object. You used OLEFormat.DoVerb 1 for that. A better way would be to call OLEFormat.Activate, but this is irrelevant for your particular problem.
  2. Use embedded object's programming model to perform useful operations like getting contents, saving or whatever is exposed. Notepad.exe exposes no such programming model, and you resorted to WinAPI which is the best choice available.

不幸的是,您当前的方法至少有2个缺陷:

Unfortunately, your current approach has at least 2 flaws:

  1. 您在问题中标识的人(激活notepad.exe可能导致用户干扰).
  2. 如果用户具有用于打开除notepad.exe以外的.txt文件的默认程序,则您的方法注定要失败.
  1. The one you identified in the question (activation of notepad.exe leading to possibility of user's interference).
  2. If a user has default program for opening .txt files other than notepad.exe, your approach is doomed.

如果您确实控制了如何创建嵌入式对象,那么更好的方法是将XML数据存储在Shape对象的某些属性中.我会使用 Shape.AlternativeText (非常简单易用;如果您将.pptm导出为HTML或在某些与AlternativeText息息相关的不同情况下使用)或

If you do have control over how embedded object is created then better approach would be to store your XML data in some property of Shape object. I would use Shape.AlternativeText (very straightforward to use; shouldn't be used if you export your .pptm to HTML or have some different scenario where AlternativeText matters) or Shape.Tags (this one is probably the most semantically correct for the task) for that.

这篇关于VBA防止在将包对象(XML)读入ADODB流时进行键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆