Open a Word document in which the file name is user input
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Open a Word document in which the file name is user input

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    37

    Open a Word document in which the file name is user input

    I am trying to have the user input a filename with extension, and then have a c# program open that file in MS Word 2010. However, I cannot seem to figure it out. Any help? I am running Visual Studio 2010 and have a console application.

    Here is the code so far...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using word = Microsoft.Office.Interop.Word;
    using System.IO;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    System.Console.Write("Enter the file name: ");
    String fileName = System.Console.ReadLine();

    FileStream fileStream = new FileStream(@"H:\SNH\" + fileName, FileMode.Open);

    }
    }
    }

    However, it seems like it runs but does nothing...

  2. #2
    Join Date
    May 2011
    Posts
    18

    Re: Open a Word document in which the file name is user input

    when you open a Filestream, you are essentially just reading the file bytes into memory.

    if you want to actually open the file and have it display, the "Microsoft.Office.Interop.Word" libraries should have an "Open" function that you can supply the filename and have it actually open the file

  3. #3
    Join Date
    Nov 2011
    Posts
    37

    Re: Open a Word document in which the file name is user input

    Ok, so I took out the FileStream, but now I have another error. I have made a Word AddIn form with the below code. When I go to run it, it populates an error saying the file does not exist...

    It's like it does not recognize the user input part of the code. Any idea if I need to use different code to collect the file name from the user?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Word;
    using System.IO;


    namespace WordAddIn2
    {
    public partial class ThisAddIn
    {
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    System.Console.Write("Enter the file name: ");
    String fileName = System.Console.ReadLine();

    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Word._Document oDoc;
    oDoc = oWord.Documents.Open(@"H:\SNH\" + fileName);
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
    this.Startup += new System.EventHandler(ThisAddIn_Startup);
    this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center