none
How to set object to one of two Word version available RRS feed

  • Question

  • I have this to start with:

    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Word
    Imports System.Runtime.InteropServices

    Module modWord

        Public WithEvents wd As Word.Application
        Public wdEvents As Word.ApplicationEvents2_Event

    When this runs, "wd" is set to Office 14. I have two Office versions on my hard disk, version 14 and 16. How do you force VB to pick a particular one?


    Robert Homes

    Saturday, September 23, 2017 1:30 PM

Answers

  • But what you provided doesn't actually answer my question. What I want to know is how do I assign a particular version of Word to a variable so that I can use that variable to programatically "tell a particular version of Word what to do". I don't need to know how to manually run a particular version of Word. I need to know how to (in my program) assign a Word version to a variable in order to use the API for that version. How do you do that? 


    Robert Homes


    Hi Robert,

    You mean that you want to assign a particular of word to a variable, the following is to create one Word.Application, it is the something you want?

    Dim oWord As Word.Application
            Dim oDoc As Word.Document
    
            'Start Word and open the document template.
            oWord = CreateObject("Word.Application.16")
            oWord.Visible = True
            oDoc = oWord.Documents.Add

    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.

    • Marked as answer by Robert Homes Monday, September 25, 2017 11:50 AM
    Monday, September 25, 2017 7:17 AM
    Moderator

All replies

  • HiRobert,

    Please take a look the following code, The following code snippet gets a list of all the installed copies of Word on the computer.

     Dim reg As RegistryKey
        Dim subKey As RegistryKey
        Dim rtn As New Dictionary(Of String, String)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Office")
            If reg IsNot Nothing Then
                For Each subKeyName As String In reg.GetSubKeyNames
                    subKey = reg.OpenSubKey(subKeyName)
                    If subKey IsNot Nothing Then
                        If subKey.GetSubKeyNames().Contains("Word") Then
                            subKey = subKey.OpenSubKey("Word\InstallRoot")
                            rtn.Add(subKeyName, subKey.GetValue("Path").ToString)
                        End If
                    End If
                Next
            End If
            For Each kvp In rtn
                MessageBox.Show(String.Format("Version: {0} at '{1}Word.exe'", kvp.Key, kvp.Value))
            Next
        End Sub

    Now you have the location of the installed copies of Word and their versions, you can create a form which lists them, to allow for the user to select which version they wish to use.

    More detailed info , please refer to the this link:

    https://stackoverflow.com/questions/6882010/how-to-choose-which-excel-version-to-open-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.

    Monday, September 25, 2017 3:56 AM
    Moderator
  • Cherry, your code may be helpful (although I had already devised a different and rudimentary way to find the locations on the hard drive of Office programs, your method seems much better). But what you provided doesn't actually answer my question. What I want to know is how do I assign a particular version of Word to a variable so that I can use that variable to programatically "tell a particular version of Word what to do". I don't need to know how to manually run a particular version of Word. I need to know how to (in my program) assign a Word version to a variable in order to use the API for that version. How do you do that? It doesn't seem that the line "Public WithEvents wd As Word.Application" can do that, since it seems to just randomly grab whatever version that pops up first and assigns that one to the "wd" variable. Once I know how to assign a particular version of Word to "wd" I can then do stuff like this: "wd.documents.add" (to create a new blank document in that version of Word), or "wd.activedocument.save" (to save whichever document is currently active in that version of Word).

    Can you help me withTHAT?


    Robert Homes


    Monday, September 25, 2017 6:37 AM
  • But what you provided doesn't actually answer my question. What I want to know is how do I assign a particular version of Word to a variable so that I can use that variable to programatically "tell a particular version of Word what to do". I don't need to know how to manually run a particular version of Word. I need to know how to (in my program) assign a Word version to a variable in order to use the API for that version. How do you do that? 


    Robert Homes


    Hi Robert,

    You mean that you want to assign a particular of word to a variable, the following is to create one Word.Application, it is the something you want?

    Dim oWord As Word.Application
            Dim oDoc As Word.Document
    
            'Start Word and open the document template.
            oWord = CreateObject("Word.Application.16")
            oWord.Visible = True
            oDoc = oWord.Documents.Add

    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.

    • Marked as answer by Robert Homes Monday, September 25, 2017 11:50 AM
    Monday, September 25, 2017 7:17 AM
    Moderator
  • That's it!

    So, if I want to force "wd" to point Word version 16, all I have to do is add ".16" to the end of "Word.Application". That is exactly what I wanted to know. Since I also have Word version 14, I can make "wd" point to it with "Word.Application.14".

    Now, what if I want "wd" to point to Word 360? Would that be "Word.Application.360"?

    BTW, where did you find this information?


    Robert Homes


    • Edited by Robert Homes Monday, September 25, 2017 11:54 AM added last sentence
    Monday, September 25, 2017 11:52 AM
  • Cherry, here's the code I used to find the Word versions on my computer:

        Private Sub LocateOfficeFolders()
            Const Ti = "Locate Office Folders"
            Dim D$
            Dim found As Boolean, m$

            MainFrm.Cursor = Cursors.WaitCursor

            Do
                found = False
                D$ = "c:\Program Files\Microsoft Office"
                If Directory.Exists(D$) Then found = LocateFolders(D$)
            Loop Until Not found

            Do
                found = False
                D$ = "c:\Program Files (x86)\Microsoft Office"
                If Directory.Exists(D$) Then found = LocateFolders(D$)
            Loop Until Not found

            MainFrm.Cursor = Cursors.Default

            If lstFolders.Items.Count = 0 Then
                m$ = "Search in Microsoft Office folders in" & vbCr _
                & "c:\Program Files and c:\Program Files (x86)" & vbCr & vbCr _
                & vbTab & "was unsuccessful"
                MsgBoxer(m$, MsgBoxStyle.OkOnly, Ti, "Critical")
            End If

            SearchDone = True
            If lstFolders.Items.Count > 0 Then
                btnSearch.Enabled = False
                btnOk.Enabled = True
                btnApps.Enabled = True
                btnExplore.Enabled = True
            End If
        End Sub

        Private Function LocateFolders(ByRef D$) As Boolean
            Const WordExe = "WINWORD.EXE"
            Dim Fold As DirectoryInfo, SubFold As DirectoryInfo, Fi As FileInfo, found As Boolean
            found = False

            Fold = My.Computer.FileSystem.GetDirectoryInfo(D$)
            For Each Fi In Fold.GetFiles
                If UCase(Fi.Name) = WordExe Then
                    If lstFolders.FindString(D$) < 0 Then
                        lstFolders.Items.Add(D$)
                        found = True
                    End If
                End If
            Next Fi

            For Each SubFold In Fold.GetDirectories
                D$ = SubFold.FullName
                found = LocateFolders(D$)
                If found Then Exit For
            Next SubFold

            LocateFolders = found

            SubFold = Nothing
            Fi = Nothing
        End Function

    All that is part of a form that includes the lstFolders control and some other stuff. I'll send you the whole form if you want. It also has a related form to run any of the Office programs found in the list produced by the first form. But, I think the code you have must be better than my stuff. But I wonder if your code will always continue to work properly in Windows in the future. (What if Microsoft changes the place it keeps the info in the Registry?)

    Also, "MsgBoxer" is a DevExpress user form I designed to mimic MsgBox a little, that allows me to get the form to appear in the colors set by DevExpress's "LookAndFeel" stuff.


    Robert Homes


    • Edited by Robert Homes Monday, September 25, 2017 12:05 PM typo
    Monday, September 25, 2017 12:04 PM
  • Cherry,

    Not, that may not be "it". This raises an error in my vb program:

        Public WithEvents wd As Word.Application.16
    
    The error says the end of the statement was "expected" after "Application" and doesn't like the ".16" I added. Now what?


    Robert Homes

    Monday, September 25, 2017 2:42 PM
  • That's it!

    So, if I want to force "wd" to point Word version 16, all I have to do is add ".16" to the end of "Word.Application". That is exactly what I wanted to know. Since I also have Word version 14, I can make "wd" point to it with "Word.Application.14".

    Now, what if I want "wd" to point to Word 360? Would that be "Word.Application.360"?

    BTW, where did you find this information?


    Robert Homes


    Hi Robert,

    What is Word 360? I find this information from this article, hope it is helpful to you.

    http://www.codeguru.com/columns/vb/how-to-determine-which-office-version-is-installed-with-vb.net-and-c.htm

    Best Regards,

    Cherry

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    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.

    Tuesday, September 26, 2017 1:35 AM
    Moderator