In the programming world, knowing how to read the Microsoft Word DOCX file format can save you time when you need contents in a DOCX file. DOCX is a new format available in Microsoft Office 2007 and greater. This format supports XML standard, which was developed for these versions of Microsoft Office products. XML is simply a set of rules for encoding documents such as a DOCX. You can use Microsoft Visual Basic .NET (VB.NET) to read and display the contents of a DOCX file.
- Skill level:
Other People Are Reading
Things you need
- Microsoft Visual Basic Express
- Open XML Format SDK
Open Microsoft Visual Basic Express and select "New Project..." from the left pane of your screen. Click "Visual Basic" under "Installed Templates" and double-click "Console Application."
Click the "Toolbox" pane and double-click "Button" to add a new button to your Form. Double-click "Textbox" to add a new text box control to your Form.
Click the "Project" menu and select "<projectname> Properties." Click "References," then select "Add." Select ".NET" and click "DocumentFormat.OpenXml." Click "OK." Double-click "Button1" to open the VB window.
Copy and paste the following code in the very top of your VB.NET module to declare the namespaces:
Copy and paste the following code under "Button1_Click" to define the path and document to open:
Dim strDoc As String = "C:\docxFile.docx" Dim txt As String Dim stream As Stream = File.Open(strDoc, FileMode.Open)
Edit the following line of code and type the path and name to your document:
Dim strDoc As String = "C:\docxFile.docx"
Copy and paste the following to call the procedure to actually read the document:
OpenAndAddToWordprocessingStream(stream, txt) stream.Close()
Copy and paste the following to display the text read in the text box control:
Me.TextBox1.Text = txt
Copy and paste the following procedure to open the DOCX file and return the contents read:
Public Sub OpenAndAddToWordprocessingStream(ByVal stream As Stream, ByRef txt As String) Dim wordprocessingDocument As WordprocessingDocument = wordprocessingDocument.Open(stream, True) Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body txt = body.InnerText.ToString wordprocessingDocument.Close() End Sub
Press "F5" to run the program, then click "Button1" to execute the code.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for