Forum Discussion
Osten24
Oct 16, 2020Copper Contributor
Tiny Life Plaguing Boxes
Hello to anyone out there reading this. I ran into a peculiar issue this morning while testing some Microsoft Word to Excel transfer scripting I had started the other day. For some reason Microso...
- Oct 16, 2020
Discovered the fix for the issue. It seems as if this was more an issue between communications with my c# scripting and word, rather excel. Excel I am sorry for questioning your abilities. He just can't read asciii symbols and apparently expresses them as boxes. I simply added the below code if anyone was ever encountering this issue when using Microsoft.Office.Interop interactions.
text = text.Substring(0, text.Length - 2);Simply removed the last two extra characters. I'd rather a better solution but this should get me through development.
Osten24
Oct 16, 2020Copper Contributor
And I don't suppose this would help but in case it could alleviate the circumstances I've attached my working c# scripting for this transfer concept.
I know it's rusty I just tossed it together quickly the other day.
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using System;
using System.Windows.Forms;
namespace WordTesting3
{
class Program
{
[STAThreadAttribute]
static void Main(string[] args)
{
ConsoleKeyInfo cki;
Console.WriteLine("Please Use The Executable Properly, Allow The Program To Do What It Is Meant To Do, Do Not Close Any Working Files, Do Not Edit Any Working Files. Will Result In Memory Loss");
Console.WriteLine("Press ESC to Escape");
Console.WriteLine("Press any key to Continue...");
cki = Console.ReadKey();
if(cki.Key == ConsoleKey.Escape)
{
System.Environment.Exit(1);
}
do
{
Transfer();
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Press ESC to Escape");
Console.WriteLine("Press any key to Continue...");
Console.WriteLine("");
cki = Console.ReadKey();
Console.WriteLine("");
} while (cki.Key != ConsoleKey.Escape);
}
static void Transfer()
{
try
{
//Word
var openFileDialogWord = new OpenFileDialog();
// Set filter options and filter index
openFileDialogWord.Filter = "Word Documents (.docx)|*.docx|All files (*.*)|*.*";
openFileDialogWord.FilterIndex = 1;
openFileDialogWord.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
openFileDialogWord.ShowDialog();
var word = new Word.Application();
object miss = System.Reflection.Missing.Value;
object path = openFileDialogWord.FileName;
object readOnly = true;
var docs = word.Documents.Open(ref path, ref miss, ref readOnly,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
//Excel
var openFileDialogExcel = new OpenFileDialog();
// Set filter options and filter index
openFileDialogExcel.Filter = "Excel Documents (*.xlsx)|*.xlsx|All files (*.*)|*.*";
openFileDialogExcel.FilterIndex = 1;
openFileDialogExcel.Multiselect = false;
// Call the ShowDialog method to show the dialog box.
openFileDialogExcel.ShowDialog();
var excel = new Excel.Application();
object exmiss = System.Reflection.Missing.Value;
string expath = openFileDialogExcel.FileName;
object exreadOnly = true;
var wb = excel.Workbooks.Open(expath);
var wks = wb.Worksheets[1];
//Begins Transfer
excel.Visible = false;
word.Visible = true;
Console.WriteLine("");
var rowCount = 0;
//Will Parse Out Values From Every Cell From Every Table And Compile It All Into One Table. Presumably A Copy Of The Last. Will Need Revisement
foreach (Word.Table tb in docs.Tables)
{
for (int row = 1; row <= tb.Rows.Count; row++)
{
rowCount++;
for (int col = 1; col <= tb.Columns.Count; col++)
{
try
{
var cell = tb.Cell(row, col);
var text = cell.Range.Text;
Console.WriteLine(text);
wks.Cells[rowCount, col].Value = cell;
}
catch (Exception)
{
continue;
}
// text now contains the content of the cell.
}
}
}
//MinimizeConsoleWindow(); Future Reference
excel.Visible = true;
docs.Close();
word.Quit();
wb.Close();
excel.Quit();
}
catch (Exception)
{
Console.WriteLine("ERROR");
}
}
public static void MinimizeConsoleWindow(bool visible)
{
//More Complicated Than It Seems
}
}
}