Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Need help!! - Macro (Excel), working with Powershell

$
0
0

Need help with an macro for excel or translation the script to powershell in general.

1)
Macro is saved inside an excel-file as "mappe1.xlsm"
This macro should open different excel-files inside a folder (excel-files are always the same, only the name is different). The task of this macro should be: open each excel-file, format the tables in columns etc, then close the file.

In my powershell script it looks right now like this and works, but I want include the macro, which format the pdf-output before

$labelPDFVerzeichnis.Text = $pfad_allgemein_pdf
	[System.Windows.Forms.MessageBox]::Show("Die PDF-Dokumente werden im validierten Umfeld abgelegt.", "Hinweis:", 0)
	# PDF
	$path = $folder.Self.Path
	$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type]
	$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -Recurse
	$objExcel = New-Object -ComObject excel.application
	$objExcel.visible = $false
	foreach ($wb in $excelFiles)
	{
		foreach ($worksheet in $wb.Worksheets)
		{
			$worksheet.PageSetup.Orientation = xlLandscape
			$Header = $Worksheet.cells.item(3, $column).text
		}
		$wb.Worksheets.PageSetup.Zoom = 60
		$filepath = Join-Path -Path $pfad_allgemein_pdf -ChildPath ($wb.BaseName + ".pdf")
		$workbook = $objExcel.workbooks.open($wb.fullname, 3)
		$workbook.Saved = $true"saving $filepath"
		$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
		$objExcel.Workbooks.close()
	}
	[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
#Ende

here is the code I found at microsoft, which I want include.

# macro
	$excel = new-object -comobject excel.application
	$excelFiles = Get-ChildItem -Path $folder.Self.Path -Include * .xlsx, * .xlsm -Recurse
	$macrobook = $excel.workbooks.open("E:\Temp\mappe1.xlsm")
	Foreach ($file in $excelFiles)
	{
		$workbook = $excel.workbooks.open($file.fullname)
		$worksheet = $workbook.worksheets.item(1)
		$excel.Application.Run("mappe1.xlsm"("Modul1"))
		$workbook.save()
		$workbook.close()
	}
	$macrobook.close()
	$excel.quit()

So, how I can combine both codes, that powershell opens the macro and formats each excel-file in the folder, where are all excel-files are stored ?. 

2) Or are there possibilities to "translate" a vba-code from macro to powershell. As I told, the code of macro only format all cells, worksheets in auto-column, text-size and add an header and footer with needed text.

Thanks in advance for each help!!!


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>