Word2003-based programming in VS2003

xiaoxiao2021-04-04  242

I have compiled a "Visual Studio .Net Automation MS Word", it is based on Word2000. In fact, since I have not too much to deal with this, I haven't continued research, yesterday, a garden is against the article. I need a problem. "I need it now to copy the content of a document to another document content, first copy the first document to the clipboard, but how should I locate the paste position to another? Then?". There is no way, I can only re-come up with the code, but now is Word2003, so compile code is to modify, so some things for Word2003, write something, I hope to do this kind of thing Friends who have no hand from hand.

The necessary assembly

Select in the COM tab:

Note: This is Office2003, if it is other version, it is different.

Several simple operations

Create a MS Word COM object, this object is we used to access the various functions provided by Word, if you want to understand which functions can be used, you'd better go to Word to find, open Word, press [Alt F11] , Then press F1, you can open help.

Word.ApplicationClass vk_word_app = new word.Applicationclass ();

VK_WORD_APP.Visible = True;

vk_word_app.activate ();

The above VK_Word_App is a Word COM object and activates it, and it is displayed. If set to false, Word runs in the background, can't see its interface.

Open a Word document

Object filename = OpenFiledialog.FileName;

Object vk_confirmconversions = true;

Object vk_readonly = false;

Object vk_addtorecentfiles = true;

Object vk_passworddocument = ""

Object vk_passwordTemplate = ""

Object vk_revert = false;

Object vk_writepassworddocument = ""

Object vk_writepasswordTemplate = ""

Object vk_format = word.wdopenformat.wdopenFormatauto;

Object vk_encoding = system.reflection.Missing.Value;

Object vk_visible = TRUE;

Object vk_openconflictdocument = false;

Object vk_openandrepair = true;

Object vk_documentdirection = word.wddocumentdirection.wdlefttoright;

Object vk_noencodingdialog = true;

Object vk_xmltransform = system.reflection.missing.value; object vk_false = false;

Object vk_true = true;

Object vk_dynamic = 2;

Object vk_missing = system.reflection.Missing.Value;

Startword ();

// Open a document

VK_MY_DOC = VK_WORD_APP.Documents.Open (Ref FileName,

Ref vk_confirmconversions, ref vk_readonly, ref vk_addtorecentfiles, ref vk_passworddocument,

REF VK_PASSWORDTEMPLATE, REF VK_REVERT, REF VK_WRITEPASSWORDDocument, Ref vk_writepasswordtemplate,

Ref vk_format, ref vk_encoding, ref vk_visible, ref vk_openconflictdocument, ref vk_openandrepair, ref vk_documentdirection, ref vk_xmltransform;

In fact, there are many parameters in this, but for everyone to see more, I am still a clear written, it is easier to understand what the parameters mean (these parameters means, please follow the method mentioned above) Looking in Word).

Create a new document

The following code tells us that if you create a new Word document, add some content to the document:

Word.document vk_new_doc = vk_word_app.documents.add (Ref vk_missing,

REF VK_MISSING, REF VK_MISSING, REF VK_Visible;

Doc.select ();

vk_word_app.selection.copy ();

// Paste Into new Document As Unformatted Text

VK_NEW_DOC.SELECT ();

vk_word_app.selection.pasteespecial (Ref vk_missing, ref vk_false,

REF VK_MISSING, REF VK_FALSE, REF VK_DATYPE, REF VK_MITISING, REF VK_MISSING

Save document

Just now create a document, now save it, and turn it off:

Object vk_lockcomments = false;

Object vk_fileformat = word.wdsaveformat.wdformator;

Object vk_password = ""

Object vk_addtorecentfiles = true;

Object vk_writepassword = ""

Object vk_readonlyrecomended = false;

Object vk_embedtruETypefonts = true;

Object vk_savenativePictureFormat = true;

Object vk_saveformsdata = false;

Object vk_saveasaocletter = true;

Object vk_encoding = system.reflection.Missing.Value;

Object vk_nsertlinebreaks = false;

Object vk_allowsubstitutions = false;

Object vk_linending = word.wdlineendingtype.wdcrlf;

Object vk_addbidimarks = false;

vk_new_doc.content.insertafter ("This is my new document !!!");

// Save a document

VK_NEW_DOC.SAVEAS (Ref vk_filename, ref vk_fileformat, ref vk_lockcomments, ref vk_password, ref vk_addtorecentfiles, ref vk_writepassword,

REF VK_READONLYRECOMMENDED, REF VK_EMBEDTRUETYPEFONTS, REF VK_SAVENATIVEPICTUREFORMAT, REF VK_SAVEFORMSDATA, REF VK_SAVEASAOCELETER, REF VK_ENCODING,

REF VK_NSERTLINEBREAKS, REF VK_ALLOWSUBSTINS, REF VK_LINEENDING, REF VK_ADDBIDIMARKS;

// Close a new document

Object vk_savechanges = word.wdsaveOptions.wdsavechange;

Object vk_originalformat = word.wdoriginalFormat.wdWordDocument;

Object vk_routedocument = false;

Doc.close (Ref vk_savechanges, ref vk_originalformat, ref vk_routedocument);

vk_new_doc.close (Ref vk_savechanges, ref vk_originalformat, ref vk_routedocument);

Find replacement

Finding and replacing is a common function, and my code is using the following methods:

Private Void FindAndreplace (Object Findtext, Object Replacewith, Object Wrap)

{

Object vk_findtext = findtext;

Object vk_matchcase = false;

Object vk_matchwholeword = false;

Object vk_matchWildcards = false;

Object vk_matchsoundslike = false;

Object vk_matchallwordforms = false;

Object vk_forward = true;

Object vk_wrap = wrap;

Object vk_format = false;

Object vk_replacewith = replacewith; object vk_replace = word.wdreplace.wdreplaceAll;

Object vk_matchkashida = false;

Object vk_matchdiacritics = false;

Object vk_matchalefhamza = false;

Object vk_matchControl = false;

vk_word_app.selection.find.execute (Ref vk_findtext, ref vk_matchcase, ref vk_matchwholeword,

REF VK_MATCHWILDCARDS, REF VK_MATCHSOSLIKE, REF VK_MATCHSOSLIKE, REF VK_MATCHALLWORDFORMS, Ref vk_forward, ref vk_wrap, ref vk_format,

REF VK_REPLACEWITH, REF VK_REPLACE, REF VK_MATCHKASHIDA, REF VK_MATCHDIAMZA, REF VK_MATCHALEFHAMZA, REF VK_MATCHCONTROL

}

Automatically load the content in the clipboard into the open document

This is what the garden is asked, the way is also posted:

String content = string.empty;

Try

{

IdataObject iData = clipboard.getdataObject ();

Idata.getdataPresent (DataFormats.Text))

{

Content = (String) iData.getdata (dataformats.text);

THIS.TEXTBOX1.TEXT = Content;

}

Else

{

This.TextBox1.text = "There is no thing on the clipboard!"

}

}

Catch (Exception EX)

{

This.TextBox1.text = ex. measureage;

}

Object vk_direction = word.wdcollapsedirection.wdcollapsestart;

Doc.content.insertparagraphafter ();

Word.Range Range = Doc.Paragraphs [Doc.Paragraphs.count] .range;

Range.collapse;

Range.paste ();

Such a result is the end of the document, starting a segment and adds the content.

Set password to the document

If you want to set a password to the document, you can change it slightly, you can do it, very simple:

Doc.password = txtpassword.text;

Closedoc (DOC);

Exit Word

vk_word_app.quit (Ref vk_false, ref vk_missing, ref vk_missing);

in conclusion

In fact, the use of VS2003 is not very difficult to operate on Word, but the parameters are still a lot. If you want to use it, then you will do it. It is very simple to write because the previous compiled "Visual Studio .NET Automation MS Word" is already very detailed. If you don't understand, check Word or look at my source.

转载请注明原文地址:https://www.9cbs.com/read-131842.html

New Post(0)