none
Importing Word library into VB.net and instantiating its methods within their types RRS feed

  • Question

  • Hello again:

    The last question, regarding sentences with capital letters, somebody informed me that I have closed the thread by mistake.

    Here, I start a new question:

    Already, I have imported the Word library into VB.NET source code.

    Imports Microsoft.Office.Interop.Word

    Then, I have declared a parameter as Words type

        Private WrdX As Words

    Now, I need to instantiate it; so that I can access its methods within the Words type.

    Any solutions?

    Regards,

    JohnDBCTX


    jp

    Tuesday, June 12, 2018 8:05 AM

All replies

  • For example :

    Imports Word = Microsoft.Office.Interop.Word
    
    '...
    
    Dim oWordApplication As Word.Application = New Word.Application
    Dim oWordDocuments As Word.Documents = oWordApplication.Documents
    Dim oWordDocument As Word.Document = oWordDocuments.Open("e:\\test.doc")
    Dim oWords As Word.Words = oWordDocument.Words
    Dim nNbWords = oWords.Count
    oWordApplication.Quit()
    
    Marshal.ReleaseComObject(oWords)
    Marshal.ReleaseComObject(oWordDocument)
    Marshal.ReleaseComObject(oWordDocuments)
    Marshal.ReleaseComObject(oWordApplication)


    Tuesday, June 12, 2018 8:38 AM
  • It is unclear in regards to the Marshal.

    What does the Marshal represent?  Does it refer to within the C# syntax library?

    Still, I want to perform the following tasks that does not need to create an instance of a Word App.

    Can that be done alone?

    Regards,

    JohnDBCTX


    jp

    Tuesday, June 12, 2018 9:15 AM
  • Marshal.ReleaseComObject allows to release each COM Interface.

    It is mainly a problem with Excel : if you forget a release, an instance of Excel.exe will remain

    Tuesday, June 12, 2018 9:55 AM
  • The Words collection is a COM object that cannot be instantiated without a running instance of the Word application.
    Tuesday, June 12, 2018 11:21 AM
  • As noted you'll need to create an instance of the Word application.

    But if this is just about getting capitalized words from a string, then there is already an answer (using Regex) in your other thread.  Or you can just use a simple little parsing function, something like:

    Public Function GetCapitalizedWords(value As String) As IEnumerable(Of String)
        Dim result As New List(Of String)
        Dim s = -1
        Dim l = 0
        For i = 0 To value.Length - 1
            Dim c = value(i)
            If Char.IsUpper(c) Then
                If s = -1 Then s = i
                l += 1
            ElseIf s > -1 AndAlso c <> "-"c AndAlso Not Char.IsLetter(c) Then
                result.Add(value.Substring(s, l))
                s = -1
                l = 0
            ElseIf s > -1 Then
                l += 1
            End If
        Next
        If s > -1 Then result.Add(value.Substring(s, l))
        Return result
    End Function

    It doesn't seem like an instance of Word should be necessary for this purpose.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, June 12, 2018 1:34 PM
    Moderator
  • The below link should help you as well:

    How to automate Word from Visual Basic .NET to create a new document


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, June 12, 2018 2:32 PM
  • Hi JohnDBCTX,

    According to your description,  do you not know how to use word.Application? Here is some article about opening or creating document using vb.net, you can take a look:

    http://www.authorcode.com/how-to-open-existing-word-document-in-vb-net/

    http://www.authorcode.com/create-a-new-word-document-using-vb-net/

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, June 13, 2018 2:26 AM
    Moderator