Results 1 to 5 of 5
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    176

    Opening Word document by clicking form button

    I open a Microsoft Word document by clicking a button on a form in my application.
    The event handler uses the following line :-
    dblRetval = Shell("Explorer """ & strDocumentPath & """", vbMaximizedFocus)
    This works ok, but has a splash screen when Word opens. I have experimented with other options, namely :-
    dblRetval = Shell("Winword """ & strDocumentPath & """", vbMaximizedFocus)
    This works exactly like the previous one.
    dblRetval = Shell("Winword /q """ & strDocumentPath & """", vbMaximizedFocus)
    This works without the splash screen, which is what I want. However, if I click on the button again, it tries to open up a read only version of the same document. The previous two options just returned to the same document.

    Can anyone explain why putting the /q (for quiet presumably) is opening up multiple versions of the same document.


    Also, is it better to use Explorer or Winword.

    Thanks for any advice or help.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    this module will open ANY file in its native application.


    Paste this code into a module, then its usage is: OpenNativeApp txtBox


    if the item in the text box is a URL, it will open in default explorer
    if a .doc, it will open in WORD




    Code:
    
    #If Win64 Then      'Public Dclare PtrSafe Function
      Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
      Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
    #else
      Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
      Private Declare Function GetDesktopWindow Lib "user32" () As Long
    #End If
    
    
    
    
    Const SW_SHOWNORMAL = 1
    Const SE_ERR_FNF = 2&
    Const SE_ERR_PNF = 3&
    Const SE_ERR_ACCESSDENIED = 5&
    Const SE_ERR_OOM = 8&
    Const SE_ERR_DLLNOTFOUND = 32&
    Const SE_ERR_SHARE = 26&
    Const SE_ERR_ASSOCINCOMPLETE = 27&
    Const SE_ERR_DDETIMEOUT = 28&
    Const SE_ERR_DDEFAIL = 29&
    Const SE_ERR_DDEBUSY = 30&
    Const SE_ERR_NOASSOC = 31&
    Const ERROR_BAD_FORMAT = 11&
    
    
    
    
    Public Sub OpenNativeApp(ByVal psDocName As String)
    Dim r As Long, msg As String
    
    
    r = StartDoc(psDocName)
    If r <= 32 Then
        'There was an error
        Select Case r
            Case SE_ERR_FNF
                msg = "File not found"
            Case SE_ERR_PNF
                msg = "Path not found"
            Case SE_ERR_ACCESSDENIED
                msg = "Access denied"
            Case SE_ERR_OOM
                msg = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                msg = "DLL not found"
            Case SE_ERR_SHARE
                msg = "A sharing violation occurred"
            Case SE_ERR_ASSOCINCOMPLETE
                msg = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                msg = "DDE Time out"
            Case SE_ERR_DDEFAIL
                msg = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                msg = "DDE busy"
            Case SE_ERR_NOASSOC
                msg = "No association for file extension"
            Case ERROR_BAD_FORMAT
                msg = "Invalid EXE file or error in EXE image"
            Case Else
                msg = "Unknown error"
        End Select
    '    MsgBox msg
    End If
    End Sub
    
    
    
    
    Private Function StartDoc(psDocName As String) As Long
    Dim Scr_hDC As Long
    
    
    Scr_hDC = GetDesktopWindow()
    StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
    End Function

  3. #3
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    176
    Thanks Ranman, but this is a lot more complicated than my code, which does work.
    It does not address the question as to why multiple versions of the same document can be opened if the /q option is added to the Winword

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Just tried Application.FollowHyperlink strDocumentPath and it opens the file with no splash screen and puts the focus on the already opened document for the subsequent clicks. No idea why your code tries to open a read-only, it looks like the /q switch forces a new instance of Word to open when the others are reusing the open one.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    176
    Vlad,

    Marvelous, that's exactly what I want. Had not come across the FollowHyperlink option before directly from the vba.
    You're probably right about the /q switch forcing a new instance of Word.

    Cheers

Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA Code for opening Word Document problem
    By andrejamih in forum Programming
    Replies: 1
    Last Post: 12-27-2017, 03:59 AM
  2. Replies: 1
    Last Post: 07-26-2017, 01:25 PM
  3. Replies: 2
    Last Post: 09-15-2015, 03:08 AM
  4. Replies: 3
    Last Post: 08-03-2015, 08:24 AM
  5. Replies: 3
    Last Post: 05-22-2014, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums