'应用'不包含'选择'的定义

时间:2016-07-20 08:52:20

标签: c# ms-word

我很遗憾不明白如何获得选择。我检查了msdn,从我可以告诉我可能缺少一个引用或其他东西。代码本身是在填写表单框时替换模板中找到的单词。非常简单直接。

@include('../settings/config.php');
if (!@include('../settings/config.php')) {
    die("<center>Login Failed</center>");
}

//======================================================================
// POST Check(Isset submit comes from html form)
//======================================================================
if(isset($_POST['submit'])) {
// Sanitize All POST Fields
$_POST = array_map('trim', $_POST);
$_POST = array_map('strip_tags',$_POST);

$login_form_user = $_POST['login_user'];
$login_form_pass = $_POST['login_pass'];
// Testing Only
echo("
<center>
Your Username is: $login_form_user!
<br>
Your Password is: $login_form_pass!
</center>
");
//======================================================================
// Input/Database Check
//======================================================================
$user_fetch = <<<LOGIN
SELECT `id` FROM `users` 
WHERE `username`='$login_form_user' 
AND `password`='$login_form_pass' 
LIMIT 1
LOGIN;

$user_result = $sql_connection->query($user_fetch);
if(!$user_result) {
    die("<center>Cannot Execute SQL Login Query</center>");
}

if ($sql_connection->num_rows == 1) {
echo("<center>User $login_form_user Exists</center>");
}


 if($row = $user_result->fetch_assoc()) {
 if(($row['username'] === $login_form_user) && ($row['password'] === $login_form_pass)) {
// Login Is Successful
echo("<center>Login Successful</center>");
} else {
echo("<center>Login Failed</center>");
}
}

} else {
// No Direct File Access Allowed
unset($_POST);
die('No Direct File Access Allowed!');
}
?>

2 个答案:

答案 0 :(得分:0)

Application是一个类名(尽管在这种情况下你有别名,所以正确的类名是Word.Application)。

Selection是此类的实例属性,因此只能通过实例访问它。您有一个实例objWord。如果您在表单中将其声明为实例字段,则可以通过按钮单击处理程序访问它:

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;
        // ...
    }

另外,从C#4开始,ref和所有参数对于COM Interop都是可选的。所以这一行:

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);

可以写成如下,使用可选和命名参数:

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

答案 1 :(得分:0)

您需要将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);
    }
}