# Excel CSV export only for files that have refresh data

**URL:** <https://forums.powershell.org/t/excel-csv-export-only-for-files-that-have-refresh-data/7759>\
**Category:** PowerShell Help\
**Created:** [December 25, 2016, 4:52pm UTC](https://forums.powershell.org/t/excel-csv-export-only-for-files-that-have-refresh-data/7759 "2016-12-25T16:52:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paps-sale](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@paps-sale](https://forums.powershell.org/u/paps-sale)\
**Post date:** [December 25, 2016, 4:52pm UTC](https://forums.powershell.org/t/excel-csv-export-only-for-files-that-have-refresh-data/7759/1 "2016-12-25T16:52:05Z")

</div>

I have a powershell script that goes through all xlsx files within each child folder, refreshes their data and saves a CSV file for each of them (also overwriting the original xlsx files).

I’m currently looking for a way for the powershell script to only create CSV files when the refresh data-source actually exists.  
If it doesn’t, excel wouldn’t create the CSV file (in other words, when the “Excel cannot find the text file to refresh…” warning happens, it would not save a CSV file).

Below is my current code:

```
$files = Get-ChildItem -Path "E:\Feeds\" *.xlsx -Recurse
$xl = New-Object -ComObject Excel.Application
$xl.DisplayAlerts = $FALSE
foreach ($file in $files) {

    $newFile = "$($file.DirectoryName)\$($file.BaseName).csv"
    $newFile2 = "$($file.DirectoryName)\$($file.BaseName).xlsx"
    $wb = $xl.Workbooks.Open($file.FullName)
    $wb.RefreshAll()
    if (Test-Path -Path $newFile) {
        
        Remove-Item $newFile -Force
    
    }
    $wb.SaveAs("$newFile2",51)
    $wb.SaveAs("$newFile",6)
    $wb.Close($true)
}
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
```

If someone could help me with this one, I would be extremely grateful!

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [December 27, 2016, 5:37pm UTC](https://forums.powershell.org/t/excel-csv-export-only-for-files-that-have-refresh-data/7759/2 "2016-12-27T17:37:54Z")

</div>

I’m not certain that the Excel COM object is able to “see” the data source status, to be honest. That’s what you’d need to figure out, but it’s a VERY old piece of code that hasn’t been updated in almost a decade.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/excel-csv-export-only-for-files-that-have-refresh-data/7759/3 "2024-05-16T20:39:55Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


