我已经找过了,但没有找到解决我的问题的方法。我已经为Word2007开发了一个插件功能区,它提供了一组额外的加载和保存功能,允许用户从定制系统加载和保存文档。
我已经完成了大部分工作--当用户请求打开一个文件时,它会被下载并保存到AppData文件夹,然后再打开。然而,我遇到的问题是,例如,如果用户打开Word并使用这个新的“加载”功能,空白的Word文档仍然存在,Word非常愉快地打开新文档,但它没有获得焦点。
(我使用的是Windows7,它会在任务栏中为新文档创建第二个“W”图标,但它不会像我使用普通的“打开”路线时Word那样切换到这个图标。)
我已经尝试过(根据这里其他地方的建议)将'visible‘属性设置为true,然后调用doc.Activate(),但都不需要这样做。我遗漏了什么?我用来打开这个文件的代码如下:
private void OK_Click(object sender, EventArgs e)
{
this.Close();
FES.FESServices wService = new FES.FESServices();
int request_id = wService.SubmitRequestFromAddIn(username, password, "RETR", "", textBox1.Text, "", "");
FES.FileRequestResponse response = wService.GetFileMembersFromAddIn(username, password, request_id);
if (response.ResponseType == "RETR")
{
byte[] data = wService.GetBytesForFilename(response.ResponseValue);
//MessageBox.Show("Loaded data for file...");
//MessageBox.Show(Application.UserAppDataPath);
FileStream fs = new FileStream(Application.UserAppDataPath + "\\" + response.ResponseValue.Substring(6).Split('~')[0], FileMode.Create, FileAccess.Write);
fs.Write(data, 0, (int)data.Length);
fs.Close();
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.Documents.Open(
Application.UserAppDataPath + "\\" + response.ResponseValue.Substring(6).Split('~')[0], Visible:true
);
doc.Activate();
}
}(我包含了this.Close(),因为加载文档的函数保存在模式对话框中,并且在没有先关闭它的情况下,Word会在打开对话框的情况下切换文档时抛出异常)。
感谢您的帮助!
发布于 2013-02-12 07:31:44
在显示模式对话框时运行此代码会干扰窗口激活。
我不确定这种干扰的确切机制是什么,但修复方法很简单。将代码移到对话框外部。在ShowDialog调用返回后立即执行此代码。
https://stackoverflow.com/questions/14822070
复制相似问题