nguyen_85
asked on
Manupilate Word Template with ASP or VBSCript
I have a word document named report.doc which i used as a template.This word document have 2 tables. Each table is 2 X 2. I need to be able to connect to this document by ASP or VBScript. Then insert some data into these tables and save the file to a different name so the original file is not changed. Is there a way I can do this in ASP? Will IIS generate an error since openning word object is an .exe (an out of process method)? If it is how do I fixed this problem? Please send me some code an explination if you can. Thanks!
ASKER
To GaryC123. I guess that's another safe way to solve this problem. But with Word it's easier for me to format the text from the Word template and just insert data from the ASP side. I probably use the method you send me if there is no better way. I know opening word is sometime dangerous in IIS! Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some light reading
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q257757
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q257757
Hi
well i do the same sought of thing what i do is write the info to a txt file open the template and use mail merage to merage the info that i have a macro that prints the word document to a postscript file wich is then picked up my acrobat distiller and printed to a pdf file
you will need some extra asp componets for this
// this delate any old file using ASPExec.Execute companent
<%Set Executor = Server.CreateObject("ASPEx ec.Execute ")
Executor.Application = "cmd /c del c:\watch\out\" & session("orig")& "fax.pdf"
Executor.Parameters = ""
Executor.ExecuteDosApp
Executor.Application = "cmd /c del F:\www.imi-uk.com\pdfs\" & session("orig") & "fax.pdf"
Executor.Parameters = ""
Executor.ExecuteDosApp
Response.Write("<font face='arial' color='#FF0000'>Setting Up </font>")
response.flush
%>
// this makes sure that the previos out of procees is stop
for n=1 to 1000000
next
Set ProcList = Server.CreateObject("ASPsv g.Process" )
varArray = ProcList.GetProcessList
ArrayLimit = UBound(varArray) -1
For I = 0 to ArrayLimit
strProcessID = varArray(I)(0)
strProcessName = varArray(I)(1)
if strProcessName="cmd.exe" then
strProcessIDtoKill = strProcessID
if ProcList.Kill(strProcessID toKill) then
Response.Write("Setting Up<BR>")
response.flush
else
Response.Write("Preparing< BR>")
response.flush
end if
end if
next
set proclist=nothing
set executor=nothing%>
<%// this writes the inforamtion to a txt file
fieldstring="imi~govinc~ro ~ra~jur~pr od~dir~shr ~sec~dfees ~shfees~se cfees~curr ~orig~not~ app~cert"
datastring=session("imiinc ") & "~" & session("govinc") & "~" & session("rofee") & "~" & session("ra") & "~" & session("jur") & "~" & session("prod") & "~" & request.Form("dir") & "~" & request.Form("shr") & "~" & request.Form("sec") & "~" & session("dfees") & "~" & session("shfees") & "~" & session("secfees") & "~" & session("bill") & "~" & session("orig") & "~" & request.Form("notarisation ") & "~" & request.Form("apostille") & "~" & request.Form("certificate" ) %>
<% Set Executor = Server.CreateObject("ASPEx ec.Execute ")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
objFSO.CreateTextFile("C:\ data\fax.t xt")
Set objIncFile = objFSO.OpenTextFile("C:\da ta\fax.txt ", 8)
objIncFile.WriteLine(field string)
objIncFile.WriteLine(datas tring)
objIncFile.Close
rem ------------------docs---- ---------- ---------- ---
Response.Write("<BR>Prepar ing<BR>")
response.flush%>
<% // this open word and merges the file the WaitFor.Comp componet allows word to process it documet before and colse them when finished
Set WdApp = Server.CreateObject("Word. Applicatio n")
Set WaitObj = Server.CreateObject ("WaitFor.Comp")
WdApp.Documents.Open("C:\d ata\fax.do c")
strFilename = "C:\watch\out\" & session("orig") & "fax.pdf"
Response.Write "Sending Data<BR>"
Response.Flush%>
<%
WaitObj.TimeOut = 10
if WaitObj.WaitForFileExists (strFileName) then
Response.Write "Waiting for response.....<BR>"
else
Response.Write "Connection Failed, please hit reload button to re-send the data<BR>"
response.end
end if
Response.Flush%>
<%
//this is the macro for the word doc
Private Sub Document_Open()
Dim fileloc As String
fileloc = "c:\watch\in\" & ActiveDocument.MailMerge.D ataSource. DataFields ("ordID"). Value & "nmapp.ps"
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=True, PrintToFile:=True, OutputFileName:=fileloc, _
Append:=False
ActiveDocument.Close savechanges = no
End Sub
well i do the same sought of thing what i do is write the info to a txt file open the template and use mail merage to merage the info that i have a macro that prints the word document to a postscript file wich is then picked up my acrobat distiller and printed to a pdf file
you will need some extra asp componets for this
// this delate any old file using ASPExec.Execute companent
<%Set Executor = Server.CreateObject("ASPEx
Executor.Application = "cmd /c del c:\watch\out\" & session("orig")& "fax.pdf"
Executor.Parameters = ""
Executor.ExecuteDosApp
Executor.Application = "cmd /c del F:\www.imi-uk.com\pdfs\" & session("orig") & "fax.pdf"
Executor.Parameters = ""
Executor.ExecuteDosApp
Response.Write("<font face='arial' color='#FF0000'>Setting Up </font>")
response.flush
%>
// this makes sure that the previos out of procees is stop
for n=1 to 1000000
next
Set ProcList = Server.CreateObject("ASPsv
varArray = ProcList.GetProcessList
ArrayLimit = UBound(varArray) -1
For I = 0 to ArrayLimit
strProcessID = varArray(I)(0)
strProcessName = varArray(I)(1)
if strProcessName="cmd.exe" then
strProcessIDtoKill = strProcessID
if ProcList.Kill(strProcessID
Response.Write("Setting Up<BR>")
response.flush
else
Response.Write("Preparing<
response.flush
end if
end if
next
set proclist=nothing
set executor=nothing%>
<%// this writes the inforamtion to a txt file
fieldstring="imi~govinc~ro
datastring=session("imiinc
<% Set Executor = Server.CreateObject("ASPEx
Set objFSO = CreateObject("Scripting.Fi
objFSO.CreateTextFile("C:\
Set objIncFile = objFSO.OpenTextFile("C:\da
objIncFile.WriteLine(field
objIncFile.WriteLine(datas
objIncFile.Close
rem ------------------docs----
Response.Write("<BR>Prepar
response.flush%>
<% // this open word and merges the file the WaitFor.Comp componet allows word to process it documet before and colse them when finished
Set WdApp = Server.CreateObject("Word.
Set WaitObj = Server.CreateObject ("WaitFor.Comp")
WdApp.Documents.Open("C:\d
strFilename = "C:\watch\out\" & session("orig") & "fax.pdf"
Response.Write "Sending Data<BR>"
Response.Flush%>
<%
WaitObj.TimeOut = 10
if WaitObj.WaitForFileExists (strFileName) then
Response.Write "Waiting for response.....<BR>"
else
Response.Write "Connection Failed, please hit reload button to re-send the data<BR>"
response.end
end if
Response.Flush%>
<%
//this is the macro for the word doc
Private Sub Document_Open()
Dim fileloc As String
fileloc = "c:\watch\in\" & ActiveDocument.MailMerge.D
Application.PrintOut FileName:="", Range:=wdPrintAllDocument,
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages,
Collate:=True, Background:=True, PrintToFile:=True, OutputFileName:=fileloc, _
Append:=False
ActiveDocument.Close savechanges = no
End Sub
http://www.codeave.com/asp/code.asp?u_log=142