Results 1 to 6 of 6
  1. #1
    Jhail83 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    8

    Check to see if Excel Workbook is open through VBA Code. And filter saveas types?

    I am using a button on an access form to export a report using a custom Excel Template.

    However, if the excel file is open when a user tries to save, it will throw an error, then nothing happens, but the excel template file stays open in the background, causing the user to have to manually close it through the task manager.

    Also, currently you can save the workbook as anything.



    For instance I can overwrite a word file, which is tacky. How can I get it to only save as .xls or .xlsx file types?

    Code:
    Option Compare Database
    Public Sub btn_Export_Click()
    Dim objXL As Object
    Dim xlWB As Object
    Dim xlWS As Object
    Dim rst As DAO.Recordset
    Dim fd As FileDialog
    Dim vrtSelectedItem As Variant
    
    Set objXL = CreateObject("Excel.Application")
    
    'Opens the Template to Put records into.
    Set xlWB = objXL.Workbooks.Open("C:\Users\xxxxx\Desktop\Test Database\2013 Excel Workbook Test")
    
    'chooses which worksheet to put the data into.
    Set xlWS = xlWB.Worksheets("xDetails")
    
    'Selects which Query to retrieve the data from to export.
    Set rst = CurrentDb.OpenRecordset("qry_ReportOutput")
    
    'Create a FileDialog Object as a SaveAs Dialog Box
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    xlWS.Range("B3").CopyFromRecordset rst
    
    With fd
       If .Show = -1 Then
          For Each vrtSelectedItem In .SelectedItems
                xlWB.SaveAs vrtSelectedItem
          Next vrtSelectedItem
       Else
       End If
    End With
    
    Set fd = Nothing
    rst.Close
    Set rst = Nothing
    xlWB.Close
    
    End Sub
    Ideally, I want it to look like this.


    Code:
    With fd
         If .Show = -1 Then
               For Each vrtSelectedItem In .SelectedItems
                    If blnWorkbookopen = 0 then
                           xlWB.SaveAs vrtSelectedItem (only as a workbook file type)
                    else
                           msgbox "Please close the Workbook you are trying to save to."
                           Set fd = Nothing
                           rst.Close
                           Set rst = Nothing
                           xlWB.Close
                           Exit Sub
               Next vrtSelectedItem
                  Else
                  End If
    End With
    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I had this issue with a VBScript determining if an Access file is open. The only solution I could find was for the script to just kill any and all Access processes before running code to open database.

    The FileDialog can be restricted to specific file types: http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Jhail83 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    8
    How would I kill the process?

    The Filters don't seem to work with (FileDialogSaveas) in Access..

    Also for some reason, it freezes when you hit cancel or "x" out of the saveas filedialog box. I thought the if .show = -1 then... statement would handle that.

    I tried:

    Code:
    If .Show = True Then
          For Each vrtSelectedItem In .SelectedItems
             xlWB.SaveAs vrtSelectedItem
          Next vrtSelectedItem
       Else
    End If
    and

    Code:
    If .Show = True Then
          For Each vrtSelectedItem In .SelectedItems
             xlWB.SaveAs vrtSelectedItem
          Next vrtSelectedItem
       Else
       xlWB.Close
       Set fd = Nothing
       rst.Close
       Set rst = Nothing
       Exit Sub
          End If

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Here is my VBScript:

    ' ProcessKillLocal.vbs
    ' Sample VBScript to kill a program
    ' Author Guy Thomas http://computerperformance.co.uk/
    ' Version 2.7 - December 2010
    ' ------------------------ -------------------------------'
    Option Explicit
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strProcessKill
    strComputer = "."
    strProcessKill = "'msaccess.exe'"
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )
    For Each objProcess in colProcess
    objProcess.Terminate()
    Next
    'WSCript.Echo "Just killed process " & strProcessKill & " on " & strComputer
    'WScript.Quit
    'End of WMI Example of a Kill Process

    Dim objFSO, oShell
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'open the Access file
    Set oShell = CreateObject("WScript.Shell")
    oShell.Run """C:\Program Files\Microsoft Office\Office12\msaccess.exe"" ""\\servername\path\databasename.accdb"""

    The X close and Cancel both work for me.

    Sorry that filters don't.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Jhail83 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Posts
    8
    Anyway I can use that to kill a specific file name of an application?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    No, that was the issue I had. Couldn't find any to determine what files are open. Just have to kill all Access processes. This is fine in my situation because I am the only user running that VBScript and I routinely close all my Access files each day anyway so the kill really isn't required. It's a 'just in case' measure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 02-07-2013, 03:15 PM
  2. Create Excel workbook from code
    By GraeagleBill in forum Programming
    Replies: 8
    Last Post: 12-08-2012, 01:58 PM
  3. Changing combo-box to check box (a la Excel filter)
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 07-31-2012, 08:23 PM
  4. Excel code not working with Excel open
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 07-11-2011, 12:12 PM
  5. Replies: 2
    Last Post: 02-26-2010, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums