Use of NetOffice.WordApi

  • Thread starter Thread starter Cynthia_1968
  • Start date Start date
C

Cynthia_1968

Guest
Hello world,

I am working on an Advanced Text-To-Speech Windows Application (it also enables Cortana!), and I thought it'd be awesome if the application can also open MS-Word documents instead of only RTF, TXT and base64 encoded RTF files (tts+).

I found various solutions, but most NuGet packages are not for free, I found one that was free (and works, from Xceed), but Xceed doesn't detect line breaks.

And since interop.office doesn't work on my PC, I decided to give it a go with NetOffice, which offers a MIT license, so I'm free to distribute it along with my TTS application as open source :-)

The only problem is: I don't know how to open a word document and then load the document into a richtextbox.

So, if anyone could tell me how to achieve this, you'd be the man (or woman) of the hour--well to me anyway!

Best regards,

Cynthia


--- This is partly my source code:, showing the file open routine --


Imports NetOffice
Imports Word = NetOffice.WordApi
Imports NetOffice.WordApi.Enums
Imports NetOffice.WordApi.Tools.Utils


Private Sub LoadFromTextFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadFromTextFileToolStripMenuItem.Click
'-- Load text file
Dim CurDir As String
CurDir = My.Computer.FileSystem.CurrentDirectory

flTxtFile.Filter = "Open Text files (*.txt, *.rtf, *.docx or read only TTS++ files *.tts+)|*.txt; *.rtf; *.docx;*.tts+|RichTextFormat documents (*.rtf)|*.rtf|MS Word - as plain text (*.docx)|*.docx|Rich Text Format TTS++ document, read only (*.tts+)|*.tts+"
If flTxtFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
ListBox1.Items.Add(flTxtFile.FileName)
SelectVoice.Text = GetFilenameFromPath(flTxtFile.FileName)

Dim ChkExtenstion As Integer = SelectVoice.TextLength - 4
Dim ChkTxtLenght As Integer = SelectVoice.TextLength
Dim GetExtenstion As String
SelectVoice.SelectionStart = ChkExtenstion
SelectVoice.SelectionLength = ChkTxtLenght
GetExtenstion = LCase(SelectVoice.SelectedText)
txtSafeFile.Text = flTxtFile.FileName

If GetExtenstion = "docx" Then
'--- Load RTF text and keeps it's format

' (I am stuck here, because I cannot open the Word Document....

Dim wordApplication As New NetOffice.WordApi.Application

Dim MyDoc As Word.Document
MyDoc = wordApplication.Documents.Open(flTxtFile.FileName)
MyDoc.Content.Select()
MyDoc.Content.Copy()

Clipboard.Clear()
Clipboard.SetText(MyDoc.Content.Text, TextDataFormat.Rtf)

ReadText.Text = ""
ReadText.Paste()
ReadText.ReadOnly = False
SaveTextFileToolStripMenuItem.Enabled = True
ContextMenuStrip1.Enabled = True

btnPrint.Enabled = True
btnPageSetup.Enabled = True
btnPrintPreview.Enabled = True
'MsgBox("hello, this is after the new procedure: docx " & doc.ToText)

Exit Sub
End If

If GetExtenstion = ".rtf" Then
'--- Load RTF text and keeps it's format
ReadText.ReadOnly = False
SaveTextFileToolStripMenuItem.Enabled = True
ContextMenuStrip1.Enabled = True

ReadText.LoadFile(flTxtFile.FileName, RichTextBoxStreamType.RichText)
btnPrint.Enabled = True
btnPageSetup.Enabled = True
btnPrintPreview.Enabled = True
Exit Sub
End If

If GetExtenstion = "tts+" Then
'--- Load TTS file as RTF text and keeps it's format
' set output as ReadOnly
EncodeText.LoadFile(flTxtFile.FileName, RichTextBoxStreamType.RichText)
Dim decodedBytes As Byte()
Try
decodedBytes = Convert.FromBase64String(EncodeText.Text)
ReadText.Rtf = Encoding.Default.GetString(decodedBytes)
Catch ex As Exception
MsgBox("This is not a valid TTS+ file. It's probable a file with the TTS extension, but we'll try to open it for you anyway :-)")
ReadText.Rtf = EncodeText.Rtf

End Try
ReadText.ReadOnly = True
ContextMenuStrip1.Enabled = False
btnPrint.Enabled = False
btnPageSetup.Enabled = False
btnPrintPreview.Enabled = False
'-- Disable save file
SaveTextFileToolStripMenuItem.Enabled = False
Exit Sub
End If
If GetExtenstion = ".txt" Then
ReadText.ReadOnly = False
SaveTextFileToolStripMenuItem.Enabled = True
ContextMenuStrip1.Enabled = True
btnPrint.Enabled = True
btnPageSetup.Enabled = True
btnPrintPreview.Enabled = True
ReadText.LoadFile(flTxtFile.FileName, RichTextBoxStreamType.PlainText)
Exit Sub
End If
End If
End Sub

Continue reading...
 

Similar threads

Back
Top