?
Solved

ASP and Word Integration

Posted on 2003-03-12
20
Medium Priority
?
662 Views
Last Modified: 2011-04-14
Hello all,

Here is the situaton:

I have a word document on the server. Which has a logo at the first pharagraph, and a big table with some text in some cells and empty cells. The word document has no bookmarks.

I want to achieve the following with ASP (Classic ASP) running under IIS 4.0.:
1. I want to copy this .doc file with a different name. To be specific I want to add a unique reference number followed by an underscore in front of it and save it.
2. With this new document which the name of the file is changed in step 1, I want to Open and Insert some text in the empty cells in the document which I will retrieve from a recordset and save it.

I now that this can be done in ASP with Scripting object but I could not find any suitable code on the net.

Can you please help
0
Comment
Question by:scorpion219
[X]
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
  • 8
  • 7
  • 5
20 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 8121525
Using Word on the server is not recommended.

1. Word is very often not installed on (web-) servers
2. It will cause problems when you use word from ASP.

In your case: why store the results in a Word document, why not in a textfile or in the database ? Do you have any special reason to store it in Word?

If you really insist on using a Word-like thing, then i'd recommend using RTF in stead of the word-format. Word can handle RTF, and RTF is much better to handle on the server then Word. Mainly because it is a "text" form (like HTML)
See http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20443835.html for more about using RTF on the server.
0
 
LVL 7

Expert Comment

by:lavinder
ID: 8125791
hi scorpion

1) to copy the file with a new name
 
 dim fso,newname
 set fso=server.createobject("scripting.filesystemobject")
 newname="source" & timer & "_.doc"
 fso.CopyFile server.mappath("source.doc"), server.mappath(newname)
 set fso=nothing

2) to open word doc and insert some text in a table's cell

   Dim wapp,Dim wdoc
   set wapp=server.createobject("word.application")
   set wdoc=server.createobject("word.document")    
   Set wdoc = wapp.Documents.Open(server.mappath(newname))
   wdoc.Tables(0).Cell(1, 1).Select
   Selection.Text = "ddd"
   wdoc.save
   .....

 using word on webserver has some issues with it, so do consider the comments of sybe.

cheers!!


0
 

Author Comment

by:scorpion219
ID: 8126691
Dear experts,

Thank you for your valuable feedback.

To Sybe:
I dont want to store the info into db. I have a word document, want to copy it, manipulate the table cells with some data of recordset, close it. Give the client the option to download this document, if the client does not want it, I will delete the modified doc from the server.

To Lavinder:

Thanks to you, I have the following code:

<%
dim fso, newname
set fso=server.createobject("scripting.filesystemobject")
newname="2003_test.doc"
fso.CopyFile server.mappath("test.doc"), server.mappath(newname)
set fso=nothing

' UNTIL HERE IT WORKS FINE

Dim wapp,wdoc
set wapp=server.createobject("word.application")
set wdoc=server.createobject("word.document")    
Set wdoc = wapp.Documents.Open(server.mappath(newname))

' THERE IS A TABLE IN THE DOC
wdoc.Tables(1).Cell(1, 1).Select
Selection.Text = "ddd"
wdoc.save
%>

When I run this code it copies the source with a new filename. Thats fine.

BUT: It returns me an error saying "Object required: 'Selection'" and when I modify it to wdoc.Selection.Text = "ddd" it does not help either

On the other hand when I comment the following lines in the code
wdoc.Tables(1).Cell(1, 1).Select
Selection.Text = "ddd"

the (test)webserver hangs

Can you please help

 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:sybe
ID: 8126940
As I said:

** It will cause problems when you use word from ASP.

Have you considered the RTF solution I mentioned ??

0
 

Author Comment

by:scorpion219
ID: 8127121
Hi sybe,

But I still believe that it can be achieved by using ASP->Word

I also have looked to the link you have send me. But the problem with that is that I could not find any example to create tables and put an image at the beginning of the file in the "Rich Text Format (RTF) Specification".

Can you give me an example how I can create an image and a table in the rft.

i.e MyFile.WriteLine etc

Cheers
0
 
LVL 28

Expert Comment

by:sybe
ID: 8127147
0
 
LVL 7

Expert Comment

by:lavinder
ID: 8127151
scorpion

 i think, you have not coded the script correctly, you have not closed the application, that is why server hanged.

 after wdoc.save
 you will have to call wdoc.close
 wapp.quit
 set wdoc=nothing
 set wapp=nothing

 i assumed that you know how to put all of this ESSENTIAL STUFF after any kind of automation. anyway try it now.
if your document has 2 tables first table will be referred as tables(0) and second one as tables(1).



 
0
 
LVL 28

Expert Comment

by:sybe
ID: 8127159
it says:

"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when run in this environment."


you might produce some runing code, but you will probably get into trouble when your application goed live. Like server hanging etc.
0
 
LVL 28

Expert Comment

by:sybe
ID: 8127191
I would recommend to create a template in Word with certain keywords on places you want to put dynamic information, like <FIELD1>

do a "Save As" to RTF, put the RTF on the server.

For interactive use, open the RTF in ASP into a String using FileSystem, do a Replace in the keywords, and save the string as RTF with a new name (using FileSystem).
0
 

Author Comment

by:scorpion219
ID: 8127243
Hi Sybe,

OK but please an example code that illustrates your methodology.

Thanks
0
 

Author Comment

by:scorpion219
ID: 8127258
Hi lavinder,

I nicely closed the application. But it is still hanging.

You forgot to give an answer to my question:
The code returns me an error saying "Object required: 'Selection'" and when I modify it to wdoc.Selection.Text = "ddd" it does not help either

Please, please help
0
 

Author Comment

by:scorpion219
ID: 8127267
Hi lavinder,

I nicely closed the application. But it is still hanging.

You forgot to give an answer to my question:
The code returns me an error saying "Object required: 'Selection'" and when I modify it to wdoc.Selection.Text = "ddd" it does not help either

Please, please help
0
 
LVL 7

Expert Comment

by:lavinder
ID: 8127292
can you paste the code here and please describe your document structure (how many tables you have, rows,columns and which row,column you wanna start with).
0
 

Author Comment

by:scorpion219
ID: 8127371
Here is the code

<%
dim fso, newname
set fso=server.createobject("scripting.filesystemobject")
newname="2010_test.doc"
fso.CopyFile server.mappath("test1.doc"), server.mappath(newname)
set fso=nothing
Dim wapp,wdoc
set wapp=server.createobject("word.application")
set wdoc=server.createobject("word.document")    
Set wdoc = wapp.Documents.Open(server.mappath(newname))
wdoc.Tables(1).Cell(1, 1).Select
Selection.Text = "Add this to the cell"
wdoc.save
wapp.quit
set wdoc=nothing
set wapp=nothing
%>

Test1 doc is a word document in the first pharagraph a picture (logo) after that there is only one table with 5 coloms and two rows. That's it

Regards
0
 
LVL 7

Expert Comment

by:lavinder
ID: 8127447
try this

<%
dim fso, newname
set fso=server.createobject("scripting.filesystemobject")
newname="2010_test.doc"
fso.CopyFile server.mappath("test1.doc"), server.mappath(newname)
set fso=nothing

Dim wapp,wdoc
set wapp=server.createobject("word.application")
set wdoc=server.createobject("word.document")    
Set wdoc = wapp.Documents.Open(server.mappath(newname))
wdoc.Tables(0).Cell(1, 1).Select
wdoc.Selection.Text = "Add this to the cell"
wdoc.save
wdoc.close
wapp.quit
set wdoc=nothing
set wapp=nothing
%>
0
 
LVL 7

Expert Comment

by:lavinder
ID: 8127470
wapp.selection.text="add this to cell"
0
 

Author Comment

by:scorpion219
ID: 8127519
As I told you before this doesnt work. I pasted this code:

<%
dim fso, newname
set fso=server.createobject("scripting.filesystemobject")
newname="2012_test.doc"
fso.CopyFile server.mappath("test1.doc"), server.mappath(newname)
set fso=nothing

Dim wapp,wdoc
set wapp=server.createobject("word.application")
set wdoc=server.createobject("word.document")    
Set wdoc = wapp.Documents.Open(server.mappath(newname))
wdoc.Tables(0).Cell(1, 1).Select
wdoc.Selection.Text = "Add this to the cell"
wdoc.save
wdoc.close
wapp.quit
set wdoc=nothing
set wapp=nothing
%>

And it returned me the following error message:

"Microsoft Word error '800a1735' The requested member of the collection does not exist. "

I changed Tables(0) to Tables(1) and no result.
Then I received a different error message saying:

"Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'Selection' '

What can it be? I am going crazy you are so near to the solution

Help please
0
 
LVL 7

Accepted Solution

by:
lavinder earned 300 total points
ID: 8127566
wapp.selection.text="add this to cell"
0
 
LVL 7

Expert Comment

by:lavinder
ID: 8127593
here is a snippet, just an idea (if selection does not works)

Set oTable = Selection.Tables(1)

    If Not oTable Is Nothing Then
      For nRow = 1 To oTable.Rows.Count
        For nCol = 1 To oTable.Columns.Count
          sText = oTable.Cell(nRow, nCol).Range.Text
          sText = Left$(sText, Len(sText) - 2)
           If IsNumeric(sText) Then
            oTable.Cell(nRow, nCol).Range.Text = Format$(sText, "#,##0.00")
          End If
        Next nCol
      Next nRow
    End If
0
 

Author Comment

by:scorpion219
ID: 8128165
Thank you very much lavinder.

I am giving you my points, I wish I had 1000 points to give.

Regards
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

801 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