c# - 创建并打开word文档并将其置于最前面

标签 c# ms-word add-in office-interop visual-studio-addins

我正在用 C# 创建一个 Word 加载项,我正在创建一个文档,插入一个超链接并尝试将其置于最前面。但新创建的文档不会排在前面。它转到当前窗口的后面。下面是代码:

Word.Document newNoteDoc = new Word.Document();
        object oActiveDocFullName = Globals.ThisAddIn.Application.ActiveDocument.FullName;
        object oAcitiveDocNameWithoutExt = Path.GetFileNameWithoutExtension(Globals.ThisAddIn.Application.ActiveDocument.Name);
        object oActiveDocName = Globals.ThisAddIn.Application.ActiveDocument.Name;
        object oScreenTip = (GeneralHelper.pstrNoteFolder + "\\" + txtFileName.Text.Trim() + ".docx").Replace('\\', '/');
        object oScreemTipInNewDoc = Globals.ThisAddIn.Application.ActiveDocument.FullName.Replace('\\', '/');
        object oMissingReference = System.Reflection.Missing.Value;
        object oFileName = "file:///" + GeneralHelper.pstrNoteFolder + "/" + txtFileName.Text.Trim() + ".docx";
        object oHyperlinkText = txtHyperlinkText.Text.Trim();
        object oFileFormat = Word.WdSaveFormat.wdFormatDocumentDefault;
        object oAddToRecentFiles = true;
        object oReadOnlyRecommended = false;
        object oStartRange = 0;
        object oVisible = true;
        //  OopenFileName = oFileName;
        Word.Range rngCurrentRange = Globals.ThisAddIn.Application.Selection.Range;

        try
        {
            //Word.Document maindoc = Globals.ThisAddIn.Application.ActiveDocument;


            //insert hyperlink into current document
            rngCurrentRange.Hyperlinks.Add(rngCurrentRange, ref oFileName, ref oMissingReference, ref oScreenTip, ref oHyperlinkText, ref oFileName);
            Word.Range rngNewRange = newNoteDoc.Range(ref oStartRange, ref  oMissingReference);

            //insert hyperlink in the new doc.
            //Globals.ThisAddIn.Application.ActiveDocument.Activate();

            newNoteDoc.Hyperlinks.Add(rngNewRange, ref oActiveDocFullName, ref oMissingReference, ref oScreemTipInNewDoc, oAcitiveDocNameWithoutExt, oActiveDocFullName);
            //save the new doc

            newNoteDoc.SaveAs2(ref oFileName, ref oFileFormat, ref oMissingReference, ref oMissingReference, ref oAddToRecentFiles, ref oMissingReference,
                       ref oReadOnlyRecommended, ref oMissingReference, ref oMissingReference, ref oMissingReference, ref oMissingReference,
                       ref oMissingReference, ref oMissingReference, ref oMissingReference, ref oMissingReference, ref oMissingReference, ref oMissingReference);

            //Globals.ThisAddIn.AddTaskPane(newNoteDoc);

            //Application.DoEvents();

            ////newNoteDoc.Application.WindowState=1;
            //newNoteDoc.Activate();
            //newNoteDoc.Windows.Application.Visible = true;
            ////newNoteDoc.Activate();


            //Thread.Sleep(5000);

            //newNoteDoc.Application.Activate();


            //newNoteDoc.Application.WindowState = Word.WdWindowState.wdWindowStateMinimize;
            //newNoteDoc.Application.WindowState = Word.WdWindowState.wdWindowStateMaximize;

            newNoteDoc.Close(ref oMissingReference, ref oMissingReference, ref oMissingReference);
object readOnly = false;
            object isVisible = true;
            object oMissing = System.Reflection.Missing.Value;

            //Make word visible, so you can see what's happening         
            //Globals.ThisAddIn.Application.Visible = true;

            Word.Document NewDoc = Globals.ThisAddIn.Application.Documents.Open(ref oFileName, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible);


            NewDoc.Activate();

当前文档仍然处于事件状态。如何激活新创建的文档并将其置于最前面。

最佳答案

尝试使用 DocumentChangeEvent(),然后尝试打开新文件。它应该自动激活它。

关于c# - 创建并打开word文档并将其置于最前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12727766/

相关文章:

add-in - 从 Excel 公式中的 UDF 中删除加载项路径

c# - 使用交换 Web 服务来获取日历项所需的参与者? C#

c# - 表单提交后启用文本框和按钮 ASP.NET MVC 4

vba - 将目录从 Word 导出到 Excel

html - 将 doc/docx 转换为语义 HTML

.net - 以编程方式将页脚添加到 Office Word/Excel 文档

C# - 无法调试 office word 加载项

c# - 在不使用循环的情况下在 ObservableCollection 中查找项目

c# - 关闭应用程序

c# - ASP.NET 中的布局问题 GridView