First, add reference. Solution Resource Manager-reference-add-Com-browse-C: \ Program Files \ Microsoft Office \ OFFICE11 \ MSWORD. olb I use other versions of office 2003 ,. net will automatically convert the OLB control to the DLL file usage: object oMissing = System. reflection. missing. value; Word. application oWord = new Word. application (); oWord. visible = false; // set the Word application to invisible // create a Word document
Word. Document oDoc = oWord. Documents. Add (ref oMissing, ref oMissing); // Copy and paste the oDoc. Content. Copy () file Content ();
ODoc. content. paste () // Save the document as oDoc. saveAs (ref fileName, ref saveFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // other settings oDoc. pageSetup. paperSize = Word. wdPaperSize. wdPaperA3; // page settings
ODoc. PageSetup. Orientation = Word. WdOrientation. wdOrientLandscape; // horizontal or vertical
ODoc. PageSetup. TextColumns. SetCount (2); // sub-column // close Word oWord. Application. Quit (ref B, ref oMissing, ref oMissing );
System. runtime. interopServices. marshal. releaseComObject (oWord); there are many functions in the operations of word documents through the oDoc object (which can be done by word). If you are interested, study another method: 1:
Add reference to a project, Microsoft Word 11.0 Object Library
2:
Add using Word = Microsoft. Office. Interop. Word in the program;
3:
Add
Word. Application app = new Microsoft. Office. Interop. Word. Application (); // you can open the word program.
Word. Document doc = null; // The Document opened by word will be recorded later
Word documents and word programs are not the same thing!
4:
Generally, few methods are used to extract word content.
Public override void openFile (object fileName) {}// open the document
Public override object readPar (int I) {}// read the I segment of the Word Document
Public override int getParCount () {}// returns the total number of Word documents
Public override void closeFile () {}// close the document
Public override void quit () {}// close the word program // The Directory copied from the web page sometimes has a manual line break ^ l. Replace it with the carriage return section mark first, can be read correctly
Public void replaceChar () {}5: code public override void openFile (object fileName)
...{
Try
...{
If (app. Documents. Count> 0)
...{
If (MessageBox. Show ("a Word document has been opened. Do you want to close it and open it again? "," Prompt ", MessageBoxButtons. YesNo) = DialogResult. Yes)
...{
Object unknow = Type. Missing;
Doc = app. ActiveDocument;
If (MessageBox. Show ("do you want to save it? "," Save ", MessageBoxButtons. YesNo) = DialogResult. Yes)
...{
App. ActiveDocument. Save ();
} App. ActiveDocument. Close (ref unknow, ref unknow, ref unknow );
App. Visible = false;
}
Else
...{
Return;
}
}
}
Catch (Exception)
...{
// MessageBox. Show ("You may have disabled the document ");
App = new Microsoft. Office. Interop. Word. Application ();
} Try
...{
Object unknow = Type. Missing;
App. Visible = true;
Doc = app. Documents. Open (ref fileName,
Ref unknow,
Ref unknow,
Ref unknow, ref unknow );
}
Catch (Exception ex)
...{
MessageBox. Show ("error:" + ex. ToString ());
}
}
Public override object readPar (int I)
...{
Try
...{
String temp = doc. Paragraphs [I]. Range. Text. Trim ();
Return temp;
}
Catch (Exception e )...{
MessageBox. Show ("Error:" + e. ToString ());
Return null;
}
} Public override int getParCount ()
...{
Return doc. Paragraphs. Count;
} Public override void closeFile ()
...{
Try
...{
Object unknow = Type. Missing;
Object saveChanges = Word. WdSaveOptions. wdPromptToSaveChanges;
App. ActiveDocument. Close (ref saveChanges, ref unknow, ref unknow );
}
Catch (Exception ex)
...{
MessageBox. Show ("Error:" + ex. ToString ());
}
} Public override void quit ()
...{
Try
...{
Object unknow = Type. Missing;
Object saveChanges = Word. WdSaveOptions. wdSaveChanges;
App. Quit (ref saveChanges, ref unknow, ref unknow );
}
Catch (Exception)
...{}
} Public void replaceChar ()...{
Try
...{
Object replaceAll = Word. WdReplace. wdReplaceAll;
Object missing = Type. Missing; app. Selection. Find. ClearFormatting ();
App. Selection. Find. Text = "^ l"; app. Selection. Find. Replacement. ClearFormatting ();
App. Selection. Find. Replacement. Text = "^ p"; app. Selection. Find. Execute (
Ref missing,
Ref missing,
Ref replaceAll, ref missing, ref missing );
}
Catch (Exception e)
...{
MessageBox. Show ("document error, please operate again ");
}
}
6:
For example, if you want to read a sentence or an article, you only need to read the doc. change Paragraphs [I] (In readPar) to doc. sentences [I] Or doc. content, because it is Microsoft's stuff, so there is no obstacle in use, coupled with the current vs2005 is very intelligent, so first from java to c # On 7:
In fact, reading word in c # is not so troublesome, but if you want to extract multiple formats such as txt and ppt, you can write an abstract class, which is convenient to call, this is why override exists at the beginning of my program method, so I always need to consider the general purpose, so I have more code.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.