creating word doc in website

Discussion in 'ASP .Net' started by ton, Mar 31, 2009.

  1. ton

    ton Guest

    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
    ton, Mar 31, 2009
    #1
    1. Advertising

  2. On Mar 31, 11:40 am, "ton" <> wrote:
    > 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
    Alexey Smirnov, Mar 31, 2009
    #2
    1. Advertising

  3. ton

    ton Guest

    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



    "Mark Rae [MVP]" <> schreef in bericht
    news:...
    > "ton" <> wrote in message
    > news:c7f75$49d1f0aa$541ec4bd$1.nb.home.nl...
    >
    >> Is it possible to run an exe file instead (without any dialog), but a exe
    >> who just create the doc and save it.

    >
    > Anything which would require a copy of Word to be installed on the server
    > is potentially subject to the same problems, so no.
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    ton, Mar 31, 2009
    #3
  4. ton

    Paul Shapiro Guest

    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.

    "ton" <> wrote in message
    news:1ad7a$49d2052e$541ec4bd$1.nb.home.nl...
    > 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
    >
    >
    > "Mark Rae [MVP]" <> schreef in bericht
    > news:...
    >> "ton" <> wrote in message
    >> news:c7f75$49d1f0aa$541ec4bd$1.nb.home.nl...
    >>
    >>> Is it possible to run an exe file instead (without any dialog), but a
    >>> exe who just create the doc and save it.

    >>
    >> Anything which would require a copy of Word to be installed on the server
    >> is potentially subject to the same problems, so no.
    >>
    >>
    >> --
    >> Mark Rae
    >> ASP.NET MVP
    >> http://www.markrae.net
    Paul Shapiro, Mar 31, 2009
    #4
  5. ton

    Hans Kesting Guest

    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
    Hans Kesting, Mar 31, 2009
    #5
  6. ton

    ton Guest

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

    thanx anyhow


    "Mark Rae [MVP]" <> schreef in bericht
    news:u6%...
    > "ton" <> wrote in message
    > news:1ad7a$49d2052e$541ec4bd$1.nb.home.nl...
    >
    >> I've just solved the problem.

    >
    > You may think you have, but you haven't...
    >
    >> It is possible.

    >
    > I didn't say that it wasn't possible! I advised you not to do it because
    > it's unsupported by Microsoft and will almost certainly fail very soon...
    >
    > Did you not actually understand why I advised you not to do this...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    ton, Apr 1, 2009
    #6
  7. ton

    cubaman Guest

    On Mar 31, 12:15 pm, "Mark Rae [MVP]" <> wrote:
    > "ton" <> wrote in message
    >
    > news:5f964$49d1e526$541ec4bd$1.nb.home.nl...
    >
    > > 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

    >
    > 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.
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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.
    cubaman, Apr 1, 2009
    #7
  8. ton

    cubaman Guest

    On Mar 31, 11:40 am, "ton" <> wrote:
    > 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
    cubaman, Apr 1, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wallace
    Replies:
    0
    Views:
    457
    John Wallace
    Jul 22, 2003
  2. Stephen Witter

    opening a word doc in word not browser

    Stephen Witter, May 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    Stephen Witter
    May 18, 2004
  3. Sharon
    Replies:
    0
    Views:
    591
    Sharon
    Jul 27, 2005
  4. Franck
    Replies:
    1
    Views:
    4,810
  5. Matt
    Replies:
    3
    Views:
    483
    Tor Iver Wilhelmsen
    Sep 17, 2004
Loading...

Share This Page