|
-
Sep 4th, 2005, 04:50 PM
#1
Thread Starter
Member
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
-
Sep 4th, 2005, 04:57 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 4th, 2005, 05:15 PM
#3
Thread Starter
Member
Re: how to close word object
i do that but it's still the same
-
Sep 4th, 2005, 05:22 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 4th, 2005, 05:24 PM
#5
Re: how to close word object
VB Code:
Private objWord As Word.Application
Private Sub Command12_Click()
Dim doc As Word.Document
objWord.ActiveDocument.Close
objWord.Quit 'Why quit if your using it again?
Set objWord = Nothing
Set objWord = New Word.Application 'Why create again?
Set objWord = GetObject(, "Word.application") 'Why recreate after being set in previous line?
objWord.Visible = True
Set doc = objWord.Documents.add()
doc.Activate
Set doc = Nothing 'Destroying righ after being set?
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 With 'Not in you code, missing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|