Word Automation with ASP


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Word Automation with ASP

  1. #1
    Stan Brown Guest

    Word Automation with ASP


    Hi,
    I'm trying to populate a word document with ASP. What I'm needing to do is
    replace tags on a word document with fields from the database. So far here
    is where I am.

    Set WordObject = server.CreateObject("Word.Application")

  2. #2
    Harish Mohanbabu Guest

    Re: Word Automation with ASP


    Hi Stan,

    In one of my earlier projects, a requirement was for creating a word doc,
    populating the same from database and printing it to a central printer.

    I had implemented the following -

    1. Created a word doc template file with Bookmarks
    2. Stored the complete path of the above word doc in the registy
    3. Created a VB component which reads the registry and opens the
    template file
    4. This component reads database and switches the bookmark with the values
    from database
    5. Saves (again path is read from registry) the file.
    6. And starts printing it.

    The VB code for replacing the bookmark will go like this -

    ---------------------------------------------
    dim letterDoc as Word.document
    set letterDoc = new Word.document
    letterDoc.activate

    ActiveDocument.Bookmarks(<book mark name>).select
    selection.typetext <rs.value>

    .....
    .....
    ---------------------------------------------

    My suggestion would be not tinkering with MS office documents from ASP directly.
    MS Office Documents are sort of white elephants in terms of memory. If you
    have to create Word Doc, try creating RTF files instead.

    Thanks,
    Harish



    "Stan Brown" <slbrown@tva.gov> wrote:
    >
    >Hi,
    >I'm trying to populate a word document with ASP. What I'm needing to do

    is
    >replace tags on a word document with fields from the database. So far here
    >is where I am.
    >
    >Set WordObject = server.CreateObject("Word.Application")



  3. #3
    Jake Duckworth Guest

    Re: Word Automation with ASP


    Here is a small sample of how I do what you are asking for.. the word doc
    must have the corresponding doc variables in it for this code to work...
    you can add them by going to Insert->Field, then select the "Document Automation"
    category and the "DocVariable" fieldname, then enter DOCVARIABLE "contact_name"
    into the text box and hit enter. I am using MS Word 2000.

    <%
    Set WordApp = Server.CreateObject("Word.Application")
    Set WordDoc = Server.CreateObject("Word.Document")
    Set WordDoc = WordApp.Documents.Open(strDocPath)
    With WordDoc
    .Variables("date").Value = Date
    .Variables("contact_name").Value = strContactName
    .Variables("company_name").Value = strCompanyName
    .Variables("contact_address").Value = strAddress
    .Variables("contact_dear").Value = strContactDear
    .Variables("contact_city").Value = strCity
    .Variables("contact_state").Value = strState
    .Variables("contact_zip").Value = strZip

    On Error Resume Next
    .Fields.Update
    .SaveAs strNewDocPath
    .Close
    On Error Goto 0
    End With
    WordApp.Quit
    Set WordDoc = Nothing
    Set WordApp = Nothing
    %>

  4. #4
    Harish Mohanbabu Guest

    Re: Word Automation with ASP


    Jake,

    Many many thanks for letting me know about DOCVARIABLE.

    Well - the point I was trying to make the other day was creating Office docs
    from any unattended, non-interactive client like ASP is quite risky - mainly
    because Office may exhibit unstable behaviour. For more info about this,
    please refer to MS KB Article - Q257757.

    If you must create Office docs from ASP, IMHO you can choose from the following
    options -

    * Accessing Office through wrapper components
    ASP --> Custom DLL --> MS Office

    * Tinkering with Response.ContentType
    (Sample - Q301044)

    * Accessing through 3rd party components. For MS Excel, there is a very
    good component from Software Artisans (Excel Writer). I am not aware of

    any 3rd party component for MS Word as such.

    BTW, the functionality offered by DOCVARIABLE is quite similar to the one
    offered by using Bookmark.

    Thanks,
    Harish Mohanbabu


    "Jake Duckworth" <jduckwo@yahoo.com> wrote:
    >
    >Here is a small sample of how I do what you are asking for.. the word doc
    >must have the corresponding doc variables in it for this code to work...
    >you can add them by going to Insert->Field, then select the "Document Automation"
    >category and the "DocVariable" fieldname, then enter DOCVARIABLE "contact_name"
    >into the text box and hit enter. I am using MS Word 2000.
    >
    ><%
    > Set WordApp = Server.CreateObject("Word.Application")
    > Set WordDoc = Server.CreateObject("Word.Document")
    > Set WordDoc = WordApp.Documents.Open(strDocPath)
    > With WordDoc
    > .Variables("date").Value = Date
    > .Variables("contact_name").Value = strContactName
    > .Variables("company_name").Value = strCompanyName
    > .Variables("contact_address").Value = strAddress
    > .Variables("contact_dear").Value = strContactDear
    > .Variables("contact_city").Value = strCity
    > .Variables("contact_state").Value = strState
    > .Variables("contact_zip").Value = strZip
    >
    > On Error Resume Next
    > .Fields.Update
    > .SaveAs strNewDocPath
    > .Close
    > On Error Goto 0
    > End With
    > WordApp.Quit
    > Set WordDoc = Nothing
    > Set WordApp = Nothing
    >%>



  5. #5
    Join Date
    Feb 2010
    Posts
    1

    Word Automation

    Office Automation For Word Application:

    On searching for office automation at internet, I’m lost. After gethering information from where i found. I tried full two days my codes on diffrent servers and office suites (Office XP and Office 2003).

    Guess we have Windows 2003 server, using pure ASP and office suite on this server (at least Word)

    And we want to send “doc” from our client to server and we want server to translate this doc to html for future view.

    I will not focus to sending process (or upload process), you can find it for example “Pure Asp Upload” (http://www.motobit.com/help/scptutl/pure-asp-upload.htm) but I want to focus on translation after upload using “Word.Application” (called office automation).


    1- We have Windows 2003 server running IIS on this server and ASP codes allowed.

    2- We installed office suite or for this example WORD on this server.

    3- Under Inetpub\wwwroot open Word2Html folder

    4- Under Inetpub\wwwroot\Word2Html open Data folder.

    5- Under Inetpub\wwwroot\Word2Html\Data folder open a word doc name aaa.doc or copy your favoriet doc to this folder name aaa.doc

    6- Just copy and paste the code below to Test.asp

    7- Program link will be http://<%server%>/Word2Html/Test.asp

    8- But this code will not run and populate unterminate WINWORD.exe under TaskManager and after run severel times it will add multiple instance of WINWORD.exe

    9- To here you can find on internet but scurity problems begins from here beacuse of “Normal.dot” which automaticly opens by WinWord on start. Winword wants to open “Normal.dot” under “C:\” where IIS user can not access to thislocation and gives error to ASP.

    10- Now Start-->Run-->DcomCnfg Component Service opens.

    11- Component Services-->Computers-->My Computer-->DCOM Config-->Microsoft Word Document right click Properties

    12- Under Security Tab we will edit “Launch and Activation Permissions” and “Access Permissions” just clisk “Customise” both of these and press “EDIT” button and add “IUSR_<%machine name%>” user to both of them.

    13- Only under “Launch and Activation Permissions” “IUSR_<%machine name%>” Permitions. Click “Local Activation” checkbox to “OK”.

    14- Then under “Identity” Tab you will see “Which user account do you want to use or run this application” defult is “The launching user” change it to “This User”
    and Browse for Administrator user and write down the password and confirm password of administrator user.

    Now it is time to run Test.asp, it will open aaa.doc under Data folder and trasnslate it to aaa.htm under same folder.

    If you open Taskmanager while converting, you will see WINWORD.exe runs and deactivates after run.



    Test.asp:



    <%
    on error resume next

    file1=Server.MapPath("\Word2Html\Data\aaa.doc")

    file2=Server.MapPath("\Word2Html\Data\aaa.htm")

    set word = Server.Createobject("Word.Application")

    call word.documents.open(file1)

    word.Visible = False

    call word.Documents(1).saveas(file2,8)

    call word.Documents.close()

    word.quit
    %>

    Bülent Artüz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center