简体   繁体   English

“应用程序”不包含“选择”的定义

[英]'Application' does not contain a definition for 'Selection'

I unfortunately do not understand how to get Selection. 不幸的是我不明白如何获得选择。 I checked msdn and from what I can tell I'm probably missing a reference or something.The code itself is to replace words found in a template when you fill out a forms box. 我检查了msdn并发现我可能缺少参考文献或其他内容。代码本身是在填写表单框时替换在模板中找到的单词。 Pretty simple and straight forward. 非常简单直接。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace WindowsFormsApplication2
 {
 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();

        Word.Application objWord = new Word.Application();
        objWord.Visible = true;
        object isVisible = true;
        object readOnly = false;
        Word.Document objDoc;
        object Missing = System.Reflection.Missing.Value;
        object fileName = @"C:\Users\jason\Documents\Custom Office Templates\MLA1.dotx";
        objDoc = objWord.Documents.Open(ref fileName, ref Missing, ref  readOnly, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref isVisible, ref Missing, ref Missing, ref Missing, ref Missing);

    }


    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }


    private void SearchReplace()
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {



        Word.Find findObject = Application.Selection.Find;
        findObject.ClearFormatting();
        findObject.Text = "Name";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox1.Text;

        object replaceAll = Word.WdReplace.wdReplaceAll;
        object missing = System.Reflection.Missing.Value;
        findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref replaceAll, ref missing, ref missing, ref missing, ref missing);

     }
     }
     } 

Application is a class name (although you have aliased in this case, so the correct class name is Word.Application ). Application是一个类名(尽管在这种情况下您已使用别名,所以正确的类名是Word.Application )。

Selection is an instance property of this class, so it can only be accessed via an instance. Selection是此类的实例属性,因此只能通过实例进行访问。 You have an instance, objWord . 您有一个实例objWord If you declared this as an instance field in your form, then you could access it from your button click handler: 如果您在表单中将此实例声明为实例字段,则可以通过按钮单击处理程序访问它:

public partial class Form1 : Form
{
    private Word.Application objWord;

    public Form1()
    {
        InitializeComponent();
        objWord = new Word.Application();
        // ...
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Word.Find findObject = objWord.Selection.Find;
        // ...
    }

As an aside, as of C# 4 the ref and all the parameters are optional for COM Interop. 顺便说一句,从C#4开始,对于COM Interop, ref和所有参数都是可选的。 So this line: 所以这行:

objWord.Documents.Open(ref fileName, ref Missing, ref  readOnly, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref isVisible, ref Missing, ref Missing, ref Missing, ref Missing);

Can be written as below, making use of optional and named arguments: 可以使用可选和命名参数编写如下:

objWord.Documents.Open(fileName, ReadOnly: readOnly, IsVisible: isVisible);

You need to store the Word instance in a field of your Form1 class so that you can later on access it in the button event handler: 您需要将Word实例存储在Form1类的字段中,以便以后可以在按钮事件处理程序中对其进行访问:

public class Form1 : Form
{
    private Word.Application _wordApplication;

    public Form1()
    {
        InitializeComponent();

        _wordApplication = new Word.Application();
        _wordApplication.Visible = true;

        var fileName = @"C:\Users\jason\Documents\Custom Office Templates\MLA1.dotx";
        var document = _wordApplication.Documents.Open(fileName, ReadOnly:false, Visible: true);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var findObject = _wordApplication.Selection.Find;
        findObject.ClearFormatting();
        findObject.Text = "Name";
        findObject.Replacement.ClearFormatting();
        findObject.Replacement.Text = textBox1.Text;

        object replaceAll = Word.WdReplace.wdReplaceAll;
        object missing = System.Reflection.Missing.Value;
        findObject.Execute(Replace: Word.WdReplace.wdReplaceAll);
    }
}

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

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