Macro to prevent or answer Excel recovering windows

MarieBocc

New Member
Joined
Jun 28, 2019
Messages
33
Hello everyone,

I have a macro that creates a new file almost similar to the one it runs on by Saving it As, copy/pasting as values every sheet and removing the old ones. It goes like this :

Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Export_EDF()[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Dim Feuille As Worksheet
    Dim FeuilleRef As Worksheet
    Dim Sheet As Object
    Dim FichierCible As Variant

[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    FichierCible = Application.GetSaveAsFilename
    If FichierCible <> False Then
        MsgBox FichierCible
        ThisWorkbook.SaveAs Filename:=FichierCible & "xlsm", FileFormat:=52, Password:="", ReadOnlyRecommended:=False, ConflictResolution:=xlOtherSessionChanges
        
        Set FeuilleRef = ActiveSheet
    
        ' copy/paste as values of all worksheets
        For Each Feuille In ActiveWorkbook.Worksheets
            With Feuille
                .Cells.Copy
                .Cells.PasteSpecial Paste:=xlPasteValues
            End With
        Next Feuille
        
     
        For Each Sheet In Worksheets
            If Sheet.Visible = False Then
                Sheet.Delete
            End If
        Next
        
        Call RemoveButtons

        FeuilleRef.Activate

    End If

    ActiveWorkbook.Save
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub RemoveButtons()
    If ActiveSheet.ProtectContents = True Then
    End If[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]    On Error Resume Next
        ActiveSheet.Buttons.Delete
End Sub[/FONT]

After this macro finishes running I am in a new Workbook pretty similar to the one I had to begin with. It is already saved and I can close it.

But when I try to reopen it afterward (only the first time), an Excel pop-up window shows up saying :
"We found a problem with some content in "documentname.xlsx" Do you want us to try to recover as much as we can?"
If I click 'Yes', which is what I do (, the default value is 'No'), I am presented with a dialogue box stating what was "fixed." The box states "Repaired Records: AutoFilter from /xl/worksheets/sheet1.xml part" (a lot of times) and I click on 'Close', which is the default and only option I have.

This will show up everytime I create a workbook using my macro and I can't really tell my clients to ignore the pop-up Windows and to just click on 'Yes' and then 'Close' for
it lacks professionnalism
so I was thinking of writing a macro to do it for them (preferably without them noticing).

My idea was to use the [FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Workbook_Open(). With a little researches I found out that I might use this to prevent Excel from asking me wheather to repare :
Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Private Sub Workbook_Open()
    If ActiveWorkbook.Name <> "theOriginalFile'sName.xlsm" Then
        Application.DisplayAlerts = False
        Application.AskToUpdateLinks = False
        ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources
    End If
End Sub[/FONT]
Sounds good, doesn't work. Unfortunately. The first pop-up window shows up before to Sub Workbook_Open() function reaches .UpdateLink.

Does anyone has any idea what to do to prevent this windows from poping up ?

Thank you very much for reading,

Marie<strike>
</strike>
[/FONT]
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi,

I try to reproduce your error but I have no message. Indeed I can close it without any message and when I open it, I have a 'enable editing' (like when I receive a file from someone) on top of the screen but no message about fixing data.

If your file has links, vba does not update them automatically, so maybe use
Code:
[LEFT][COLOR=#666666][FONT=Courier New]ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks[/FONT][/COLOR][/LEFT]
after .save and then re-save it?
 
Last edited:
Upvote 0
Thank you for taking the time to test it and to answer !

Like you advised me to do, I tried this

Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]ActiveWorkbook.Save
    ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
    ActiveWorkbook.Save[/FONT]

Instead of just saving it but nothing changed at all. Since I use a Worksheets.Cells.PasteSpecial Paste := xlPasteValues, I really wonder why I have links recovering problems to begin with, since my new file is supposedly, after saving it, just a normal file with no formlas at all, right ?

Thank you again,

Marie
 
Upvote 0
Indeed.

In your new file, you can check 'existing connection' in your data tab.
Formulae as long as within the workbook are fine, might have troubles if linked to other workbooks that are closed when you duplicate the file. I assume you had links since you used
Application.AskToUpdateLinks = False. You might use that code in your duplicating sub before saving but I guess it will be set back to true when closing/opening.
The code seems right, I would only dim Sheet as Worksheet instead of object and not put "
ConflictResolution:=xlOtherSessionChanges" but
XlSaveConflictResolution.xlLocalSessionChanges
and what I usually do is to dim 2 workbooks, the origininal (activeworkbook) as Wb1 and saves as Wb2. When overwriting, it seems you need to use Wb2.displayalterts=false but since your message seems to be about dataintegrity, I am not sure what to do to solve it.
By the way, do you have the issue if you leave your reference/original workbook opened while closing and opening the new one?


 
Upvote 0
Thank you again for your reply,

First of all I tried (without changing anything to my code) to leave my original workbook open while closing and opening the copy. It doesn't change anything to the issue.

Regarding the Application.AskToUpdateLinks = False, honestly it was more like a vain attempt to get rid of the two Windows poping-up than anything else. But ! I went to check 'existing connections' in the data tab and I have no 'connections' to other files but in 'tables' I do have linked tables to the original workbook !

So I tried searching for a way to remove them and this sounded good :

Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub removeconnections()
    
    MsgBox "removeconnection function is entered"
    Dim xConnect As Object
    Dim i As Integer
    i = 1
    For Each xConnect In ActiveWorkbook.Connections
        MsgBox "connection " & i
        If xConnect.Name <> "ThisWorkbookDataModel" Then
            xConnect.Delete
            MsgBox "connection deleted"
        End If
        i = i + 1
    Next xConnect
End Sub[/FONT]

I added MsgBoxes for debugging purposes and the only message I get is "removeconnection function is entered" implying that ActiveWorkbook.Connections is empty and no connection has been deleted, right ? So I find it really weird that now the data tab > existing connections > tables of the copied workbook has no connections with the original workbook anymore ! How could they be deleted if the For Each loop isn't entered ?
Also it didn't solve the issue of the tabs poping up at the opening of the file asking me to recover links. I really am clueless… I'll keep on trying to get rid of it,

Thank you,

Marie
 
Upvote 0
In File/options/advanced you have at the very end an option to 'update links to other documents' in 'when calculating this worbook' segment. It can be set up by Activeworkbook.UpdateRemoteReferences = False.
The 'ask to update automatic link' in next segment can be set up with Application.AskToUpdateLinks = False
 
Upvote 0
Thank you again for your reply,

I tried puting it in the code after the copy has been done completely. I also tried putting it in the ThisWorkbook > Workbook_open() function.
None of it worked, I am sorry...
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top