I have a Power Shell script that extracts data from a CSV file. Captures the data in each column. Then creates a Word Document and enters the data from the CSV file into the Word Document. It does that by searching for KEYWORDS inside the Word Document and replacing them with the data from the CSV file.

It works great except when it finds data that contains multiple lines. The data is separated by a '~'. I need to capture the data in that column and then format it so that it fits in the word document.

Here is a sample of the Data in the column in question .....

"Field~Field~Field~Field~Field~Field~"

When I output it in the script, the Cell is populated like this.... Field~Field~Field~Field~Field~Field~

I need it to populate like this....

Field
Field
Field
Field
Field
Field

I tried using the $row.imports.Split("~") property and it throws a type miss match error

OperationStopped: untitled:Untitled-3:40:5
Line |
  40 |      $newDoc.Content.Find.Execute("Copy_Imports", $false, $true, $fals …
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Type mismatch. (0x80020005 (DISP_E_TYPEMISMATCH))

Here is my script....

# Add reference to Microsoft Office Interop Assemblies
# Add-Type -AssemblyName Microsoft.Office.Interop.Word

Add-Type -Path C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\office.dll
Add-Type -Path C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll
Add-Type -Path C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Outlook\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Outlook.dll

# Load Word application
$word = New-Object -ComObject Word.Application

# Disable alerts and make Word visible
$word.DisplayAlerts = [Microsoft.Office.Interop.Word.WdAlertLevel]::wdAlertsNone
$word.Visible = $true

# Open the Word document
$doc = $word.Documents.Open("C:\Temp\Sample.docx")

# Open the CSV file
$csv = Import-Csv "C:\Temp\Sample.csv"

# Loop through each row in the CSV file
foreach ($row in $csv) {
    # Extract data from the current row
    $data = $row.column_name
    
    # Create a new document based on the original
    #$newDoc = $doc.Copy()
    $newDoc = $word.Documents.Add()
    $newDoc.Range().FormattedText = $doc.Range().FormattedText
    
    # Replace text in the new document with data from the CSV file
    $newDoc.Content.Find.Execute("modname", $false, $true, $false, $false, $false, $true, 1, $false, $row.modname, 2)
    # $newDoc.Content.Find.Execute("calling_modules", $false, $true, $false, $false, $false, $true, 1, $false, $row.calling_modules, 2)
    #$newDoc.Content.Find.Execute("references_files", $false, $true, $false, $false, $false, $true, 1, $false, $row.references_files, 2)
    #$newDoc.Content.Find.Execute("references_tables", $false, $true, $false, $false, $false, $true, 1, $false, $row.references_tables, 2)
    #$newDoc.Content.Find.Execute("Candidates_found", $false, $true, $false, $false, $false, $true, 1, $false, $row.candidates, 2)
    $newDoc.Content.Find.Execute("language_type", $false, $true, $false, $false, $false, $true, 1, $false, $row.language, 2)
    $newDoc.Content.Find.Execute("TotalLoc", $false, $true, $false, $false, $false, $true, 1, $false, $row.TotalLoc, 2)
    $newDoc.Content.Find.Execute("cyclomatic", $false, $true, $false, $false, $false, $true, 1, $false, $row.cyclomatic, 2)
    $newDoc.Content.Find.Execute("Copy_Imports", $false, $true, $false, $false, $false, $true, 1, $false, $row.imports.Split('~'), 2)
    #$newDoc.Content.Find.Execute("Copy_Imports", $false, $true, $false, $false, $false, $true, 1, $false, $row.imports, 2)
    $newDoc.Content.Find.Execute("comloc", $false, $true, $false, $false, $false, $true, 1, $false, $row.comloc, 2)
    $newDoc.Content.Find.Execute("deadloc", $false, $true, $false, $false, $false, $true, 1, $false, $row.deadloc, 2)
    $newDoc.Content.Find.Execute("exeloc", $false, $true, $false, $false, $false, $true, 1, $false, $row.exeloc, 2)
    # $newDoc.Content.Find.Execute("called_modules", $false, $true, $false, $false, $false, $true, 1, $false, $row.called_modules, 2)

    
    # Save the new document with a name based on the current row of the CSV file
    $newDoc.SaveAs("C:\Temp\BRE_Generator\" + $row.modname + ".docx")
    
    # Close the new document
    $newDoc.Close()
}

# Close the Word document
$doc.Close()

# Quit Word application
$word.Quit()
0

There are 0 best solutions below