+ Reply to Thread
Results 1 to 14 of 14

corrupted file- result of output of VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    corrupted file- result of output of VBA code

    I have an excel with VBA code that allows users to click a button to save the created report to an normal excel file without all the macros and code.

    They use Excel 2007 and I use Excel 2010- works fine for me- but they get a corruption when they try to reopen the report file. I get a corruption as well when I try to open a file they have created- is there anyway to tell what is causing this corruption?


    Code is:
    Dim newWb As Workbook, wbFileName As String
           Set newWb = Workbooks.Add
        wbFileName = Application.GetSaveAsFilename( _
        fileFilter:="Excel  Workbook (*.xlsx), *.xls")
        If wbFileName <> "False" Then
            newWb.SaveAs wbFileName
        End If

    Corrupted file is attached
    Last edited by Cutter; 07-16-2012 at 01:44 PM. Reason: Added code tags

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: corrupted file- result of output of VBA code

    There's no attachment.

    You need to specify the FileFormat, so using xlsx, fileformat 51 will save without the code. Switching off DisplayAlerts will stop the warning prompt

    
    Option Explicit
    
    Sub SaveCopy()
        Dim wbFileName As String
    
        'amend the path to save to
        wbFileName = InputBox("Enter name for new file")
        Application.DisplayAlerts = False
        If wbFileName <> "" Then ThisWorkbook.SaveAs "C:\" & wbFileName & ".xlsx", FileFormat:=51
        Application.DisplayAlerts = True
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Hi- I added Fileformat 51 to my code - appreciate the help.

    I will see if it resolves the issue

  4. #4
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    They are still claiming issues when they shut the computer down- very weird- will find out tomorrow what is going on i hope

  5. #5
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Appears there is a known Excel 2010 bug http://social.msdn.microsoft.com/For...e-7748f3c83175

    Suggested fix is
    i had the same issue and within the loop of creating the charts i added a line to activate the chart object which solved the issue.



    For Each ws In ActiveWorkbook.Worksheets
        i = 0
        For Each oCht In ws.ChartObjects
            ' added next line to activate -  fix export issue in excel 2010
            oCht.Activate
            i = i + 1
            fName = ws.Name & i & ".gif"
            oCht.Chart.Export Filename:=fName, FilterName:="GIF"
        Next
    Next
    
    
    So my question is how do I modify my code below- 
    
    For i = 1 To ActiveSheet.ChartObjects.Count
        ActiveSheet.ChartObjects(i).Name = "Chart " & i
     Next i
    Last edited by Paul; 07-17-2012 at 02:23 PM. Reason: Added CODE tags (you can edit posts to add them rather than creating additional posts.)

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: corrupted file- result of output of VBA code

    Where did exporting charts as gif come from? It wasn't mentioned in your first post

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: corrupted file- result of output of VBA code

    You need to add Code Tags

  8. #8
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Sorry about that

    Appears there is a known Excel 2010 bug http://social.msdn.microsoft.com/For...e-7748f3c83175

    Suggested fix is
    i had the same issue and within the loop of creating the charts i added a line to activate the chart object which solved the issue.


    For Each ws In ActiveWorkbook.Worksheets
    i = 0
    For Each oCht In ws.ChartObjects
    ' added next line to activate - fix export issue in excel 2010
    oCht.Activate
    i = i + 1
    fName = ws.Name & i & ".gif"
    oCht.Chart.Export Filename:=fName, FilterName:="GIF"
    Next
    Next
    So my question is how do I modify my code below-
    For i = 1 To ActiveSheet.ChartObjects.Count
    ActiveSheet.ChartObjects(i).Name = "Chart " & i
    Next i

  9. #9
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Hi Roy
    No exporting gifs for me- that was the snip form the link that said it was a known bug in Excel.

    I am simply renaming the charts after copying the sheet and want to activate each one as they are renamed to see if ti fixes the corruption issue

  10. #10
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Would this work?
     'Activating charts
    Dim myChart As ChartObject
    For Each myChart In Sheets("Output Report").ChartObjects
        myChart.Activate
    Next myChart
    
    ' Renaming the Charts so they are always the same Chart number
    
    For i = 1 To ActiveSheet.ChartObjects.Count
        ActiveSheet.ChartObjects(i).Name = "Chart " & i
    Next i

  11. #11
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Removed- erroneous post
    Last edited by delaneybob; 07-17-2012 at 09:46 PM.

  12. #12
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Does this address the issue?

    ' Renaming the Charts so they are always the same Chart number
    For i = 1 To ActiveSheet.ChartObjects.Count
    ' this loop shuld stop the corruption issue
        Dim myChart As ChartObject
        For Each myChart In Sheets("Output Report").ChartObjects
            myChart.Activate
        Next myChart
    ActiveSheet.ChartObjects(i).Name = "Chart " & i
    Next i

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: corrupted file- result of output of VBA code

    Try:
    ' Renaming the Charts so they are always the same Chart number
    For i = 1 To ActiveSheet.ChartObjects.Count
    ' this loop should stop the corruption issue
    with ActiveSheet.ChartObjects(i)
    .activate
    .Name = "Chart " & i
    end with
    Next i
    Everyone who confuses correlation and causation ends up dead.

  14. #14
    Registered User
    Join Date
    02-06-2012
    Location
    roswell
    MS-Off Ver
    O365
    Posts
    95

    Re: corrupted file- result of output of VBA code

    Many thanks for the cleaner code- I will make other changes and work with the person try it tomorrow with the person having the corruption issue, I got dinged for cross posting- i thought the topic had moved to VBA and did not read the rules

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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