CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2006
    Posts
    63

    open a word document...

    how do i open a word document using vb6? my command button should pop up the File Open dialog box and let me browse my computer for the document.

    thanx

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: open a word document...

    The easiest way is thru Windows API's, that do it the same way that the OS does.
    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_HIDE As Long = 0
    
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Dim sSave As String, Ret As Long
    
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim sSave As String, Ret As Long
        'Create a buffer
        sSave = Space(255)
        'Get the system directory
        Ret = GetSystemDirectory(sSave, 255)
        'Remove all unnecessary chr$(0)'s
        sSave = Left$(sSave, Ret)
    End Sub
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c dir c: > D:\Myfile.txt", "D:\", SW_SHOWNORMAL
    End Sub
    All you have to do is use this:

    Code:
        ShellExecute Me.hwnd, "Open", "D:\Myfile.docxt", "D:\", SW_SHOWNORMAL
    You can OPEN or PRINT, any recognized document format, and can specify the starting folder for the command.

    It should open the doc in WORD (if it's installed) in normal screen size
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2006
    Posts
    63

    Re: open a word document...

    Quote Originally Posted by dglienna View Post
    The easiest way is thru Windows API's, that do it the same way that the OS does.
    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_HIDE As Long = 0
    
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Dim sSave As String, Ret As Long
    
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim sSave As String, Ret As Long
        'Create a buffer
        sSave = Space(255)
        'Get the system directory
        Ret = GetSystemDirectory(sSave, 255)
        'Remove all unnecessary chr$(0)'s
        sSave = Left$(sSave, Ret)
    End Sub
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "Open", sSave & "CMD.exe", " /c dir c: > D:\Myfile.txt", "D:\", SW_SHOWNORMAL
    End Sub
    All you have to do is use this:

    Code:
        ShellExecute Me.hwnd, "Open", "D:\Myfile.docxt", "D:\", SW_SHOWNORMAL
    You can OPEN or PRINT, any recognized document format, and can specify the starting folder for the command.

    It should open the doc in WORD (if it's installed) in normal screen size


    thanx for your reply. but, i need to open a doc - not from a specific folder. it could be anywhere on the computer or on the network. the program should pop up a dialog box where i can browse for the file and then open it. how is this possible?

  4. #4
    Join Date
    Dec 2008
    Posts
    19

    Re: open a word document...

    Use the Common Dialog to get your file name. Once you have the path/filename use the ShellExecute API to open the file in Word.

  5. #5
    Join Date
    Jan 2006
    Posts
    63

    Re: open a word document...

    thanx for your replies. i was able to get this one to work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured