Powershell script for setting Multi User Editing in excel for numerous files?












0















I've written a code that tries to go through my company server and set all the excel files, under a certain folder, to shared so that multiple people can edit them at once. This has been a problem for a very long time here and I thought I had a nice code to fix this but I can't seem to access the property correctly. Can anyone help?



$ErrorActionPreference = "Stop"

$root = "P:A N G"

$excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

foreach ($excelFile in $excelFiles.FullName)
{
$excel = New-Object -ComObject Excel.Application
$excelWorkBook = $excel.Workbooks.Open($excelFile)

if (!$excelWorkBook.MultiUserEditing)
{
Write-Host $excelFile
$excelWorkBook._SaveAs([Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared)
$excelWorkBook.Close
}
}









share|improve this question























  • Is that accelerated .NET method, xlShared, just a boolean? Also, after you call Close(), most PS/Excel code I've seen calls .Quit() after that as well. Are you able to open those Excel files from your machine? Or can you query the property on one of those Excel objects that you're trying to change after the code runs?

    – trebleCode
    Nov 21 '18 at 20:06











  • @trebleCode For future reference, type accelerators are a specific class in the powershell library which essentially aliases types and the term does not apply to every use of a .NET type (which is what is happening here). Observe here: [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get It's likely an enumeration that they're using

    – TheIncorrigible1
    Nov 21 '18 at 20:23













  • Cheers @TheIncorrigible1, your protips are always good!

    – trebleCode
    Nov 21 '18 at 20:26











  • @trebleCode glad to be of service. You can also utilize that type's static member Add to define your own type accelerators (I use this functionality in my $profile for shortcuts)

    – TheIncorrigible1
    Nov 21 '18 at 20:30











  • @TheIncorrigible1 good to know, I can forsee making alot of use of that in the near future

    – trebleCode
    Nov 21 '18 at 20:36
















0















I've written a code that tries to go through my company server and set all the excel files, under a certain folder, to shared so that multiple people can edit them at once. This has been a problem for a very long time here and I thought I had a nice code to fix this but I can't seem to access the property correctly. Can anyone help?



$ErrorActionPreference = "Stop"

$root = "P:A N G"

$excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

foreach ($excelFile in $excelFiles.FullName)
{
$excel = New-Object -ComObject Excel.Application
$excelWorkBook = $excel.Workbooks.Open($excelFile)

if (!$excelWorkBook.MultiUserEditing)
{
Write-Host $excelFile
$excelWorkBook._SaveAs([Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared)
$excelWorkBook.Close
}
}









share|improve this question























  • Is that accelerated .NET method, xlShared, just a boolean? Also, after you call Close(), most PS/Excel code I've seen calls .Quit() after that as well. Are you able to open those Excel files from your machine? Or can you query the property on one of those Excel objects that you're trying to change after the code runs?

    – trebleCode
    Nov 21 '18 at 20:06











  • @trebleCode For future reference, type accelerators are a specific class in the powershell library which essentially aliases types and the term does not apply to every use of a .NET type (which is what is happening here). Observe here: [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get It's likely an enumeration that they're using

    – TheIncorrigible1
    Nov 21 '18 at 20:23













  • Cheers @TheIncorrigible1, your protips are always good!

    – trebleCode
    Nov 21 '18 at 20:26











  • @trebleCode glad to be of service. You can also utilize that type's static member Add to define your own type accelerators (I use this functionality in my $profile for shortcuts)

    – TheIncorrigible1
    Nov 21 '18 at 20:30











  • @TheIncorrigible1 good to know, I can forsee making alot of use of that in the near future

    – trebleCode
    Nov 21 '18 at 20:36














0












0








0








I've written a code that tries to go through my company server and set all the excel files, under a certain folder, to shared so that multiple people can edit them at once. This has been a problem for a very long time here and I thought I had a nice code to fix this but I can't seem to access the property correctly. Can anyone help?



$ErrorActionPreference = "Stop"

$root = "P:A N G"

$excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

foreach ($excelFile in $excelFiles.FullName)
{
$excel = New-Object -ComObject Excel.Application
$excelWorkBook = $excel.Workbooks.Open($excelFile)

if (!$excelWorkBook.MultiUserEditing)
{
Write-Host $excelFile
$excelWorkBook._SaveAs([Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared)
$excelWorkBook.Close
}
}









share|improve this question














I've written a code that tries to go through my company server and set all the excel files, under a certain folder, to shared so that multiple people can edit them at once. This has been a problem for a very long time here and I thought I had a nice code to fix this but I can't seem to access the property correctly. Can anyone help?



$ErrorActionPreference = "Stop"

$root = "P:A N G"

$excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

foreach ($excelFile in $excelFiles.FullName)
{
$excel = New-Object -ComObject Excel.Application
$excelWorkBook = $excel.Workbooks.Open($excelFile)

if (!$excelWorkBook.MultiUserEditing)
{
Write-Host $excelFile
$excelWorkBook._SaveAs([Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared)
$excelWorkBook.Close
}
}






excel powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:54









user10484977user10484977

12




12













  • Is that accelerated .NET method, xlShared, just a boolean? Also, after you call Close(), most PS/Excel code I've seen calls .Quit() after that as well. Are you able to open those Excel files from your machine? Or can you query the property on one of those Excel objects that you're trying to change after the code runs?

    – trebleCode
    Nov 21 '18 at 20:06











  • @trebleCode For future reference, type accelerators are a specific class in the powershell library which essentially aliases types and the term does not apply to every use of a .NET type (which is what is happening here). Observe here: [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get It's likely an enumeration that they're using

    – TheIncorrigible1
    Nov 21 '18 at 20:23













  • Cheers @TheIncorrigible1, your protips are always good!

    – trebleCode
    Nov 21 '18 at 20:26











  • @trebleCode glad to be of service. You can also utilize that type's static member Add to define your own type accelerators (I use this functionality in my $profile for shortcuts)

    – TheIncorrigible1
    Nov 21 '18 at 20:30











  • @TheIncorrigible1 good to know, I can forsee making alot of use of that in the near future

    – trebleCode
    Nov 21 '18 at 20:36



















  • Is that accelerated .NET method, xlShared, just a boolean? Also, after you call Close(), most PS/Excel code I've seen calls .Quit() after that as well. Are you able to open those Excel files from your machine? Or can you query the property on one of those Excel objects that you're trying to change after the code runs?

    – trebleCode
    Nov 21 '18 at 20:06











  • @trebleCode For future reference, type accelerators are a specific class in the powershell library which essentially aliases types and the term does not apply to every use of a .NET type (which is what is happening here). Observe here: [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get It's likely an enumeration that they're using

    – TheIncorrigible1
    Nov 21 '18 at 20:23













  • Cheers @TheIncorrigible1, your protips are always good!

    – trebleCode
    Nov 21 '18 at 20:26











  • @trebleCode glad to be of service. You can also utilize that type's static member Add to define your own type accelerators (I use this functionality in my $profile for shortcuts)

    – TheIncorrigible1
    Nov 21 '18 at 20:30











  • @TheIncorrigible1 good to know, I can forsee making alot of use of that in the near future

    – trebleCode
    Nov 21 '18 at 20:36

















Is that accelerated .NET method, xlShared, just a boolean? Also, after you call Close(), most PS/Excel code I've seen calls .Quit() after that as well. Are you able to open those Excel files from your machine? Or can you query the property on one of those Excel objects that you're trying to change after the code runs?

– trebleCode
Nov 21 '18 at 20:06





Is that accelerated .NET method, xlShared, just a boolean? Also, after you call Close(), most PS/Excel code I've seen calls .Quit() after that as well. Are you able to open those Excel files from your machine? Or can you query the property on one of those Excel objects that you're trying to change after the code runs?

– trebleCode
Nov 21 '18 at 20:06













@trebleCode For future reference, type accelerators are a specific class in the powershell library which essentially aliases types and the term does not apply to every use of a .NET type (which is what is happening here). Observe here: [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get It's likely an enumeration that they're using

– TheIncorrigible1
Nov 21 '18 at 20:23







@trebleCode For future reference, type accelerators are a specific class in the powershell library which essentially aliases types and the term does not apply to every use of a .NET type (which is what is happening here). Observe here: [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get It's likely an enumeration that they're using

– TheIncorrigible1
Nov 21 '18 at 20:23















Cheers @TheIncorrigible1, your protips are always good!

– trebleCode
Nov 21 '18 at 20:26





Cheers @TheIncorrigible1, your protips are always good!

– trebleCode
Nov 21 '18 at 20:26













@trebleCode glad to be of service. You can also utilize that type's static member Add to define your own type accelerators (I use this functionality in my $profile for shortcuts)

– TheIncorrigible1
Nov 21 '18 at 20:30





@trebleCode glad to be of service. You can also utilize that type's static member Add to define your own type accelerators (I use this functionality in my $profile for shortcuts)

– TheIncorrigible1
Nov 21 '18 at 20:30













@TheIncorrigible1 good to know, I can forsee making alot of use of that in the near future

– trebleCode
Nov 21 '18 at 20:36





@TheIncorrigible1 good to know, I can forsee making alot of use of that in the near future

– trebleCode
Nov 21 '18 at 20:36












1 Answer
1






active

oldest

votes


















0














With some help from a friend I managed to come up with a solution actually.



$ErrorActionPreference = "Stop"

$root = "P:A N G"

$excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

foreach ($excelFile in $excelFiles.FullName)
{
$excel = New-Object -ComObject Excel.Application
$excelWorkBook = $excel.Workbooks.Open($excelFile)

if (!$excelWorkBook.MultiUserEditing)
{
Write-Host $excelWorkBook.FullName
$accessMode = [Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
$excelWorkBook.SaveAs($excelWorkBook.FullName,$xlFixedFormat,$null,$null,$null,$null,$accessMode,$null,$null,$null,$null,$null)
$excelWorkBook.Close
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419596%2fpowershell-script-for-setting-multi-user-editing-in-excel-for-numerous-files%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    With some help from a friend I managed to come up with a solution actually.



    $ErrorActionPreference = "Stop"

    $root = "P:A N G"

    $excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

    foreach ($excelFile in $excelFiles.FullName)
    {
    $excel = New-Object -ComObject Excel.Application
    $excelWorkBook = $excel.Workbooks.Open($excelFile)

    if (!$excelWorkBook.MultiUserEditing)
    {
    Write-Host $excelWorkBook.FullName
    $accessMode = [Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared
    $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
    $excelWorkBook.SaveAs($excelWorkBook.FullName,$xlFixedFormat,$null,$null,$null,$null,$accessMode,$null,$null,$null,$null,$null)
    $excelWorkBook.Close
    }
    }





    share|improve this answer




























      0














      With some help from a friend I managed to come up with a solution actually.



      $ErrorActionPreference = "Stop"

      $root = "P:A N G"

      $excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

      foreach ($excelFile in $excelFiles.FullName)
      {
      $excel = New-Object -ComObject Excel.Application
      $excelWorkBook = $excel.Workbooks.Open($excelFile)

      if (!$excelWorkBook.MultiUserEditing)
      {
      Write-Host $excelWorkBook.FullName
      $accessMode = [Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared
      $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
      $excelWorkBook.SaveAs($excelWorkBook.FullName,$xlFixedFormat,$null,$null,$null,$null,$accessMode,$null,$null,$null,$null,$null)
      $excelWorkBook.Close
      }
      }





      share|improve this answer


























        0












        0








        0







        With some help from a friend I managed to come up with a solution actually.



        $ErrorActionPreference = "Stop"

        $root = "P:A N G"

        $excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

        foreach ($excelFile in $excelFiles.FullName)
        {
        $excel = New-Object -ComObject Excel.Application
        $excelWorkBook = $excel.Workbooks.Open($excelFile)

        if (!$excelWorkBook.MultiUserEditing)
        {
        Write-Host $excelWorkBook.FullName
        $accessMode = [Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared
        $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
        $excelWorkBook.SaveAs($excelWorkBook.FullName,$xlFixedFormat,$null,$null,$null,$null,$accessMode,$null,$null,$null,$null,$null)
        $excelWorkBook.Close
        }
        }





        share|improve this answer













        With some help from a friend I managed to come up with a solution actually.



        $ErrorActionPreference = "Stop"

        $root = "P:A N G"

        $excelFiles = Get-ChildItem -path $root -File "*.xlsx" -Recurse

        foreach ($excelFile in $excelFiles.FullName)
        {
        $excel = New-Object -ComObject Excel.Application
        $excelWorkBook = $excel.Workbooks.Open($excelFile)

        if (!$excelWorkBook.MultiUserEditing)
        {
        Write-Host $excelWorkBook.FullName
        $accessMode = [Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlShared
        $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
        $excelWorkBook.SaveAs($excelWorkBook.FullName,$xlFixedFormat,$null,$null,$null,$null,$accessMode,$null,$null,$null,$null,$null)
        $excelWorkBook.Close
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 20:49









        user10484977user10484977

        12




        12
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419596%2fpowershell-script-for-setting-multi-user-editing-in-excel-for-numerous-files%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter