+ Reply to Thread
Results 1 to 6 of 6

MAC VBA Fails to save and close named workbook

  1. #1
    Registered User
    Join Date
    08-12-2013
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    4

    MAC VBA Fails to save and close named workbook

    I have a Excel VBA to anlayse SFDC data that has been running on Windows for years and I need to port it across to Excell 2011 for MAC, due to the number of users choosing MAC laptops.

    Most of the code /userforms , opening files and calculations all work fine. The only issue I have is trying to save a named Workbook, any method i try results in an error and exception. I end up with a lot of working file therefore left open.

    Attempt to close open files either by direct filename reference or <file_pointer> always fails....even though the file is valid and open in MAC Excel.
    • Workbooks("Extract.xls").Close
    • Workbooks("Extract.xls").Close savechanges := False
    • Workbooks(ref_fn_index("Extract.xls")).Close



    ======Example code tried ========

    Workbooks("Extract.xls").Close

    Workbooks("Extract.xls").Close savechanges := False

    Workbooks(ref_fn_index("Extract.xls")).Close


    Public Function ref_fn_index(enq_file As String) As Integer
    '=== Find reference ptr of open workbook filename for MAC restriction====
    Dim i As Integer
    Dim file_seen As Boolean

    file_seen = False
    ref_fn_index = -1
    For i = 1 To Workbooks.count
    If LCase(Workbooks(i).name) = LCase(enq_file) Then
    file_seen = True
    ref_fn_index = i
    Exit Function
    End If
    Next i
    '==== Catch error case ===
    If Not file_seen Then MsgBox "Unable to find current workbook", vbCritical, "File I/O Eror"
    End Function
    Last edited by codebasher; 08-12-2013 at 10:03 AM.

  2. #2
    Registered User
    Join Date
    08-12-2013
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: MAC VBA Fails to save and close named workbook

    Additional Info : Directory_path

    In case MAC VBA wants full directory path, I also tried to pre-pend the current directory to the filename, which made no difference.

    Note : MAC directory uses ":" rather than "\" in Windows

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: MAC VBA Fails to save and close named workbook

    How are you opening the files?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    08-12-2013
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: MAC VBA Fails to save and close named workbook

    Open by this

    Workbooks("Extract.xls").Open

    I only wrote the ref_fn_ptr function to see if filename was seen by MAC VBA and it is - returning "3", when debuging I see the workbook.name matching "Extract.xls" in that function. I'm just unable to close a workbook by "Name".

    By testing I found Active.Workbook.close works...but I don't want to keep swaping Active.workspace -hence want to use the "Name" Close.

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: MAC VBA Fails to save and close named workbook

    presumably you mean
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    since neither of your examples are valid code. I suggest you simply store a reference to the workbook and use that to close it
    Please Login or Register  to view this content.
    although I can't see why your code would fail-do you get an error message?
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Registered User
    Join Date
    08-12-2013
    Location
    UK
    MS-Off Ver
    Excel 2013
    Posts
    4

    Re: MAC VBA Fails to save and close named workbook

    Sadly have to return to this topic, as more MAC users and pressure not to run a Windows Virtual machine on MAC.. hence refreshing theme of Workbooks.Close on MAC VBA for Excel.

    I tried a separate sub-routine for closing after reading other threads on same topic

    Public Sub MAC_close_book(ByVal book_name As String, ByVal storedata As Boolean)
    '==== Attempt to safely close workbook without MAC error ======

    If storedata Then Workbooks(book_name).Save
    Workbooks(book_name).Saved = True
    Application.DisplayAlerts = False
    Workbooks(book_name).Close False
    End Sub


    The only line that throws an error is the final close statement which would indicate that MAC VBA knows the file reference is valid and able to update it. The error message is always :

    Method of _workbook Object failed

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM
  2. Save and close a workbook using VBA
    By Kimberly123 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-23-2012, 09:33 AM
  3. Close workbook and save as.
    By Hjahren in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-13-2010, 04:41 AM
  4. button to save and close the workbook
    By Darrell456 in forum Excel General
    Replies: 3
    Last Post: 12-14-2009, 03:54 AM
  5. Help on Workbook close and workbook save events
    By Adam Harding in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2005, 12:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1