header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Powershell script error onExcel file
Last Post 13 Apr 2012 05:17 AM by djh53. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BullUser is Offline
New Member
New Member
Posts:1
Avatar

--
13 Apr 2012 12:15 AM

    For all same value of first column value , all related values have to be moved in new file.


    When i run the script , It randomly leaves some rows and throws this error.
    Error is showing Exception Calling "Paste" with "0" argument(s).


    Plz help
    I need i urgently.


    Thanks


    Script::::





    param($file="")
    $working_directory=(pwd).tostring()

    $filename=$working_directory+"\"+$file

    #source file
    $excels = new-object -comobject excel.application
    $excels.Visible = $true
    $wbs = $excels.Workbooks.open($filename)    #$wb = $excel.Workbooks.Add()
    $wss = $wbs.Worksheets.Item(1)

     

     

    $row=2


    while($wss.cells.item($row,1).value2 -ne $null)
     { 
     
      #Destination file
      $exceld = new-object -comobject excel.application
      $exceld.Visible = $true
      $wbd = $exceld.Workbooks.add()   
      $wsd = $wbd.Worksheets.Item(1)


      #filling the first row
      
      $range=$wss.Range("A1").entirerow.copy()
      $wsd.Paste()
      
      

      $currentserver=$wss.cells.item($row,1).value2
      $server=$currentserver
      
      $rowd=2
      while($server -eq $currentserver)
      { 
       
      
       
       
       $range=$wss.range("A"+$row).entirerow.copy()
       #Pointing destination file to first column of 2nd row
       $r = $wsd.Range("A"+$rowd)
       $exceld.GoTo($r) | out-null
       $wsd.Paste() 
       
       $rowd+=1
       $row+=1

       $currentserver=$wss.cells.item($row,1).value2
       
      }
      
      $dfilename=$working_directory+"\"+$server+".xlsx"
      $wbd.SaveAs($dfilename)
      
      #Nulling all remaining variables connected to Exceld
      $Exceld.Quit()
      $Exceld=$Null
      $wbd=$null
      $wsd=$null
      #[GC]::Collect()
      
     }
    #Nulling all remaining variables connected to Excels  
    $Excels.Quit($false)
    $Excels=$Null
    $wbs=$null
    $wss=$null


    #finally releasing the Object Wrapper.
    [GC]::Collect() 

     

    djh53User is Offline
    New Member
    New Member
    Posts:31
    Avatar

    --
    13 Apr 2012 05:17 AM
    It's unlikely two Excel application objects are needed.  An Excel application object can support multiple open workbooks.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer