Mehanizing MS Word Using Best Unity Visual Studio Code

29

All about Unity Visual Studio Code:

Unity Visual Studio Code: Someday, you may be asked to write any parser that will have to parse many documents, split them down into an organized model, and store these questions relational-database. And those documents will in all probability be written in MILLISECONDS Word. And the sad portion would be that they would not have got any structure, they will not adhere to any standard, and they will contain OLE/embedded objects. I was allocated such a task, and it also was an exciting experience for me.

Unity Visual Studio Code: Since this was my 1st such project, meaning robotizing MS Office application, I put to go and do a lot of examining automation. The good news is that there is also a bunch of stuff out there; the unhealthy information is that they are prepared for VB or VBA developers. I couldn’t come across anything for C++ coders. Long story short, On the web writing, this article makes stuff a little easier for someone else that could be assigned with a similar undertaking.

Unity Visual Studio Code: The original code I worked tirelessly on was written using Borland C++ Builder 5 Skilled. This article is, however, the C# version. By the way, if you are serious about seeing the C++ type ask for it, and I will probably post it. I encourage people to look at the C++ version some time to start to prefer the simplicity of C#.

Track record

No particular background is critical. Just have some hands-on practical experience with C#.

Using the computer

Unity Visual Studio Code – I will include some computers that will allow you to understand how to find what you need from a Word file. It doesn’t matter should you be making a console application or even a Windows application. The steps, as well as the code, is the same. To help you go ahead and create a new C# project. You may choose to produce a Windows Application that way, and it is possible to click some button.

Unity Visual Studio Code: Ok, so once you create a new project, go ahead and right-click in References in the Solution Parcourir, and select Add Reference… If the Add Reference window appears, select the COM tab. This will likely list all Component Titles available on your machine; given that we are going to use MS Phrase, we will scroll down right up until we find Microsoft Word on the lookout. 0 Object Library.

Observe: Yours might be a different type depending on the company’s version installed on your machine. This is directed at MS Word 2000.

employing System;

using System. Painting;

using System. Collections;

making use of the System. ComponentModel;

using Method. Windows. Forms;

using Method. Data;

namespace sparser

/// Summary description for Form1.

public class frmParserMainUI : System.Windows.Forms.Form

 

/// User Interface Objects

/// I have removed the user interface object, since

/// they have nothing to do with

/// the actual code and they take a lot of space.

 

/// Required designer variable.

private System.ComponentModel.Container components = null;

private System.Windows.Forms.OpenFileDialog openFileDialog;

 

Unity Visual Studio Code: The following block creates MS Word COM Object. This is the object which will be used to access WORD application functions. To see what positions are available, you can do it either within Visual Studio .NET IDE or MS Word.

/// MS Word COM Object

/// This is where we create our WORD object

private Word.ApplicationClass vk_word_app = new Word.ApplicationClass();

 

Unity Visual Studio Code: To view the functions from MS Word, launch Word, hold down the Alt key, and press F11 [Alt+F11]. This will give you the VBA window. Once there, press F1 to get the help window and do a search for the document object. This is the best source of documentation for available functions. However, the documents have been written for VBA, but you know what the process does and what parameters it takes.

Alright, the code continued…

 

/// The main entry point for the application.

[STAThread]

static void Main()

 

Application.Run(new frmParserMainUI());

 

/// Get source document. Open a FileDialog window for

/// user to select single/multiple files for

/// parsing.

private void butSourceDocument_Click(object sender, System.EventArgs e)

 

if( openFileDialog.ShowDialog() == DialogResult.OK )

 

object fileName = openFileDialog.FileName;

object saveFile = fileName + “_Vk.doc”;

object vk_read_only = false;

object vk_visible = true;

object vk_false = false;

object vk_true = true;

object vk_dynamic = 2;

object vk_missing = System.Reflection.Missing.Value;

// Let make the word application visible

vk_word_app.Visible = true;

vk_word_app.Activate();

// Let’s open the document

Word.Document vk_my_doc = vk_word_app.Documents.Open(

ref fileName, ref vk_missing, ref vk_read_only,

ref vk_missing, ref vk_missing,

ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,

ref vk_missing, ref vk_missing, ref vk_visible );

Alright, the user is given an open file dialog where they can select a Word document. Notice that we save the filename as an object. This is because the functions that we use need a reference to the item.

Unity Visual Studio Code: So now we can use our Word object to start Word. The vk_word_app achieves this.Visible = true; and vk_word_app.Activate(); The first statement makes sure that Word is visible, and the second one activates it. If you don’t want the Word instance to be visible, just set the Visible property to false.

Unity Visual Studio Code: Next, we create a Word Document object, which is done using Word.Document vk_my_doc = vk_word_app.Documents.Open( … ); Notice all the parameters which are required by the function. Most of them are NULL values, so we use vk_missing, which is a System.Reflection.Missing.Value.

So now we have created a Word instance and opened a Word document. Now let’s move on with the code…

 

// Let’s create a new document

Word.Document vk_new_doc = vk_word_app.Documents.Add(

ref vk_missing, ref vk_missing, ref vk_missing, ref vk_visible );

// Select and Copy from the original document

vk_my_doc.Select();

vk_word_app.Selection.Copy();

// Paste into new document as unformatted text

vk_new_doc.Select();

vk_word_app.Selection.PasteSpecial( ref vk_missing, ref vk_false,

ref vk_missing, ref vk_false, ref vk_dynamic,

ref vk_missing, ref vk_missing );

// close the original document

vk_my_doc.Close( ref vk_false, ref vk_missing, ref vk_missing );

 

Unity Visual Studio Code: Next, we would like to create a new document. This is just like clicking the New Blank Document button on the toolbar. So we create another Word Document object, and this time, we notice that we use vk_word_app.Documents.Add( … ); This will add a new blank form, which is also visible.

Next, we select all content from the Document which we opened, and we copy it.

Unity Visual Studio Code – Next we paste our content into the new document with a special format. The format used in the code is for plain text. This is because we want to get rid of the crap Word puts into the formatting of the text. Then we close our original document without making any changes.

vk_new_doc.Select();

FindAndReplace( “^t^t^t^t^t^t^t”, “^t”, vk_num );

// Save the new document

vk_new_doc.SaveAs( ref saveFile, ref vk_missing,

ref vk_missing, ref vk_missing, ref vk_missing,

ref vk_missing, ref vk_missing, ref vk_missing,

ref vk_missing, ref vk_missing, ref vk_missing );

// close the new document

vk_new_doc.Close( ref vk_false, ref vk_missing, ref vk_missing );

// close word application

vk_word_app.Quit( ref vk_false, ref vk_missing, ref vk_missing );

 

 

Unity Visual Studio Code: Now, let’s say if we are interested in finding and replacing operation, we would select the whole Document and use the Find and Replace function.

Note: The FindAndReplace function does not belong to the Word object or Document object. It is user-defined. The actual Find and Replaces part is defined in the following block.

Next, we want to save our changes to the new Document and close it.

And finally, quit Word.

private void FindAndReplace( object vk_find, object vk_replace,

object vk_num )

 

object vk_read_only = false;

object vk_visible = true;

object vk_false = false;

object vk_true = true;

object vk_dynamic = 2;

vk_word_app.Selection.Find.Execute( ref vk_find,

ref vk_false, ref vk_false,

ref vk_false, ref vk_false, ref vk_false, ref vk_true,

ref vk_num, ref vk_false,

ref vk_replace, ref vk_dynamic, ref vk_false,

ref vk_false, ref vk_false, ref vk_false );

Unity Visual Studio Code: The above block shows the exact FindAndReplace function. Notice that the item belongs to the vk_word_app object. Along with the process operates on the dynamic selection. The Find, in addition to the Replace function is the primary Find function with exclusive parameters. These parameters are usually identified in the documentation because I mentioned at the top of the article.

Often, the code demonstrated above demonstrates how to open a word data, create a new concept document, select, copy, paste, and do a Find in addition to the Replace function.

Unity Visual Studio Code: As you can see, when you finally understand how Concept operates and go through the certification, you will be able to automate every one of the functions that Word has got to provide.

// Let’s find the content from the document

Concept. Paragraphs vk_my_doc_p = vk_new_doc. Paragraphs;

// Count range of paragraphs in the file

longer p_count = vk_my_doc_p. Depend;

// step through the sentences