Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 28th, 2011, 10:17 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Exclamation excel documents not closing

I am working in Word 2007 and during the application I use a global function to open an excel document (so that I can transfer data over to my word document).

However, the code I am using doesn't come up with any errors but it doesn't appear to work as it still says the excel workbook is locked by me (so I think it must still be running on my computer in the background).

I have the following code:

Declare Source:
Code:
Function RetrieveExcelData()
    'Parts document (ScopeDeliverablesExclusionsExcelData)
    Set appParts = Excel.Application
    Set wkParts = appParts.Workbooks.Open("H:\proposalDocumentDevelopment\proposalTemplates\excelData.xlsm")

    'Other document (ScopeDeliverablesExclusionsExcelData)
    Set appOther = Excel.Application
    Set wkOther = appOther.Workbooks.Open("H:\proposalDocumentDevelopment\proposalTemplates\testExclusionsDeviationsClarifications.xlsx")
I will not put the transferring data code unless you need it. but it is basically just call function and insert text.

Exit button within all forms :
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    'have a dialogue box asking whether the user is sure they want to close the application
    Dim msgPrompt As String
    Dim msgButtons As Integer
    Dim msgTitle As String
    Dim msgResult As Integer
    
    msgPrompt = "Are you sure you are finished?"
    msgButtons = vbYesNo + vbQuestion + vbDefaultButton2
    msgTitle = "Exit"
    msgResult = MsgBox(msgPrompt, msgButtons, msgTitle)
    
    If CloseMode = vbFormControlMenu Then
        If msgResult = 7 Then
            Cancel = True
        End If
        If msgResult = 6 Then
            Call CloseAll
        End If
    End If
    
End Sub


Close the excel documents (global variable):
Code:
Function CloseAll()
    wkOther.Close xlDoNotSaveChanges
    Unload UserForm1
    wkParts.Close xlDoNotSaveChanges
End Function

Could somebody please help me?
Thanks

Last edited by cjcant; June 28th, 2011 at 11:56 AM..
 
Old June 28th, 2011, 01:17 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

There might be a memory leak as the program still holds the object instance. You can dispose it using

Set wkParts = Nothing

etc

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
The Following 2 Users Say Thank You to Shasur For This Useful Post:
cjcant (June 29th, 2011), StarDrago (November 17th, 2011)
 
Old June 29th, 2011, 03:58 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Thanks.

It is amazing how the answer can be something so simple.
 
Old July 19th, 2011, 04:13 AM
Authorized User
 
Join Date: May 2011
Posts: 44
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi,

I thought it had been working but it isn't. I think I maybe tested it with the wrong document.

Do you have any other ideas? The program just does not seem to let it go. Is there maybe some code for closing a document rather than cutting the connection?

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make one specific Excel file from several XML documents using Xpath??? andris.susciks XML 0 June 11th, 2011 09:06 AM
Inserting Excel pivot chart into Word documents jfejsa Word VBA 1 January 23rd, 2006 09:16 PM
Closing Excel Workbook rekha_jsr Excel VBA 0 July 9th, 2005 02:40 AM
VBAProject Password closing Excel jdmurcia Excel VBA 1 December 4th, 2004 09:41 AM
Closing Excel hek Excel VBA 2 October 29th, 2004 10:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.