Option Strict Off
Option Explicit On

Imports Microsoft.Office.Interop
<System.Runtime.InteropServices.ProgId("ODocument_NET.ODocument")> Public Class ODocument

    Public WordGlobal_definst As Word.Global
    Public newdoc As Word.Document
    Public Function Open(ByRef inifile As String, ByRef docname As String, Optional ByRef templatename As String = "") As Boolean

        Dim opt As odaba.Option
        Dim exp_path As String
        Dim exp_template As String

        WordGlobal_definst = New Word.Global
        opt = New odaba.Option(docname)
        exp_path = opt.toString
        If templatename <> "" Then
            opt = New odaba.Option(templatename)
            exp_template = opt.toString
            newdoc = WordGlobal_definst.Documents.Add(Template:=exp_template, Visible:=True)
            newdoc.SaveAs(exp_path)
        Else
            newdoc = WordGlobal_definst.Documents.Add(Template:=exp_path, Visible:=True)
        End If
        newdoc.Activate()
        WordGlobal_definst.Selection.EndKey(Unit:=Word.WdUnits.wdStory)
        Open = True
    End Function
    Public Function Close(Optional ByVal save As Boolean = True, Optional ByVal compatibility As Boolean = True) As Boolean

        If save Then
            '            newdoc.Save()
            '            newdoc.Close(Word.WdSaveOptions.wdSaveChanges)
        Else
            newdoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
        End If

        'UPGRADE_NOTE: Object newdoc may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1029"'
        newdoc = Nothing
        Close = True

    End Function
    Public Function Save() As Boolean

        newdoc.Save()
        Save = True

    End Function
    Public Function Find(ByRef string1 As String) As Boolean

        WordGlobal_definst.Selection.SetRange(Start:=WordGlobal_definst.Selection.Range.End, End:=WordGlobal_definst.Selection.Range.End)
        WordGlobal_definst.Selection.Find.ClearFormatting()
        With WordGlobal_definst.Selection.Find
            .Text = string1
            .Forward = True
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Find = WordGlobal_definst.Selection.Find.Execute

    End Function
    Public Function ReplaceText(ByRef string1 As String, ByRef string2 As String) As Boolean

        WordGlobal_definst.Selection.Find.ClearFormatting()
        WordGlobal_definst.Selection.Find.Replacement.ClearFormatting()
        With WordGlobal_definst.Selection.Find
            .Text = string1
            .Replacement.Text = string2
            .Forward = True
            .Wrap = Word.WdFindWrap.wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        WordGlobal_definst.Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)
        ReplaceText = True
    End Function
End Class
