简体   繁体   English

Word.application.Selection.Find.Text打开多个Word应用程序

[英]Word.application.Selection.Find.Text With multiple Word applications open

I'm having a problem where when I use the Word.Application.Selection.Find set of commands, I run into a NullReferenceException . 我在使用Word.Application.Selection.Find命令集时遇到NullReferenceException In this program I initialize a new Word document as such: 在此程序中,我将这样初始化一个新的Word文档:

Dim wrdApp As Word.application = New Word.Application 
Dim wrdDoc As Word.Document = New Word.Document                          
wrdApp.Visible = True

With wrdDoc
    wrdApp.Selection.Find.ClearFormatting()
    wrdApp.Selection.Find.Text = sequenceObject(i, 1, 1, 1).cat
    wrdApp.Selection.Find.Forward = True
    wrdApp.Selection.Find.Wrap = Word.WdFindWrap.wdFindContinue
    wrdApp.Selection.Find.Execute
End With

When I have no windows open prior to the code running, I have no problem. 如果在运行代码之前没有打开任何窗口,则没有问题。 If I have another word document open at the time, the error occurs at the first Find command; 如果我当时打开了另一个Word文档,则在第一个“ Find命令时发生错误; it used to occur at the ClearFormatting line. 它曾经发生在ClearFormatting行。 I commented it out, then it occurred at the Find.Text line. 我将其注释掉,然后发生在Find.Text行。 I have the correct object libraries referenced and imported. 我有正确的对象库引用和导入。

I figured it out, I wasn't actually specifying which document to select. 我知道了,我实际上并没有指定要选择的文档。 By using 通过使用

wrdApp.ActiveDocument.ActiveWindow

I was able to solve the problem. 我能够解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM