C# ASP.NET Open .Docx file that is located on the server on the client

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 16163 Views - Last Post: 20 August 2012 - 02:15 PM Rate Topic: ***** 1 Votes

#1 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

C# ASP.NET Open .Docx file that is located on the server on the client

Posted 01 August 2012 - 03:37 PM

Hello,

I am using C# ASP.NET and I need to be able to open a .Docx file that is located on the server on the client, I was thinking that this could be done with a memory stream or simply just directing Word to the server URL. Saldy I have not gotten anything to work, the code snippet below is my current attempt at resolving the issue.

    void ReadDocument(String fileName)
    {
        // setup the path to the file.
        object template = Server.MapPath("~\\Assets\\" + fileName.Trim());

        Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application();
        object nullobject = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Document outDocument = wordObject.Documents.Open
                    (ref template, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject);

        outdocument.Activate();
        processReport(wordObject);

        //return outDocument;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: C# ASP.NET Open .Docx file that is located on the server on the client

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 01 August 2012 - 04:04 PM

There's really not anything special to do other than provide a link to the file's location.

http://mywebsite.com/document.docx

If the client computer has word installed then it should open it automatically in Word. If they don't have word then their computer won't know what to do with a file with that extension.

This post has been edited by Nakor: 01 August 2012 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 01 August 2012 - 04:21 PM

Thank you for the reply, I guess I should have been slightly more detailed.

I need to be able to do all of this via code so that when word is opened I can manipulate the text of the document. Basically I will be loading a template into the browser, and the replacing variables with values.

View Postsuperken81, on 01 August 2012 - 04:20 PM, said:

Thank you for the reply, I guess I should have been slightly more detailed.

I need to be able to do all of this via code so that when word is opened I can manipulate the text of the document. Basically I will be loading a template into the browser, and the replacing variables with values.


Sorry I meant "I will be loading a template into the Word application" not "browser".
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 01 August 2012 - 09:52 PM

The Open method needs the file path as the first parameter.
Was This Post Helpful? 0
  • +
  • -

#5 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 08:12 AM

View Post_HAWK_, on 01 August 2012 - 09:52 PM, said:

The Open method needs the file path as the first parameter.


Thank you for the reply.

In the code snippet the first parameter of the Open method is the file path.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 12:02 PM

Is the path valid?
Was This Post Helpful? 0
  • +
  • -

#7 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 12:03 PM

View Post_HAWK_, on 02 August 2012 - 12:02 PM, said:

Is the path valid?


Yes the Path is valid, manually typing in the path works fine.. running the code from the server, everything works, but on the client end it cannot find the file.

Thanks again for looking at this issue with me.
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 04:05 PM

Any chance this is a virtual directory? You can use this: Web.VirtualPathUtility.
Was This Post Helpful? 0
  • +
  • -

#9 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 04:20 PM

View Post_HAWK_, on 02 August 2012 - 04:05 PM, said:

Any chance this is a virtual directory? You can use this: Web.VirtualPathUtility.


Hey, _Hawk_

I do not think so, I can access the file by "Http://mydomain.com/Assets/template.docx" without any issues. Maybe I can try making it a virtual drive.. but I would prefer not to have to do that.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 04:38 PM

Did you try "~\Assets\" instead of "~\\Assets\\"?
Was This Post Helpful? 0
  • +
  • -

#11 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 04:41 PM

View Post_HAWK_, on 02 August 2012 - 04:38 PM, said:

Did you try "~\Assets\" instead of "~\\Assets\\"?


"\\" escapes the "\" character, just using "\" in C# causes an "Unrecognized escape sequence" error. Basically "\\" is equal to just "\".
Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 05:01 PM

Right! I don't do C#, I can understand it, I just forget things like that. My browser swaps \ for / have you tried @"~/Assets/".

This post has been edited by _HAWK_: 02 August 2012 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#13 superken81  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 01-August 12

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 05:05 PM

View Post_HAWK_, on 02 August 2012 - 05:01 PM, said:

Right! I don't do C#, I can understand it, I just forget things like that. My browser swaps \ for / have you tried @"~/Assets/".


No I have not tried that, I will give that a go and let you know if it helps at all.

Nope no go using the @

I get the same error message
Was This Post Helpful? 0
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,087
  • Joined: 02-July 08

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 02 August 2012 - 08:26 PM

Code behind is server side so shouldn't Word be loaded on the server for this to work?
Was This Post Helpful? 0
  • +
  • -

#15 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: C# ASP.NET Open .Docx file that is located on the server on the client

Posted 03 August 2012 - 02:23 PM

Hi,

Sorry, I just want to clarify what you are attempting here. Are you trying to do some sort of manipulation of a docx file on the server then send it to the browser? Could you give us a better idea of what you are trying to do? What is happening when you run the code you have now? What does processReport() do?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3