Open word doc from asp.net app?

  • Thread starter Thread starter CK
  • Start date Start date
C

CK

Sorry for x-posting. I am really not sure where to post this question. Is it
possible to open a Word document from a asp.net page? I need to pass
parameters from a web form to a word document. Then inside Word I would need
to use those parameters. Also I want to make a new document from an
exisiting template. Does anyone have any examples of this?

Thanks in Advance,
~CK
 
CK,

The only way I know of to do this is to have a COM Reference to the Word
object model on the server, and use the Word Object model's methods to open
Word and manipulate / save documents.

This is fraught with complications since Word was never designed to be a
Web-server - side COM server. In particular, you may find it very difficult
to get all the COM Interop references released with the result usually being
123 instances of Word.EXE clogging up your machine, which can be all seen in
Task Mgr.

Several vendors produce server-safe .NET assemblies for doing this, one that
I have seen is the Aspose product.

Peter
 
CK said:
Sorry for x-posting. I am really not sure where to post this question. Is it
possible to open a Word document from a asp.net page? I need to pass
parameters from a web form to a word document. Then inside Word I would need
to use those parameters. Also I want to make a new document from an
exisiting template. Does anyone have any examples of this?

Thanks in Advance,
~CK
You would need to use word automation to achieve this.
You would also need read/write permissions on the file server.

Check out the google search below:
http://www.google.com.au/search?cli...q=word+automation+c#&meta=&btnG=Google+Search

JB
 
| CK wrote:
| > Sorry for x-posting. I am really not sure where to post this question.
Is it
| > possible to open a Word document from a asp.net page? I need to pass
| > parameters from a web form to a word document. Then inside Word I would
need
| > to use those parameters. Also I want to make a new document from an
| > exisiting template. Does anyone have any examples of this?
| >
| > Thanks in Advance,
| > ~CK
| >
| >
| >
| You would need to use word automation to achieve this.
| You would also need read/write permissions on the file server.
|
| Check out the google search below:
|
http://www.google.com.au/search?cli...q=word+automation+c#&meta=&btnG=Google+Search
|
| JB

Such articles gives you the false impression that you can use Office in a
server context like ASP, which is not the case. Such articles should start
by referring to the Q257757 KB article, those brave souls who still think
they want Office server-side should keep in mind that it only talks about
ASP, ASP.NET is even more problematic because of the non deterministic
nature of the GC/Finalizer.

Willy.
 
Such articles gives you the false impression that you can use Office in a
server context like ASP, which is not the case. Such articles should start
by referring to the Q257757 KB article, those brave souls who still think
they want Office server-side should keep in mind that it only talks about
ASP, ASP.NET is even more problematic because of the non deterministic
nature of the GC/Finalizer.

Willy, isn't that one of the features that Visual Studio Tools for Office 2005 provides -- server-based Word and Excel document applications?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Excel Services is a new feature of Office 12 that can be deployed to a web
server.

http://techtalkpt.wordpress.com/2006/04/28/bi-in-office-2007-resources/

--
Regards

John Timney
Microsoft MVP

Sue Mosher said:
Such articles gives you the false impression that you can use Office in a
server context like ASP, which is not the case. Such articles should start
by referring to the Q257757 KB article, those brave souls who still think
they want Office server-side should keep in mind that it only talks about
ASP, ASP.NET is even more problematic because of the non deterministic
nature of the GC/Finalizer.

Willy, isn't that one of the features that Visual Studio Tools for Office
2005 provides -- server-based Word and Excel document applications?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
It's really quite simple ...

This sample opens a Word template, uses Find & Replace to change the
content of the document and then saves the document. Hope it helps.

Regards
James

Add the reference to the version of MS Word you are using. Then in the
code behind page ...

Dim MSWord As New Word.ApplicationClass
Dim doc As New Word.DocumentClass

strFile = Server.MapPath("/template.dot")
MSWord = CreateObject("Word.Application")
MSWord.Visible = True
doc = MSWord.Documents.Add(strFile)
selection = MSWord.Selection

MSWord.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekMainDocument

strFind = "<date>"
strReplace = Format(DateValue(Now()), "D")

SearchAndReplace(strFind, strReplace)

strFind = "<name>"
strReplace = theName
SearchAndReplace(strFind, strReplace)

strFind = "<address>"
strReplace = address
SearchAndReplace(strFind, strReplace)

strFile = Server.MapPath("") & "filename.doc"

doc.SaveAs(strFile)
doc.Close()
doc = Nothing
MSWord.Quit()
MSWord = Nothing
end sub

Function SearchAndReplace(ByVal strTmpSearchFor, ByVal
strTmpReplaceWith)
selection.Find.ClearFormatting()
selection.Find.Replacement.ClearFormatting()
selection.Find.Text = strTmpSearchFor
selection.Find.Replacement.Text = strTmpReplaceWith
selection.Find.Forward = True
selection.Find.Wrap = Word.WdFindWrap.wdFindContinue
selection.Find.Format = False
selection.Find.MatchCase = False
selection.Find.MatchWholeWord = False
selection.Find.MatchWildcards = False
selection.Find.MatchSoundsLike = False
selection.Find.MatchAllWordForms = False
selection.Find.Execute(, , , , , , , , , ,
Word.WdReplace.wdReplaceAll)
End Function
 
Hey James. I got everything going in csharp. I am able to hit the page
locally and open word, but when another user tries to hit my page from their
site, they are prompted for username and password. Any ideas?
I used impersonate = true in the web config and gave the IUSR_MachineName
access to word.
Any other ideas?
 
Back
Top