Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用PowerShell将文本文件数据复制到现有Excel工作簿_Vba_Excel_Powershell - Fatal编程技术网

Vba 使用PowerShell将文本文件数据复制到现有Excel工作簿

Vba 使用PowerShell将文本文件数据复制到现有Excel工作簿,vba,excel,powershell,Vba,Excel,Powershell,因此,我在将Text.文件中的数据导出到包含月份数据的Excel工作簿时遇到问题。最终的结果是,代码打开了一个新工作簿,其中包含名为“Geraldine3-16-2016”的工作表的名称和标题,我不介意,但它从未添加或复制到主工作簿中。最终,主工作簿中唯一的变化是创建了一个名为“sheet 1”的新工作表,但文本文件中没有数据。非常感谢您的帮助,提前谢谢 function Release-Ref ($ref) { ([System.Runtime.InteropServices.Marshal

因此,我在将Text.文件中的数据导出到包含月份数据的Excel工作簿时遇到问题。最终的结果是,代码打开了一个新工作簿,其中包含名为“Geraldine3-16-2016”的工作表的名称和标题,我不介意,但它从未添加或复制到主工作簿中。最终,主工作簿中唯一的变化是创建了一个名为“sheet 1”的新工作表,但文本文件中没有数据。非常感谢您的帮助,提前谢谢

function Release-Ref ($ref) { 
([System.Runtime.InteropServices.Marshal]::ReleaseComObject( 
[System.__ComObject]$ref) -gt 0) 
[System.GC]::Collect() 
[System.GC]::WaitForPendingFinalizers() 
} 

$File='C:\users\cesar.sanchez\downloads\Returns data 2-16-15.xlsx'
$TextFile='C:\Users\cesar.sanchez\downloads\Geraldine3-16-2016.txt'

$Excel = New-Object -C Excel.Application
$Excel.Visible=$true #For troubleshooting purposes only.
# $Excel.DisplayAlerts = $false 
$TextData = $Excel.Workbooks.Opentext($TextFile,$null,$true) 
$ExcelData = $Excel.Workbooks.Open($File) # Open Template
$NewS_ExcelData=$ExcelData.sheets.add()
$TexttoCopy=$TextData.Sheets.item(1)
$TexttoCopy.copy($NewS_ExcelData)
我相信这与代码的这一部分有关,但我不能完全确定

$NewS_ExcelData=$ExcelData.sheets.add()
$TexttoCopy=$TextData.Sheets.item(1)
$TexttoCopy.copy($NewS_ExcelData)

.OpenText
.Open
不同。它不返回对象。(找到了艰难的道路!)

$TexttoCopy=$TextData.Sheets.item(1)
抛出错误:

You cannot call a method on a null-valued expression.
备选代码:

$File='C:\users\cesar.sanchez\downloads\Returns data 2-16-15.xlsx'
$TextFile='C:\Users\cesar.sanchez\downloads\Geraldine3-16-2016.txt'

$Excel = New-Object -C Excel.Application
$Excel.Visible=$true #For troubleshooting purposes only.
# $Excel.DisplayAlerts = $false 
$Excel.Workbooks.Opentext($TextFile,$null,$true)   # Open Text file
$TextData       = $Excel.ActiveWorkbook            # Assign active workbook
$ExcelData      = $Excel.Workbooks.Open($File)     # Open Template

$NewS_ExcelData = $ExcelData.sheets.add()
$TexttoCopy     = $TextData.Sheets.item(1)

$TexttoCopy.copy($NewS_ExcelData)

您还可能会发现
$TexttoCopy.move($NewS\u ExcelData)
很有用。

。OpenText
不同。Open
。它不返回对象。(找到了艰难的道路!)

$TexttoCopy=$TextData.Sheets.item(1)
抛出错误:

You cannot call a method on a null-valued expression.
备选代码:

$File='C:\users\cesar.sanchez\downloads\Returns data 2-16-15.xlsx'
$TextFile='C:\Users\cesar.sanchez\downloads\Geraldine3-16-2016.txt'

$Excel = New-Object -C Excel.Application
$Excel.Visible=$true #For troubleshooting purposes only.
# $Excel.DisplayAlerts = $false 
$Excel.Workbooks.Opentext($TextFile,$null,$true)   # Open Text file
$TextData       = $Excel.ActiveWorkbook            # Assign active workbook
$ExcelData      = $Excel.Workbooks.Open($File)     # Open Template

$NewS_ExcelData = $ExcelData.sheets.add()
$TexttoCopy     = $TextData.Sheets.item(1)

$TexttoCopy.copy($NewS_ExcelData)
您还可能发现
$TexttoCopy.move($NewS\u ExcelData)
非常有用