1

The purpose is to generate proposal documents that can manually be edited in Word after the fact, but before sending them out to the customers.

Much proposal content would be drawn from existing HTML website content (backing CMS) and also some custom (non-HTML) injection for certain scenarios. Of course the conditional logic could go into server-side ASP.NET to vary the content appropriately.

I'm open to 3rd-party tools if raw manipulation of the Word API is arduous. In fact a good 3rd party tool might be the answer.

0

5 Answers 5

3

Use the Aspose Words component for .Net.

Aspose Words Component Link

The component natively understands the Microsoft Word file format without having to install any Microsoft Office products on your application environment. You can then start from an existing word template or programatically build up an entire Microsoft Word document from scratch. The Word object model then allows you to export to doc / docx etc and save as a native Word file to wherever you required.

They have plenty of demos set up on their website.

3
3

I've not used any third-party tools before, as I've only ever written Office automation applications for PCs which already have Office installed.

Creating documents from scratch, or basing them on a template, is quite straightforward. With templates, you can define bookmarks and mail-merge fields to make finding and replacing document elements easier.

Here's a few things that you may find useful:

Named and Optional Arguments
The Word object model is reasonably easy to work with. VB.NET used to be easier to work with than C#: as the Office automation APIs were originally written with VB in mind, you could take advantage of optional parameters. In earlier versions of C#, you had to specify every argument in API calls, which was quite tedious. I understand that this has changed in Visual C# 2010:

How to: Use Named and Optional Arguments in Office Programming (C# Programming Guide) http://msdn.microsoft.com/en-us/library/dd264738.aspx

Tutorials
I found these tutorials quite handy:

Automating Office Programs with VB.NET
http://www.xtremevbtalk.com/showthread.php?t=160433

VB.NET Office Automation FAQ
http://www.xtremevbtalk.com/showthread.php?t=160459

Understanding the Word Object Model from a .NET Developer's Perspective
http://msdn.microsoft.com/en-us/library/aa192495%28office.11%29.aspx

Early and Late binding
One point worth mentioning: late-binding is normally recommended against, but it can be very useful if you don't know what version of Office will be deployed on the application's host. Early-binding tends to operate faster, and has the advantage of intellisense in your IDE:

Using early binding and late binding in Automation
http://support.microsoft.com/kb/245115

Early vs. Late Binding
http://word.mvps.org/faqs/interdev/earlyvslatebinding.htm

Search and Replace
One thing to be aware of is that the find and replacement objects may not work as you would expect. Rather than searching the whole document, it searches just the main text. If you have text frames in the document, these will be ignored. Instead, you have to loop through all the StoryRanges, and search the content of each. Here's what I do in VB.NET to search the main text story and text frames:

Private Sub FindReplaceAll(ByVal objDoc As Object, ByVal strFind As String, ByVal strReplacement As String)
    Dim rngStory As Object

    For Each rngStory In objDoc.StoryRanges
        Do
            If rngStory.StoryType = wdMainTextStory Or rngStory.StoryType = wdTextFrameStory Then
                With rngStory.Find
                    .Text = strFind
                    .Replacement.Text = strReplacement
                    .Wrap = wdFindContinue
                    .Execute(Replace:=wdReplaceAll)
                End With
            End If
            rngStory = rngStory.NextStoryRange
        Loop Until rngStory Is Nothing
    Next rngStory
End Sub

StoryRanges Collection Object
http://msdn.microsoft.com/en-us/library/bb178940%28office.12%29.aspx

0
2

I have a long history regarding document generation and mail merge. In the old days we were using Office COM extensively even in server side (ASP) applications. In years we have learnt that this approach was causing many problems and today I’m always advocating against using Office COM (Word automation) in almost any scenario.

With the Microsoft’s introduction of Open XML SDK we managed to create a solid mail-merge component that was many times faster and much more robust than the solution(s) with Office COM. In my experience Open XML SDK allows a developer to create a solid solution, but it takes a lot of effort and time to make it useful and robust.

There are several good document generation/processing libraries on the market. We later ended up purchasing one and in my opinion creating your own solution (based on Open XML SDK or Office COM) simply never pays off.

Currently we are using Docentric Toolkit which is a general purpose document processing library and even better template-based/mail-merge toolkit for .NET. It allows template design in MS Word and then populating them with application data and producing final documents in different formats.

1

You can look into using XSL to generate some WordML.

This technique is definitely convoluted but gives you a lot power in your layout.

1
  • +1 Now this is interesting stuff allow human-readable source to create a Word doc without needing MS Word installed on the server. I'm going to add this link msdn.microsoft.com/en-us/library/…
    – John K
    Jun 25, 2010 at 22:23
0

You don't need any 3rd party controls to create a Word document. From 2007 and onward Word can read html as a word document. You simply save any web page with the ".doc" extension and Word will sort it out.

Simply create your web page with whatever formatting you want then save it with a .doc extension.

I used HttpWebRequestto call the Url (with parmaters) to my page then used WebResponse and Stream to get my page into a buffer, then StreamReader and StreamWriter to save it to an actual document. I've then got my own custom function to download the file.

If anyone wants my code let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.