Get the code for the Microsoft Word document from ASP.NET

  • 2020-05-12 02:27:46
  • OfStack

background
Automation (Automation) is a process that allows applications written in programming languages such as Visual Basic.NET or C# to programmatically control other applications. Automation to Word allows you to do things like create new documents, add text to documents, mail merge, and control document formatting. With Word and other Microsoft Office applications, almost everything you can do manually on the user panel can be automated programmatically. Word implements this programming functionality through an object model (programmatically functionality). The object model is a family of 1 classes and methods that provide services similar to the logical components of Word. For example, one application object, one document object, and one paragraph object, each of which contains the functionality of the corresponding component of Word.

project
To do step 1 of Word in.NET, you need to add an COM reference to your project by right-clicking on the reference in the solution window - > Add a reference. Click the COM TAB to find Microsoft Word 10.0 Object Library. Click select to add, and click ok to return.
This will automatically place an assembly (assembly) in your application folder to specify the COM interface to Word.
You are now ready to generate an instance of the Word application.

Word.ApplicationClass oWordApp = new Word.ApplicationClass();
You can call the very interesting methods and properties that Microsoft Word provides you with to manipulate documents in Word format. The best way to learn how to manipulate the Word,Excel, and PowerPoint object models is to use a macro recorder in these Office applications:
1. Select "record new macro" from the "macro" option in the "tools" menu, and then perform the task you are interested in.
2. Select "stop recording" from the "macro" option in the "tools" menu.
3, 1 once you have finished recording, select "macro" under the "macro" option in the "tools" menu, select the macro you recorded, and click "edit".
This will take you to the generated VBA code, which completes the task you recorded. Note that the recorded macro is not the best code in most cases, but it provides a quick and usable example.
For example, to open an existing file and add some text:
 
object fileName = "c:\\database\\test.doc"; 
object; 
object isVisible = true; 
object missing = System.Reflection.Missing.Value; 
Word.ApplicationClass oWordApp = new Word.ApplicationClass(); 
Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, 
ref missing,ref readOnly, 
ref missing, ref missing, ref missing, 
ref missing, ref missing, ref missing, 
ref missing, ref missing, ref isVisible, 
ref missing,ref missing,ref missing); 
oWordDoc.Activate(); 
oWordApp.Selection.TypeText("This is the text"); 
oWordApp.Selection.TypeParagraph(); 
oWordDoc.Save(); 
oWordApp.Application.Quit(ref missing, ref missing, ref missing); 

Or to open a new document and save it:
 
Word.ApplicationClass oWordApp = new Word.ApplicationClass(); 
Word.Document oWordDoc = oWordApp.Documents.Add(ref missing, 
ref missing,ref missing, ref missing); 
oWordDoc.Activate(); 
oWordApp.Selection.TypeText("This is the text"); 
oWordApp.Selection.TypeParagraph(); 
oWordDoc.SaveAs("c:\\myfile.doc"); 
oWordApp.Application.Quit(ref missing, ref missing, ref missing); 

In C#, the open method of the Word document class is defined as: Open(ref object, ref object, ref object, ref object, ref object, ref object,ref ref object, ref object, ref object, ref object, ref object) ref object, ref object, ref object) This means that the Open method of C# accepts 15 required parameters, each of which must be prefixed with the ref keyword and each of which must be of type Object. Since the first parameter is a file name, usually an String value in Visual Basic.NET, we must declare a variable of type Object to save the string value of C#. The code is as follows:

object fileName = "c:\\database\\test.doc";
Although we in the Open method only need to use the first parameter, but remember C # does not allow optional parameters, so we provide in the form of Object type of variable 14 parameters, the rest of them System. Reflection. Missing. Value value.

Use the template
If you use automation to create documents that are all 1 to 1, it is convenient to use predefined templates to process new documents. Using templates in your Word automated client has two significant advantages over using no templates:
· you can have more control over the format of your documents and the location of objects
· you can build your documentation with less code
Using templates, you can adjust the position of tables, paragraphs, and other objects in a document, as well as the formatting of those objects. By using automation, you can create a document based on your template using only the following code:

Word.ApplicationClass oWordApp = new Word.ApplicationClass();
object oTemplate = "c:\\MyTemplate.dot";
oWordDoc = oWordApp.Documents.Add(ref oTemplate,
ref Missing,ref Missing, ref Missing);
In your template, you can define bookmarks so that your automated client can fill in variable text at specific points in the document, as follows:

object oBookMark = "MyBookmark";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
Another advantage of using templates is that you can create a storage format style that you want to apply at runtime, as follows:

object oStyleName = "MyStyle";
oWordDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
Using CCWordApp class
This project contains one file: CCWordAPP.cs. I don't want to write code every time to insert text, open a file, etc. So I decided to write an CCWordApp class to include most of the important methods. The following is a brief description of this class and its methods.
 
public class CCWordApp 
{ 
//it's a reference to the COM object of Microsoft Word Application 
private Word.ApplicationClass oWordApplic; 
// it's a reference to the document in use 
private Word.Document oWordDoc; 
// Activate the interface with the COM object of Microsoft Word 
public CCWordApp(); 
// Open an existing file or open a new file based on a template 
public void Open( string strFileName); 
// Open a new document 
public void Open( ); 
// Deactivate the interface with the COM object of Microsoft Word 
public void Quit( ); 
// Save the document 
public void Save( ); 
//Save the document with a new name as HTML document 
public void SaveAs(string strFileName ); 
// Save the document in HTML format 
public void SaveAsHtml(string strFileName ); 
// Insert Text 
public void InsertText( string strText); 
// Insert Line Break 
public void InsertLineBreak( ); 
// Insert multiple Line Break 
public void InsertLineBreak( int nline); 
// Set the paragraph alignment 
// Possible values of strType :"Centre", "Right", "Left", "Justify" 
public void SetAlignment(string strType ); 
// Set the font style 
// Possible values of strType :"Bold","Italic,"Underlined" 
public void SetFont( string strType ); 
// Disable all the style 
public void SetFont( ); 
// Set the font name 
public void SetFontName( string strType ); 
// Set the font dimension 
public void SetFontSize( int nSize ); 
// Insert a page break 
public void InsertPagebreak(); 
// Go to a predefined bookmark 
public void GotoBookMark( string strBookMarkName); 
// Go to the end of document 
public void GoToTheEnd( ); 
// Go to the beginning of document 
public void GoToTheBeginning( ); 

In this way, the operation of opening an existing file is:
CCWordApp test ;
test = new CCWordApp();
test.Open ("c:\\database\\test.doc");
test.InsertText("This is the text");
test.InsertLineBreak;
test.Save ();
test.Quit();
details
Example projects include:
CCWordApp.cs - the class
CreateDocModel.aspx: create an example of a template-based document and bookmark.
CreateNewDoc.aspx: example of creating a document and inserting some text.
ModifyDocument.aspx: an example of opening an existing document and adding some text later.
template\ template1.dot: example of 1 template (used in CreateDocModel.aspx).
Remember 1 that the directory where you save your files must be writable. See the Web.config file to modify the path.
reference
Microsoft Word Objects
Converting Microsoft Office VBA Macros to Visual Basic .NET and C#
HOWTO: Automate Microsoft Word to Perform a Mail Merge from Visual Basic .NET
A Primer to the Office XP Primary Interop Assemblies
HOWTO: Find and Use Office Object Model Documentation
Creating and Opening Microsoft Word Documents from .NET using C#

Related articles: