Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Open word document in read only mode using asp.net c#

Posted on 2007-12-04
14
1,508 Views
Last Modified: 2008-02-01
Does anyone have any sample code opening a word document using word automation in c# and asp.net.  I found examples of creating new documents, but no examples of opening existing documents in read only mode.

Thanks...
0
Comment
Question by:michael1174
  • 6
  • 5
  • 3
14 Comments
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 250 total points
ID: 20402626
private Word.ApplicationClass WordApp = new Word.ApplicationClass();

object fileName = myFileName;
object readOnly = true;

object isVisible = true;
object missing = System.Reflection.Missing.Value;
WordApp.Visible = true;

Word.Document aDoc = WordApp.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);

aDoc.Activate();
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 20402635
other nice sample you can find here: http://www.codeproject.com/KB/aspnet/wordapplication.aspx
0
 
LVL 2

Author Comment

by:michael1174
ID: 20402667
junges, I get the following error;

The type or namespace name 'Word' could not be found (are you missing a using directive or an assembly reference?)

I added the microsoft office com library to my reference list... do I need to add a using reference line?

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Expert Comment

by:stevemarshall
ID: 20402811
You could just change the file that you are opening to be read only using the

File.SetAttributes( YourFilePath, FileAttributes.ReadOnly );

method.
0
 
LVL 2

Author Comment

by:michael1174
ID: 20402843
stevemarshall, what reference do I need to specify to use File?

so, I could technically do something like this:

File.SetAttributes( "file://uncpath/" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc", FileAttributes.ReadOnly );

Response.Write("<script>window.open('file://uncpath/" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc')</script>");
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 20402864
dont forget to add the references.

if you are using Word2003 then use this namespace Microsoft.Office.Interop.Word
and the open function muss have 16 parameters. just add "ref missing" for the others.
0
 
LVL 2

Author Comment

by:michael1174
ID: 20402884
junges, i'm stuck on word 97 right now.. haha, but I was able to add the word object reference, the only thing now is i get a problem with the path I am specifiying... what would be the correct unc path name?

the error I get:
The document name or path is not valid. Try one or more of the following:

my unc path:

//server001/vol1/SHARDATA/Admiral Forms/doument.doc

0
 

Expert Comment

by:stevemarshall
ID: 20402896
It's in the System.IO namespace, and I think you just need to have the System.dll referenced.
0
 
LVL 2

Author Comment

by:michael1174
ID: 20403038
can someone help me correctly define my document path...

object fileName = "\\server\vol1\SHARDATA\" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc";

I keep getting:

; expected

and

Newline in constant


0
 

Expert Comment

by:stevemarshall
ID: 20403177
The '\' character is the escape character, you should either use \\ i.e. escape the escape character or prefix the string with a @ symbol.

So your code above would become:

object fileName = @"\\server\vol1\SHARDATA\" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc";
0
 
LVL 2

Author Comment

by:michael1174
ID: 20403595
thanks steve.. still getting the

The document name or path is not valid.

I've tried these two... and when I step through the code, fileName is correct, and I can access it through windows explorer

object fileName = @"\\server\vol1\SHARDATA\ADMIRAL\Admiral Forms\" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc";

object fileName = "\\\server\\vol1\\SHARDATA\\ADMIRAL\\Admiral Forms\\" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc";


 Here is all my code:
Word.ApplicationClass WordApp = new Word.ApplicationClass();
 
object fileName = @"\\server\vol1\SHARDATA\ADMIRAL\Admiral Forms\" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc";
			
object fileName = "\\\server\\vol1\\SHARDATA\\ADMIRAL\\Admiral Forms\\" + e.Item.Cells[1].Text + e.Item.Cells[3].Text + ".doc";
 
object readOnly = true;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
WordApp.Visible = true;
 
Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
 
aDoc.Activate();

Open in new window

0
 

Expert Comment

by:stevemarshall
ID: 20403869
Just comparing your code to mine, firstly I create a FileInfo object then use the FullName property rather than creating a path by hand.

Secondly I use Type.Missing rather than System.Reflection.Missing.Value.

To check that your path is valid you could use:

File.Exists( fileName.ToString() );

It returns a boolean and at least then you will know if it's a problem with your unc path or with Word automation.
0
 
LVL 2

Author Comment

by:michael1174
ID: 20404053
steve,
when I do a:

? fileName.ToString()
@"\\server\vol1\SHARDATA\ADMIRAL\Admiral Forms\AI00121.doc"

I copy and past inbetween the quotes into my windows explorer, and I open the file, no problem.. but I used you code as you suggested below and it skipped it.


			if (File.Exists(fileName.ToString()))
			{
				Word.Document oWordDoc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
				oWordDoc.Activate();
			}

Open in new window

0
 

Assisted Solution

by:stevemarshall
stevemarshall earned 250 total points
ID: 20404735
This is a permissions problem.

Open your web.config and make sure that you have the line

<identity impersonate="true"/>

You should also check that the permission that is configured for anonymous access in IIS is set to a user who has access to the share.

It'll be worth reading up on the pitfalls surrounding impersonation before you do this in a live environment.

http://msdn2.microsoft.com/en-us/library/aa719560(VS.71).aspx  - might be a good place to start.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 49
Please explain purpose of GZIP 4 34
reading excel file in .net 2 15
C# Linq Select From List 3 10
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question