Results 1 to 12 of 12

Thread: Open existing Word Doc in active Word App?

  1. #1

    Thread Starter
    Addicted Member BarankinBeAMan's Avatar
    Join Date
    Aug 2002
    Location
    Reston,Va
    Posts
    164

    Resolved Open existing Word Doc in active Word App?

    Could anybody help with this?

    Scenario:

    There is already Word doc open, I just need to add another existing one, to the same Word Application, just onw Word Active window for all documents.

    I used SetParent API, did not help much, it opens new word appl for existing doc.


    Thank you all.
    Last edited by BarankinBeAMan; Sep 22nd, 2004 at 11:36 AM.

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    Use GetObject to attach to the current running Word application.
    Then you can open as usual off of the attached word aplication so
    the document will be another window in word.

    HTH
    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
    Addicted Member BarankinBeAMan's Avatar
    Join Date
    Aug 2002
    Location
    Reston,Va
    Posts
    164
    Something like that?:

    Public Sub New
    Set Me.word_application = Nothing
    Set Me.word_document = Nothing
    On Error Resume Next
    Set Me.word_application = GetObject("", "Word.Application")
    If (Err = 208) Then
    ' Word not active. Create new object to launch Word in background.
    Err = 0
    Set Me.word_application = CreateObject("Word.Application")
    End If
    Call Me.word_application.Documents.Add
    Set Me.word_document = Me.word_application.ActiveDocument
    End Sub

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    Yes, but why do you need all the Me's?
    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

  5. #5

    Thread Starter
    Addicted Member BarankinBeAMan's Avatar
    Join Date
    Aug 2002
    Location
    Reston,Va
    Posts
    164
    no me's, that was for Net.

    But this code still does not help, even if no Word App is active, this getobject still returns 0 for the error. Even if I kill all WinWord apps.
    Any ather suggestions?

    Thanks

  6. #6
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    This works in VB6. Can't test for .NET because my test system is
    taken apart right now.

    VB Code:
    1. Option Explicit
    2. 'Add reference to M$ Office Word Object Library
    3. Private moApp As Word.Application
    4. Private moDoc As Word.Document
    5.  
    6. Private Sub Command1_Click()
    7.     Set moDoc = moApp.Documents.Open("C:\Test.doc")
    8. End Sub
    9.  
    10. Private Sub Form_Load()
    11.    
    12.     On Error GoTo No_Set
    13.    
    14.     Set moApp = GetObject(, "Word.Application")
    15.     If TypeName(moApp) = "Nothing" Then
    16.         Set moApp = CreateObject("Word.Application")
    17.     End If
    18.     moApp.Visible = True
    19.     Exit Sub
    20. No_Set:
    21.     If Err.Number = 429 Then
    22.         Resume Next
    23.     Else
    24.         MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    25.     End If
    26. 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

  7. #7
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    It will open the Test.doc in the same Word window. Although
    it will still show each document in the taskbar as separate windows.


    Attached Images Attached Images  
    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

  8. #8

    Thread Starter
    Addicted Member BarankinBeAMan's Avatar
    Join Date
    Aug 2002
    Location
    Reston,Va
    Posts
    164
    Yes, the code runs fine, it opens Test.doc in the same app.
    But if you add second doc. Test1.doc, it opens new window for me. Is there anyway to open Test1.doc in the same window?

    Thanks.

    Private moApp As Object 'Word.Application
    Private moDoc As Object 'Word.Document
    Private moDoc2 As Object

    Private Sub Command1_Click()
    Set moDoc = moApp.Documents.Open("C:\Test.doc")
    End Sub
    Private Sub Command3_Click()
    Set moDoc2 = moApp.Documents.Open("C:\Test1.doc")
    End Sub

    Private Sub Command2_Click()

    On Error GoTo No_Set

    Set moApp = GetObject(, "Word.Application")
    If TypeName(moApp) = "Nothing" Then
    MsgBox "No Word is running, creating new."
    Set moApp = CreateObject("Word.Application")
    Else
    MsgBox "Word is running, add document."
    End If
    moApp.Visible = True
    Exit Sub
    No_Set:
    If Err.Number = 429 Then
    Resume Next
    Else
    MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    End If

    End Sub

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    I added two more docs and they are all in the same instance of
    Word. Do you mean that you dont want them to show separate
    in the taskbar?

    VB Code:
    1. 'This is what I did for three docs.
    2. Option Explicit
    3. 'Add reference to M$ Office Word Object Library
    4. Private moApp As Word.Application
    5. Private moDoc As Word.Document
    6.  
    7. Private Sub Command1_Click()
    8.     Set moDoc = moApp.Documents.Open("D:\Add1.doc")
    9.     Set moDoc = moApp.Documents.Open("D:\Add2.doc")
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13.    
    14.     On Error GoTo No_Set
    15.    
    16.     Set moApp = GetObject(, "Word.Application")
    17.     If TypeName(moApp) = "Nothing" Then
    18.         Set moApp = CreateObject("Word.Application")
    19.     End If
    20.     moApp.Visible = True
    21.     Set moDoc = moApp.Documents.Open("D:\Test.doc")
    22.     Exit Sub
    23. No_Set:
    24.     If Err.Number = 429 Then
    25.         Resume Next
    26.     Else
    27.         MsgBox Err.Number & " - " & Err.Description, vbOKOnly + vbExclamation
    28.     End If
    29. End Sub
    If you dont want them to show separately in the
    taskbar, you can change the settings in Word programmatically
    like this.

    VB Code:
    1. moApp.ShowWindowsInTaskbar = False
    Or manually like this.

    Attached Images Attached Images  
    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

  10. #10

    Thread Starter
    Addicted Member BarankinBeAMan's Avatar
    Join Date
    Aug 2002
    Location
    Reston,Va
    Posts
    164
    Thank you.

    Ńëŕâŕ Ňđóäó !

  11. #11
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    No prob.

    Was it just eliminating the showing in the taskbar or was it the
    code that solved it for you?
    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

  12. #12

    Thread Starter
    Addicted Member BarankinBeAMan's Avatar
    Join Date
    Aug 2002
    Location
    Reston,Va
    Posts
    164
    GetObject is pretty much what I needed.
    Also I realized I wanted something else that I did not really need

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