Results 1 to 4 of 4

Thread: opening files(word and excel) from vb

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    18

    opening files(word and excel) from vb

    hi!
    how can i open a file wordfile or excelfile from vb.i have a combo containing file names as a.doc,b.xls,c.doc etc. if i select a.doc from combo then this file should be opened.same is for others

    do reply
    ashish sharma

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: opening files(word and excel) from vb

    Depends. Are you going to want to automate them or just open them?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    18

    Re: opening files(word and excel) from vb

    Quote Originally Posted by RobDog888
    Depends. Are you going to want to automate them or just open them?
    open them but can also work/modify contents with the opened file

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: opening files(word and excel) from vb

    Here is something to get you started.
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Excel xx.0 Object Library
    3. 'Add a reference to MS Word xx.0 Object Library
    4. Private moAppXL As Excel.Application
    5. Private moAppWD As Word.Application
    6.  
    7. Private Sub Command1_Click()
    8.     Select Case Right(Combo1.Text, InStr(1, Combo1.Text, ".") - 1) 'doc, xls, etc
    9.         Case "doc"
    10.             Dim oDoc As Word.Document
    11.             Set oDoc = moAppWD.Documents.Open(Combo1.Text)
    12.             moAppWD.Visible = True
    13.            
    14.         Case "xls"
    15.             Dim oWB As Excel.Workbook
    16.             Set oWB = moAppXL.Workbooks.Open(Combo1.Text)
    17.             moAppXL.Visible = True
    18.            
    19.         Case Else
    20.             MsgBox "File not found or extension not listed!", vbOKOnly + vbExclamation
    21.     End Select
    22. End Sub
    23.  
    24. Private Sub Form_Load()
    25.     Set moAppXL = New Excel.Application
    26.     Set moAppWD = New Word.Application
    27. End Sub
    28.  
    29. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    30.     If TypeName(moAppXL) <> "Nothing" Then
    31.         moAppXL.Quit
    32.     End If
    33.     If TypeName(moAppWD) <> "Nothing" Then
    34.         moAppWD.Quit
    35.     End If
    36.     Set moAppXL = Nothing
    37.     Set moAppWD = Nothing
    38. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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