Results 1 to 5 of 5

Thread: how to close word object

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Location
    Macedonia
    Posts
    49

    how to close word object

    place this code on command button run and on the first time in word will show table with data in it close word and run it again on command button and will be shown only data without the table how to fix this

    Private Sub Command12_Click()
    Dim objWord As Word.Application
    Dim doc As Word.Document
    objWord.ActiveDocument.Close
    objWord.Quit
    Set objWord = Nothing
    Set objWord = New Word.Application
    Set objWord = GetObject(, "Word.application")
    objWord.Visible = True
    Set doc = objWord.Documents.add()
    doc.Activate
    Set doc = Nothing
    With objWord.Selection
    .Tables.add Range:=Selection.Range, NumRows:=5, NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
    .TypeText "{ifra" & vbTab & vbTab & "m2" & vbTab & vbTab & "Cena" & vbTab & vbTab & "Kat" & vbTab & vbTab & "Naselba" & vbTab & vbTab & "Grad"
    End Sub

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

    Re: how to close word object

    Because your destroying the document object before adding the table to it. - Set doc = Nothing

    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
    Member
    Join Date
    Aug 2005
    Location
    Macedonia
    Posts
    49

    Re: how to close word object

    i do that but it's still the same

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

    Re: how to close word object

    Your logic is not that good. You dont need to keep a app object var local to the procedure and set it and then destroy it every button click. If you need your app object available across several procedures then it would be better to have them module level vars so you dont need to keep doing a set/destory/set in every procedure.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: how to close word object

    VB Code:
    1. Private objWord As Word.Application
    2.  
    3. Private Sub Command12_Click()
    4.     Dim doc As Word.Document
    5.     objWord.ActiveDocument.Close
    6.     objWord.Quit 'Why quit if your using it again?
    7.     Set objWord = Nothing
    8.     Set objWord = New Word.Application 'Why create again?
    9.     Set objWord = GetObject(, "Word.application") 'Why recreate after being set in previous line?
    10.     objWord.Visible = True
    11.     Set doc = objWord.Documents.add()
    12.     doc.Activate
    13.     Set doc = Nothing 'Destroying righ after being set?
    14.     With objWord.Selection
    15.         .Tables.add Range:=Selection.Range, NumRows:=5, NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
    16.         .TypeText "{ifra" & vbTab & vbTab & "m2" & vbTab & vbTab & "Cena" & vbTab & vbTab & "Kat" & vbTab & vbTab & "Naselba" & vbTab & vbTab & "Grad"
    17.     End With 'Not in you code, missing
    18. 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