Solved

ASP/VB: Word Doc file generation

Posted on 2004-08-23
44
1,289 Views
Last Modified: 2008-08-13
Hi,

We use the Word.Application component to convert our HTML report files into Word DOC files. This works good. But this process is inefficient, in that for generating every word document, we have to load the resource intensive Word.Application component every time and there is a known issue of some reports failing when a lot of our customers try to generate word reports at the same time.

We have looked at using some substitute components to perform this translation (SoftArtisans WordWriter, but the integration is not seamless enough and we need this done very quickly).   Now, to solve this issue, I am trying to use the Response.ContentType statement to transform the HTML output, and then save the resulting output as a ".doc" file.

Here're the statements:

Response.AddHeader "Content-Disposition", "attachment; filename=reportName.doc"
Response.Charset = "UTF-8"
Response.ContentType = "application/msword"

This works too! But not quite good enough.

The problem is that this removes the control over the format of the word file that I normally get when using the Word.Application component. Main problem is that the images in the word document are now linked to the images on the server:

1.  Is it possible to set the Word DOC properties like
SavePictureWithDocument = True
ActiveDocument.ActiveWindow.View.Type = wdPrintView
etc., using ASP?

2. Is there another way to do this; ie, altering the normal.doc template file on the server where these files are being saved, so that the image links are broken.

Thanks,
Raj
0
Comment
Question by:rb79
  • 14
  • 10
  • 8
  • +4
44 Comments
 

Author Comment

by:rb79
Comment Utility
Please do move it to the ASP section.
(While posting I had found an ASP.Net section, but no ASP section. But, now I think it should be ok.)
0
 

Author Comment

by:rb79
Comment Utility
Thanks Venabili!
0
 
LVL 15

Expert Comment

by:joeposter649
Comment Utility
Perhaps you could keep the word.application object around and just create new documents.  Not sure if this would cause threading problems thought...
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q243/5/43.ASP&NoWebContent=1
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I would definitely stay away from launching word on the server as MS recommends against doing so:

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q257/7/57.ASP&NoWebContent=1

This is not supported without a component:

SavePictureWithDocument = True
ActiveDocument.ActiveWindow.View.Type = wdPrintView

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
One other option to consider is using something like ActivePDF available at www.activePDF.com

FtB
0
 

Author Comment

by:rb79
Comment Utility
All your help is appreciated. Also found TX Text Control (http://www.textcontrol.com) looks very promising. But I would still be inclined to use ASP to generate the Word DOC file, rather than a third party control.

FYI This same issue has been left hanging here: http://forums.devarticles.com/t2648/s.html

My question is that is there a MS Word script that I could add to the doc file that I generate.
Something like the following that allegedly adds header text to the word doc file. Don't know how to use it though in our application.
/* Page Definitions */
@page
{mso-footnote-separator:url("<%=Application("ServerPath")%>header.htm") fs;
mso-footnote-continuation-separator:url("<%=Application("ServerPath")%>header.htm") fcs;
mso-endnote-separator:url("<%=Application("ServerPath")%>header.htm") es;
mso-endnote-continuation-separator:url("<%=Application("ServerPath")%>header.htm") ecs;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:35.4pt;
mso-footer-margin:35.4pt;
mso-header:url("<%=Application("ServerPath")%>header.htm") h1;
mso-footer:url("<%=Application("ServerPath")%>header.htm") f1;
mso-paper-source:0;
mso-gutter-direction:rtl;}
div.Section1
{page:Section1;}
0
 

Author Comment

by:rb79
Comment Utility
Right now I think I need a good conversion tool (that will work like the Office Automation Word object)

1.  Tried TX Text Control ... very poor performance. They accepted the inadequacy of the control. Rejected.
2. Looking at WordWriter from SoftArtisans. Problem is that they ask you to create template Word files with mergefields for text and images(recently added feature). This is not the best conversion but should work.
3. Also checked CZ Document converter. Extremely slow and I am pretty certain that it uses Microsoft Word object in the background. Worst of the lot.

What I really need is a conversion tool, and I would be really thankful if somebody could suggest a few good conversion tools.

Thanks,
Raj
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I don't really know of anything like that. If you were just using simple text, then you could use the file system object to create the document and then you could just stream it. Otherwise, I don't know what else you could do instead.

FtB
0
 
LVL 15

Expert Comment

by:joeposter649
Comment Utility
Perhaps you can convert your html to mhtml and then have Word open it.
http://www.codeproject.com/aspnet/aspnethtml2mht.asp
I'm really busy right now so I can't help much...
0
 

Author Comment

by:rb79
Comment Utility
Thanks for the suggestion, but MHTML is not good enough.
The images are linked files on the server in the MHTML document that get deleted every couple of days.
The Word document should have the images embedded, not linked to a remote server.
0
 
LVL 15

Expert Comment

by:joeposter649
Comment Utility
MHTML uses mime encoding to embed images in a single file.
Just do a save as in IE or Word and you'll see what I mean.  Choose "Web Archive" for the mht file extension.
0
 

Author Comment

by:rb79
Comment Utility
Okay, I'll give it a try using the codeproject website link you provided earlier.
BTW my previous comment was based on the following observation:
I tried to save a random webpage in IE using the "Save as type" of "Web Archive, single file (*.mht)". That saved the webpage as a single file on my computer, but the links to the images were hardcoded, and I didn't see any sort of mime encoding. But maybe these are two different things.
0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
Did you get any further with this? I'm looking to do something similar, and while I'm familiar with Word VBA I'm new to ASP and ASP.NET in particular.

Could you give me some more info re: issue reports failing etc.?
0
 

Author Comment

by:rb79
Comment Utility
Actually, I haven't gotten much further. I am trying out Softartisan's WordWriter product. WordWriter lets you insert text and images in a pre-built Word template file. But I have been facing some problems with that.

The issues we are facing currently with Word file generation using the Office Automation object are that the process is extremely slow and is prone to failure if the load on the server is too high.
0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
Have you identified that the VBA is slower when run through ASP as opposed to across the LAN as opposed to run locally? Is it definately only when via ASP that is particularly slow? Sorry if thats an obvious thing you've tried.

In my experience  if you write word VBA using a lot of selections instead of using ranges it can slow drastically. We had one macro that took 10 mins before we otimised the code and 30 sec afterwards. Tables and selections being the main culprits.

I've never done a lot of VBA via ASP so it may well be a problem at another level entirely.
0
 

Author Comment

by:rb79
Comment Utility
Our ASP code calls a COM+ object created in VB6 that in turn loads the Word.Application object. The bottleneck is the Word application object that consumes a lot of server memory.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
>>The issues we are facing currently with Word file generation using the Office Automation object are that the process is extremely slow and is prone to failure if the load on the server is too high.<<

Ahhhh...it seems to me that someone mentioned that issue earlier in the thread....

FtB

0
 

Author Comment

by:rb79
Comment Utility
That's ok. Probably I might not get a satisfactory answer here anytime soon. But I presumed conversion to MS Word was a common operation. Probably not, and so I will have to look for an answer myself.
0
 

Author Comment

by:rb79
Comment Utility
Guess I will have to stick with MS Word for a while.
Anyways, I have a different query about Word programming that I have posted here: http://www.experts-exchange.com/Applications/MS_Office/Q_21205380.html
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
fritz_the_blank - the issue haven't been clearly defined. The object being slow, consuming large amounts of memeory and failing are different problems. Theres been no purposeful discussion of these issues. Sugesting a complelely different product is not answering the question, but answering a question that wasn't asked.

rb79  - I have built a VBA active X control which I run in a ASP page which is passed in parameters by the ASP. The active X contains VBA which uses word on the client machine (not the server) to create a new document and automate some things in the doc. I'm not having problem with performance. This is working across a intranet which spans a few offices. TBH I don't see why you are doing this on the server when you've been told its v.bad idea and the solution to you problem is not to do it and use your word on the client instead. Unless you know that most of your users won't have word, theres no reason NOT to use word on the client.

I have built lots of reporting solutions using word automation and I've never had problems with performance or frequent errors. At leaast none that I couldn't resolve eventually.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Hey Sparky191--

Thank you for telling me how to post (I guess that an old dog can learn new tricks). I regret to inform you that sometimes the correct answer is that you shouldn't do it a certain way. Please take a moment to read the link that I gave above and you'll see that is exactly what Microsoft has to say about it. But hang on, later in your post, you repeat the very point that I made.

FtB

0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
I'm suggesting that you CAN use word, but just in a different way. I don't see a posted solution that solves the problem USING word as opposed to NOT using word. To me the question is how to automate word for reports from a web page. Not how can I run word on a server.

The code I'm running from within a OCX on an ASP page runs on Word on the client. Word is not installed on the server.  Its been live for 2 months. Mind you its only very basic automation. Something along the lines of. User queries asp/database to get data. Loads ActiveX in ASP. Download data and word template if not on client to client. Open on client, import data to template.







0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
I was concerned on reading joeposter649 post about threading. But I destroy the object once the doc is finshed and my understanding is that threading issues only occur if you persist the objects.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
@Sparky191 :

Capitals ALWAYS make things CLEARER....

Please don't misunderstand me. I really appreciate the content of your posts--they are presenting an option that has not been discussed earlier, and depending on rb79's needs, may very well solve the problem (although I suspect that the need for ActiveX may be a no starter for many because of security reasons).

I am taking issue with your characterization of my respones to date--essentially I am pointing out that using server-side automation with Word is a very, very bad idea, and I haven't changed my mind about that. Moreover, I was simply suggesting an alternative approach. Again, I stand by that post even if you feel that it addresses a question that was not asked (after you answer questions here for awhile, you find out that the solution very often is not one explicitly asked for in the thread). What I fail to understand is why you feel the need to unjustly criticize the posts of other while making your own very good contributions.

FtB
0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
I'm only commenting on the technical issues at hand. Any personal inflection was unintentional. Apologies if the impression was otherwise. I use CAPS to emphasis code and comments in code to aid clarity. I was taught that is good practise. But this is the web and obviously its taken as shouting. Perhaps I should have had my forum hat on and not my programming one. I'll go change.






0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Sorry to have snapped at you in return.

FtB
0
 

Author Comment

by:rb79
Comment Utility
Okay, a final comment before this thread closes.

We are still using the Word component :(  BUT we have been able to optimize the word doc generation for a performance gain of about 80% from before. DOC generation now takes ~4 seconds instead of ~15 seconds. We did this by removing the SavePictureWithDocument for each InlineShape and by just doing .Unlink and .Update for each Shape. This should do for now.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
@rb79--

Please be sure to close this thread out by accepting the comment(s) that helped you the most as an answer.

FtB
0
 

Author Comment

by:rb79
Comment Utility
I appreciate all the helpful comments.
Apparently, the original question for a good third party component is still unresolved. I will keep looking at the other ones in the market.
Also I fixed the SavePictureWithDocument without any help.

Probably, this question should be better deleted.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Well, if my post regarding Microsoft's warning against running Word as an automation object was of no use whatsoever, then I suppose a delete is fine.

FtB
0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
Theres a lot you can do to improve performance of word vba. Use ranges instead of selections where ever possible. I've seen macros go from 10 mins to 10 sec just by doing that alone.
0
 
LVL 1

Expert Comment

by:Sparky191
Comment Utility
Has your recoding reduced the amount of errors, memory utilisation and failed reports?
0
 

Author Comment

by:rb79
Comment Utility
Errors were in the form of slow response times and failed reports. We are yet to stress test the application after this change was implemented.
Memory utilization is the same though the time to run a report has reduced significantly.
0
 
LVL 8

Expert Comment

by:fozylet
Comment Utility
@rb79,
Would be great if you close the question yourself by accepting a comment or requesting a delete.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I still think that ID: 11883095 is relevant here, but if rb79 doesn't think so, then I suppose that a PAQ is fine.

FtB
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQed with no points refunded (of 500)

modulo
Community Support Moderator
0
 

Expert Comment

by:starship_wars
Comment Utility
Hi there rb79 -- hope you can help me, I am also using:
    Response.ContentType = "application/msword"
You said you were able to "remove the SavePictureWithDocument for each InlineShape and by just doing .Unlink and .Update for each Shape."

Could you elaborate or share the code to do this, my images are linked to the server, and I need them embedded in the word doc.
0
 

Author Comment

by:rb79
Comment Utility
@starship_wars:
d is the document object.  Code below:
foreach (MOI.Word.Section s in d.Sections) {

	s.Range.Fields.Update();

	s.Range.Fields.Unlink();

}

Open in new window

0
 

Expert Comment

by:starship_wars
Comment Utility
Thank you Raj!

I was hoping it was possible with the Response object.  But doesn't look like it.  Here's what I'm using:
            Response.Clear()
            Response.AddHeader("Content-Disposition", "attachment;filename=doc.doc")
            Response.AddHeader("Content-Length", s.Length.ToString())
            Response.ContentType = "application/msword"  
            Response.Write(s) 's includes the <img="http://web.com/img/x.gif">
            Response.End()
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now