Announcement

Collapse
No announcement yet.

Run Word and open the Open Dialog using a vbs file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Run Word and open the Open Dialog using a vbs file

    Hi folks

    I rarely need to do anything like this, so have never learnt. I'm wondering if anyone would give me an example of how this is done please or actually do it for me?

    I am building an Intranet using Sharepoint Services. I've got document libraries and forums set up so far. One of the things I want to do is create a link (to a .vbs or .bat) that staff can use to open MS-Word 2003, and cause the Open Dialog to open at a pre-determined location.

    I thought I had found the answer here, but although I can change the title of the open dialog box (a very useful feature), and make it open the folder I want the users to see, I cannot get Word to actually open a file that has been selected. Searching elsewhere provides examples but nothing so far that is useful or useable.

    I would like the link to open MS-Word, open the Open dialog box at \\server\share and then finish, leaving Word open at this point and allowing the user to select a file and work on it.

    If anyone can write this for me I will be extremely grateful. Failing that, if anyone has any useful links that show how to start an application this way I would be similarly grateful.

    Thanks
    A recent poll suggests that 6 out of 7 dwarfs are not happy

  • #2
    Re: Run Word and open the Open Dialog using a vbs file

    It requires the use of SendKeys to open a Dialog Box for Selecting Files after Word is opened:
    Code:
    strProc = "Winword.exe"
    OpenPath = "c:\test"
    
    Set WshShell = CreateObject("WScript.Shell")
    
    strCMD = GetAppPath(strProc)
    With GetObject("winmgmts:Win32_Process")
      Set oInParams = .Methods_("Create").InParameters.SpawnInstance_
         oInParams.CommandLine = strCMD
      Set oOutParams = .ExecMethod_("Create", oInParams)
         strPID = oOutParams.ProcessId
         returned = oOutParams.ReturnValue
    End With
    
    While WshShell.AppActivate(strPID) = FALSE
    wscript.sleep 50
    Wend
    
    If returned = 0 Then
       OpenPath = Replace(OpenPath,"(","{(}")
       OpenPath = Replace(OpenPath,")","{)}")
    
       WshShell.AppActivate strPID
       If WshShell.AppActivate(strPID) then WshShell.SendKeys "^o"
       wscript.sleep 10
       WshShell.AppActivate strPID
       If WshShell.AppActivate(strPID) then WshShell.SendKeys "%n"
       wscript.sleep 10
       WshShell.AppActivate strPID
       If WshShell.AppActivate(strPID) then WshShell.SendKeys OpenPath
       wscript.sleep 10
       WshShell.AppActivate strPID
       If WshShell.AppActivate(strPID) then WshShell.SendKeys "%o"
    End If
    
    
    Function GetAppPath(byval strApp)
       With WshShell '(has a global scope)
         RegKey = "HKLM\SOFTWARE\Microsoft\Windows" _
              & "\CurrentVersion\App Paths\"
         GetAppPath = .RegRead (regkey  & strApp & "\" & "")
       End With 
    End Function
    However, the use of SendKeys is not always reliable.
    Therefore I think you can beter open a Dialog Box for Selecting a File first and then after a file is selected MS Word automatically starts and opens the file selected:
    Code:
    Set objDialog = CreateObject("UserAccounts.CommonDialog")
    objDialog.Filter = "Important Word Files|*.doc"
    objDialog.InitialDir = "c:\test"
    intResult = objDialog.ShowOpen
    
    If intResult = 0 Then
        wscript.echo "null"
        Wscript.Quit
    Else
       strProc = "Winword.exe"
       Set WshShell = CreateObject("WScript.Shell")
       strCMD = GetAppPath(strProc)
       With GetObject("winmgmts:Win32_Process")
          Set oInParams = .Methods_("Create").InParameters.SpawnInstance_
          oInParams.CommandLine = strCMD & " """ & objDialog.FileName & """"
          Set oOutParams = .ExecMethod_("Create", oInParams)
          strPID = oOutParams.ProcessId
          returned = oOutParams.ReturnValue
       End With
    End If
    
    Function GetAppPath(byval strApp)
       With WshShell '(has a global scope)
         RegKey = "HKLM\SOFTWARE\Microsoft\Windows" _
              & "\CurrentVersion\App Paths\"
         GetAppPath = .RegRead (regkey  & strApp & "\" & "")
       End With 
    End Function
    \Rems
    Last edited by Rems; 23rd March 2009, 23:37.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Run Word and open the Open Dialog using a vbs file

      OMG! That is astonishingly well done.

      Did not try the first one - but I bet others will find it useful.

      Thank you very much! I really appreciate your help. Have a beer



      Edit:

      Ah, one thing - text files open in notepad. I'll just tell folks to right-click and choose 'Open with'

      Cheers!
      Last edited by Blood; 24th March 2009, 10:06.
      A recent poll suggests that 6 out of 7 dwarfs are not happy

      Comment


      • #4
        Re: Run Word and open the Open Dialog using a vbs file

        Glad to be of help.

        Originally posted by Blood View Post
        Ah, one thing - text files open in notepad. I'll just tell folks to right-click and choose 'Open with'
        I don't know how and for what type of files the dialogbox is used, but there can be configured a lot more in the box if needed.
        Also it is possible to make the script determine what program should be used for certain file types.
        So if you think the script can be improved please give me the details.

        cheers,
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Run Word and open the Open Dialog using a vbs file

          No. It's just a case of understanding what was required.

          I steamed into this without really considering what would be the easiest thing to do. Your script works a treat - it opens the folder I specify, and double-clicking an item opens that item in the associated application.

          Because I wanted a Word document to open, I thought "how do you start Word and tell it to open the Open Dialog at \\server\share\sub-folder". After seeing the behaviour of the folder that the script opens I realised that all I had to do was create a normal "\\server\share\sub-folder" link which would do the same thing, but the other way round - choose the document, and the associated app would open as opposed opening the app and choosing the document.

          With regards to the text file - I'll just rename it to .doc

          I really do appreciate your generous help with this, you certainly earned the points. It's great to know that there are folks willing to share their knowledge.

          Cheers!
          A recent poll suggests that 6 out of 7 dwarfs are not happy

          Comment

          Working...
          X