Results 1 to 16 of 16

Thread: Closing word properly

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Closing word properly

    Im running word within a label on my form and i want to be able to close word properly upon form closing so that when the program is re run you don't get document recovery coming up. Im currently using the following but this always causes document recovery to appear the next time it is run. Thanks


    Code:
    For Each currentProcess As Process In killword
                        If currentProcess.ProcessName = "WINWORD" Then currentProcess.Kill()
    
                    Next
    Using vb.net 2003

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Closing word properly

    VB Code:
    1. For each P as Process.GetProcessesByName("WINWORD")
    2.      P.CloseMainWindow
    3. Next
    Note that this prompts the user to save his work (of he hasnt already done it). The user could press cancel, resulting in Word not closing down.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Closing word properly

    Ask the process to exit nicely by calling its CloseMainWindow method first. Only if it hasn't exited after a timeout period, which may indicate that it can't, should you call its Kill method. Search the forum for closemainwindow with my user name and you'll find more than one code example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Hello i found this code that you posted but this dosen't close word but will eventually kill it? Anyideas?

    Code:
    Dim procs As Process() = Process.GetProcessesByName("WINWORD")
            For Each proc As Process In procs
                ' Ask the process to closely gracefully.            
                proc.CloseMainWindow()
                'Wait up to five seconds for the process to exit.            
                proc.WaitForExit(5000)
                If Not proc.HasExited Then
                    'Force the process to exit.                
                    proc.Kill()
                End If
                'Free resources.            
                proc.Close()
            Next proc
        End Sub
    vb.net 2003 code please!

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Actually it does close another instance of Word no worries just not the one within my form. Is this something to do with the fact the label on my form is the handler of word?

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

    Re: Closing word properly

    Terminating a process is never a good solution. Since this is Word you could automate it to attachan Application object variable to your instance and then just issue the .Close SaveChanges:=True and a .Quit() so it gracefully closes th open document and THEN quits the application with no issues or corruption of the document.
    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

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Could you give me a code example?

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

    Re: Closing word properly

    I'm still working on another code example for another member but using GetObject will allow you to set the object variable to that instance and then its just word automation from there like I mentioned. If you need more help I'll be done witht hte other code in a few minutes.
    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

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Yeah i do if that ok! No hurry.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Robdogg888 could really do with your help if you don't mind!!

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

    Re: Closing word properly

    Try using something like this...
    vb Code:
    1. Imports Microsoft.Office.Interop
    2.  
    3. Public Class Form1
    4.  
    5. Friend Sub CloseWord()
    6.     Dim oApp As Word.Application
    7.     Try
    8.         oApp = DirectCast(Microsoft.VisualBasic.GetObject(, "Word.Application"), Word.Application)
    9.     Catch ex As Exception
    10.         If TypeName(moApp) = "Nothing" Then
    11.             MessageBox.Show("Word not found", "VB/Office Guru™ Word Demo", _
    12.             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    13.         Else
    14.             MessageBox.Show(ex.Message, "VB/Office Guru™ Word Demo", _
    15.             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    16.             Exit Sub
    17.         End If
    18.     End Try
    19.     oApp.Documents.Close
    20.     oApp.Quit()
    21. End Sub
    22. End Class
    Last edited by RobDog888; Apr 20th, 2007 at 04:16 AM.
    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
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Should moapp be oapp?

    Word.application is not defined is that a 2005 command as im using 2003.

    Thanks for your help!

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

    Re: Closing word properly

    I updated the posts code. You need to make sure you inported the .Interop class. Word used in that conext is a qualifier for the constant.

    Modified from 2003 to work in 2005 but I can post another for 2003
    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

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

    Re: Closing word properly

    One other thing, if you are loading Word into a Label then instead of just starting that process you could use Word Automation from the start and you wont need to try to close the process with all this code or end up closing some other word process that was created by the user.

    See this part of my Office FAQ: http://vbforums.com/showthread.php?t=406637
    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

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Closing word properly

    Being a pain now ... It dosen't like Imports Microsoft.Office.Interop it dosen't have office as an option after microsoft.

    Ill take a look at your link

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

    Re: Closing word properly

    You need to add a reference to Word but then you will be tied in to only one version of support. See the link in my sig for "Primary Interop Assemblies".

    To support multiple versions of Word, you need to use late binding (also in that link).
    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