creating word doc in website

  • Thread starter Thread starter ton
  • Start date Start date
T

ton

Hi,

In VB6 I can create (generate) a word document based upon a template.
- The user click the tempate which I offer in a list
- based on the data he choosed upfront I cabn fill all the bookmarks with
the correct data
- I can save the document I even can print it
some code:
Dim Wordx As Word.Application
Dim thisWord As Word.Document
Set Wordx = CreateObject("Word.Application")
Set thisWord = Wordx.Documents.Add(Template)

' filling the bookmarks something like
Set range = thisWord.GoTo(What:=wdGoToBookmark, Name:=bmbookmark.Name)
If Trim(tekst) <> 0 Then
tekst = Trim(tekst)
End If
range.text = tekst
range.Select
Wordx.ActiveDocument.Bookmarks.Add a, range

and so on at the end:
mydoc="c:\temp\x.doc"
thisWord.BuiltinDocumentProperties(3) = " ton"
thisWord.SaveAs mydoc

easy

But how can it be done in in my webapp. I want to mail the result to the
user, or if possible show it on a webpage, whatever is possible

Can someone explain me how to do this.

Thanx

Ton
 
Hi,

In VB6 I can create (generate) a word document based upon a template.
- The user click the tempate which I offer in a list
- based on the data he choosed upfront I cabn fill all the bookmarks with
the correct data
- I can save the document I even can print it
some code:
Dim Wordx As Word.Application
Dim thisWord As Word.Document
Set Wordx = CreateObject("Word.Application")
Set thisWord = Wordx.Documents.Add(Template)

' filling the bookmarks something like
Set range = thisWord.GoTo(What:=wdGoToBookmark, Name:=bmbookmark.Name)
If Trim(tekst) <> 0 Then
    tekst = Trim(tekst)
End If
range.text = tekst
range.Select
Wordx.ActiveDocument.Bookmarks.Add a, range

and so on at the end:
    mydoc="c:\temp\x.doc"
    thisWord.BuiltinDocumentProperties(3) = " ton"
    thisWord.SaveAs mydoc

easy

But how can it be done in in my webapp. I want to mail the result to the
user, or if possible show it on a webpage, whatever is possible

Can someone explain me how to do this.

Thanx

Ton

Google the answer
http://www.google.com/search?q=create+word+document+asp.net
 
Hi Mark,

I've just solved the problem. It is possible. I found a website with an
example http://www.freevbcode.com/ShowCode.Asp?ID=1727

I've used just the creating doc part which looks like: (and added the
bookmark substitution to it). It works in my development enciroment AND on
my server after publishing it !!!


Dim wordapp, worddoc, myrange1, worddocpath, bmbookmark,
UsedBookmarks, BookmarkName, range, tekst

' CREATE WORD DOCUMENT

WordApp = CreateObject("word.application")
worddoc = wordapp.Documents.Add("c:\temp\dbRecord.dot")

WordApp.Application.Visible = False

MyRange1 = WordDoc.Paragraphs.Add.Range
MyRange1.InsertBefore("Appraisal Form")
'' MyRange1.Style = "Heading 1"

MyRange1 = WordDoc.Paragraphs.Add.Range
myrange1.InsertBefore("Manager: " & "Ton" & vbCrLf & "Appraisee: ")
MyRange1.Font.Bold = True

MyRange1 = WordDoc.Paragraphs.Add.Range
MyRange1.InsertBefore(vbCrLf & "Please fill in all the required
sections and return to HR via the internal mail system.")

' Set the directory location to store the generated documents
'' worddocpath = Server.MapPath("\document")
' Use the unique session ID as the filename.
UsedBookmarks = ","
For Each bmbookmark In worddoc.Bookmarks
BookmarkName = bmbookmark.Name
If InStr(UsedBookmarks, "," + BookmarkName + ",") = 0 Then
tekst = BookmarkName
If Mid$(BookmarkName, 1, 3) <> "tab" Then
'' replacebookmark(range, tekst, bmbookmark)
range = worddoc.GoTo(-1, , , BookmarkName) ''
wdGoToBookmark=-1
If Trim(tekst) <> "" Then
tekst = Trim(tekst)
End If
range.text = tekst
range.Select()
End If
UsedBookmarks = UsedBookmarks & BookmarkName & ","
End If

Next

worddoc.SaveAs("c:\temp\" & Session.SessionID & ".doc")
worddoc.Close()
wordapp.Quit()

worddoc = Nothing
wordapp = Nothing


Thanx

Ton
 
But as Mark already indicated, this only works if you install Word on your
webserver, and this is both unsupported and a license violation. And while
it might work fine for a single user, it probably fails with even a moderate
number of users.
 
After serious thinking ton wrote :
Hi Mark,

I've just solved the problem. It is possible. I found a website with an
example ...


The phrase "Office may exhibit unstable behavior and/or deadlock" (from
the URL Mark pointed out) could be translated as:

"it just might work in some situations (my personal guess: when 1 user
at a time requests a word document to be generated) but *unpredictably
fail* in other situations (personal guess: when multiple users request
it at the same time)"

So the fact that that one test seems to succeed doesn't mean that it
will continue to work when the website is in "regular use".

Hans Kesting
 
to bad, I read your link with the ms warning just now, it is clear to me now

thanx anyhow
 
You can't use the same method in a server-side technology like ASP.NET. Word
(and the rest of Office) simply wasn't designed to be instantiated like
this. Microsoft won't provide any support for this:http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2- note
the paragraph in bold.

So, you have three choices:

1) Use Aspose:http://www.aspose.com/categories/file-format-components/aspose.words-...
The advantage of this is that it provides all of the functionality you're
familiar with from VB6 - the syntax is essentially identical - but without
needing to have a copy of Word installed on the webserver. The disadvantage
is that it's not free. However, it's not *very* expensive, and your time may
be more valuable anyway...
See also:http://www.aspose.com/categories/product-suites/aspose.total-for-.net...

2) Use HTML
Word (like the rest of Office) simply "understands" HTML. So you can create
an HTML document, give it a Word extension, and Word will treat it like a
regular Word document. The advantage of this is that it's free. The
disadvantage is that it doesn't support all of the features of Word that
Aspose does but, if your requirements are fairly basic, it may be
sufficient.

3) Create Word 2007 documents directly.
Word 2007 documents are, essentially, zipped up XML documents. The advantage
of this that it's free. The disadvantage is that it's quite difficult and
time-consuming, as even the simplest of Word documents requires fairly
complex XML. Also, it will require your users to have either Word 2007 or
the Word 2007 viewer.

I disagree with you. It's possible to create Word 2003 documents based
on xsl templates. I do that even with excell workbooks. Office 2003
allows to save and read documents in xml format, without compression.
And is not very complicated.
Best regards.
Oscar Acosta
Best regards.
 
Hi,

In VB6 I can create (generate) a word document based upon a template.
- The user click the tempate which I offer in a list
- based on the data he choosed upfront I cabn fill all the bookmarks with
the correct data
- I can save the document I even can print it
some code:
Dim Wordx As Word.Application
Dim thisWord As Word.Document
Set Wordx = CreateObject("Word.Application")
Set thisWord = Wordx.Documents.Add(Template)

' filling the bookmarks something like
Set range = thisWord.GoTo(What:=wdGoToBookmark, Name:=bmbookmark.Name)
If Trim(tekst) <> 0 Then
    tekst = Trim(tekst)
End If
range.text = tekst
range.Select
Wordx.ActiveDocument.Bookmarks.Add a, range

and so on at the end:
    mydoc="c:\temp\x.doc"
    thisWord.BuiltinDocumentProperties(3) = " ton"
    thisWord.SaveAs mydoc

easy

But how can it be done in in my webapp. I want to mail the result to the
user, or if possible show it on a webpage, whatever is possible

Can someone explain me how to do this.

Thanx

Ton

First, create a word document using word 2003. It would be your
template. Format the document according to your requirements.
Save it as xml document.
Create an xslt template with this document.
Get your data as xml.
Apply a transform to insert relevant data in your template.
Best regards.
Oscar Acosta
 
Back
Top