简体   繁体   English

使用OpenFIleDialog在C#中获取word文档的路径

[英]Using OpenFIleDialog to get path for word document in C#

I'm trying to use OpendFileDialog to get the path to be passed to word application instance. 我正在尝试使用OpendFileDialog来获取传递给word应用程序实例的路径。 This is my code so far. 到目前为止这是我的代码。 It is telling me 'Sorry, we couldn't find your file. 它告诉我'抱歉,我们找不到您的文件。 Was it moved, renamed, or deleted? 是移动,重命名还是删除?

This is my code. 这是我的代码。 Thanks. 谢谢。

OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog1.Filter = @"All Files|*.*";

        if (openFile.ShowDialog() == DialogResult.OK)
        {
            string filePath = System.IO.Path.GetFullPath(OpenFileDialog1.FileName);
        }

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document wordDoc = null;       

        object fileName = "filePath";

        object missing = System.Type.Missing;
        document = App.Documents.Open(ref fileName, ref missing, ref missing, ref missing, 
                                  ref missing, ref missing, ref missing, ref missing, 
                                  ref missing, ref missing, ref missing, ref missing,
                                  ref missing, ref missing, ref missing, ref missing);

Your currently setting your fileName to "filePath" as a string not to the actual path. 您当前将fileName设置为"filePath"作为字符串而不是实际路径。 You will notice in your code you are setting your filePath variable within the if scope which is only scoped to the if statement. 您将在代码中注意到,您在if范围内设置了filePath变量,该范围仅限于if语句。

OpenFileDialog OpenFileDialog1 = new OpenFileDialog();

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog1.Filter = @"All Files|*.*";
    object fileName = "";
    if (openFile.ShowDialog() == DialogResult.OK)
    {
        fileName = System.IO.Path.GetFullPath(OpenFileDialog1.FileName);
    }

    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document wordDoc = null;       



    object missing = System.Type.Missing;
    document = App.Documents.Open(ref fileName, ref missing, ref missing, ref missing, 
                              ref missing, ref missing, ref missing, ref missing, 
                              ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing);

You will notice the change is moving the fileName assignment before the if scope and then setting the fileName value to the File from the OpenFileDialog. 您将注意到更改是在if范围之前移动fileName分配,然后从OpenFileDialog将fileName值设置为File。 This is then passed to the interop methods. 然后将其传递给互操作方法。 My suggestion will be to set a breakpoint on the object fileName = "" line and check your variable assignments. 我的建议是在object fileName = ""行上设置一个断点并检查你的变量赋值。

I will add that you should be changing your code it ensure if the result of your OpenFile dialog IS NOT OK then you should add a return statement or through another pattern to ensure you are not executing code that will fail as the user has cancelled or closed the dialog. 我将补充说你应该更改你的代码,它确保如果OpenFile对话框的结果不正确,那么你应该添加一个return语句或通过另一个模式来确保你没有执行因用户取消或关闭而失败的代码对话框。

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

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