Archive for the ‘MS Office & VBA’ Category.

E-Mails mit personalisierten Attachments versenden

Vor kurzem hatte ich die dankbare Aufgabe, mehrere hundert E-Mails mit personalisierten Anhängen (im PDF-Format) zu versenden. Da auf die Schnelle kein Tool bereitstand, das bei dieser Aufgabe helfen konnte, musste etwas Selbstentwickeltes her.

Teil 1 der Aufgabe bestand darin, aus der PDF-Gesamtdatei jeweils zwei Seiten zu extrahieren und abzuspeichern. In einer Excel standen glücklicherweise die Namen der Kunden sowie die Kunden-ID. Die Exceldatei habe ich dann als CSV exportiert (man hätte die Excel-Datei sicher auch direkt lesen können) und ein Powershellskript für PDFTK geschrieben.

# Uwe Ziegenhagen, 20101207
 
# read addresses, tab-delimited
$entries = Import-Csv -Delimiter "`t" c:\adresses.txt
# change to directory of PDF pages
cd c:\Email_Sende_Aktion\
# user counter variable ($i contains an object)
$j = 0
foreach ($i in $entries){
$j = $j +1 
# take filename of new PDF from CSV
$filename = $i.addressid + ".pdf"
# calculate pages to be extracted
$pagestoextract = "A" + $j + "-" + ($j+1)
# call pdftk
pdftk A= bigserialletter.pdf cat $pagestoextract output $filename 
$j = $j +1 
}

Teil 2 der Aufgabe bestand dann darin, jedem Kunden eine E-Mail mit seinem Attachment zu schicken, kein Kunde sollte ein PDF erhalten, das nicht für ihn bestimmt war. Dank Powershells „send-mailmessage“ ließ sich das auch recht einfach umsetzen.

# generate Encoding object for UTF8 (no äüö otherwise)
$encoding = [System.Text.Encoding]::UTF8
 
# Import email addresses and fund ids
$files = Import-Csv -Delimiter "`t" c:\Email_Sende_Aktion\adresses.txt
 
# read mail body, preserve line breaks
# http://www.eggheadcafe.com/software/aspnet/33110753/passing-a-string-with-linebreaks-to-a-com-object.aspx
$body  = [io.file]::ReadAllText("c:\Email_Sende_Aktion\mailbody.txt")
 
# just a counter for checking
$t = 0;
foreach ($i in $files){
	# inc counter
	$t = $t +1
	# cat body from the different parts
	$usedbody = "Sehr geehrte/r " + $i.Anrede.Trim() + ". " + $i.Nachname.Trim() + "," + $body 
	# find the individual attachment based on addressid
	$file = "c:\Email_Sende_Aktion\" + $i.addressid + ".pdf"
	# send message
	 send-mailmessage -Encoding $encoding -From me@me.com -BCC notme@me.com -To $i.RealMail -Subject "Kundeninformation Foobar"  -Body $usedbody -Smtpserver nameofexchangeserver -Attachments  $file
	$t
}

English summary: If you have no dedicated tool to send personalized PDF-attachments via e-mail, you can easily use Powershell to generate theses e-mails. Part 1 of the task consisted of splitting a big PDF file with all serial letters into 2-page pieces, task 2 of sending the e-mails to the recipients.

Keywords: Powershell, Outlook, Attachment, PDF

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

CUDA mit Excel-Unterstützung

Die kommende Version 3.1 von Nvidias Programmierbibliothek CUDA kommt mit Excel-Unterstützung. Sobald ich Excel 2010 habe, werde ich mir das mal genauer anschauen.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Powershell Excel fernsteuern

Was mit Word geht, geht auch mit Excel. Hier ein Beispiel, das ich im Netz (http://www.vistax64.com/powershell/173327-make-excel-chart-powershell.html) gefunden habe und etwas angepasst habe. Schade, dass die Sprachausgabe nur englisch ist:

# http://www.vistax64.com/powershell/173327-make-excel-chart-powershell.html
$excel = New-object -comobject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.Worksheets.Item(1)
$sheet.Name = 'Hallo Welt'
 
foreach ($i in 1..25){
    $sheet.cells.item($i,1) = $i
    $sheet.cells.item($i,2) = $i*2
}
 
# Bar Chart
# Datenherkunft
$range = $sheet.range('a1:a25')
 
# Chart-Variable anlegen
$chart = $sheet.shapes.addChart().chart
# Typ zuweisen (Bar Chart)
$chart.chartType = 58
# Daten zuweisen
$chart.setSourceData($range)
1..48 | % {$chart.chartStyle = $_; $excel.speech.speak("Style $_"); sleep 2}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Word-Dokumente mit Powershell generieren

Durch die integrierte COM-Anbindung ist es einfach, Applikationen wie Word und Excel über die Powershell fernzusteuern. Ausgehend von dem Beispiel auf http://command-line-programming.suite101.com/article.cfm/how_to_create_a_word_document_with_powershell, das ich etwas anpassen musste, hier ein ‚Minimalbeispiel‘:

$oWord = New-Object -Com Word.Application
$oWord.Visible = $true
 
$oMissing = [System.Reflection.Missing]::Value
$oDoc = $oWord.Documents.Add($oMissing, $oMissing, $oMissing, $oMissing)
 
# $odoc | get-Member save* |Select-Object definition| format-list
 
$oPara1 = $oDoc.Paragraphs.Add($oMissing)
 
$oPara1.Range.Style = "Überschrift 1"
$oPara1.Range.Text = "Hallo, ich bin etwas Text"
 
$oPara1.Range.InsertParagraphAfter()
$oPara1.Range.Text = "Hallo, ich bin etwas Text"
$oPara1.Range.InsertParagraphAfter()
 
$oPara2 = $oDoc.Paragraphs.Add($oMissing)
$oPara2.Range.Text = "Hier der zweite Absatz"
$oPara2.Range.InsertParagraphAfter()
 
$filename = "C:\MeinDokument.doc"
$oDoc.SaveAs([ref]$filename,[ref]$oMissing, 
[ref]$oMissing,[ref]$oMissing, [ref]$oMissing,
[ref]$oMissing,[ref]$oMissing,[ref]$oMissing, 
[ref]$oMissing,[ref]$oMissing, [ref]$oMissing,
[ref]$oMissing,[ref]$oMissing,[ref]$oMissing,
[ref]$oMissing)
 
$oDoc.Close()
$oWord.Quit()

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Programmieren für Einsteiger mit Microsoft Small Basic

Von Microsoft Small Basic gibt es jetzt die siebente Community Preview. Bei Small Basic handelt es um eine Programmiersprache für Einsteiger, die mit wenigen Schlüsselwörtern auskommt. Microsoft selbst schreibt auf http://msdn.microsoft.com/en-us/devlabs/cc950524.aspx:

Microsoft Small Basic aims to make computer programming accessible to beginners.

Ein PDF-Tutorial gibt es auch: http://download.microsoft.com/download/9/0/6/90616372-C4BF-4628-BC82-BD709635220D/Introducing%20Small%20Basic.pdf.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Excel Dubletten filtern

Kürzlich musste ich wissen, welche Elemente in einer Excelliste nur einmal vorkamen. Die Lösung liegt in der Verwendung der SUMMEWENN-Funktion, das Bild zeigt wie.

excel_summewenn

Um Dubletten herauszulöschen hat Excel auch eine entsprechende Funktion unter Daten => Duplikate entfernen.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Keys von installierter Microsoft-Software auslesen

Chip online hat einen Link zu ProduKey, einer Software die die Installationsschlüssel von installierter MS Software auslesen kann. Wenn man den eigenen Schlüssel verschusselt hat, kann man ihn so wieder ermitteln.

http://www.chip.de/downloads/ProduKey_38097950.html

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website