SOLVED

Tiny Life Plaguing Boxes

Copper Contributor

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 Microsoft Excel has taken to leaving small rectangular looking characters in every cell that had data inputted. These characters do disappear once I attempt to edit the cell but if the user has to F2 every cell to rid themselves of these tiny life plaguing boxes it makes my work practically useless as it aims to increase the efficiency of the worker and not have copy/paste functions be simpler. I haven't been able to find any other reference to my little boxes and have inserted a small screenshot for any clarification.

 

Please someone help get rid of these forsaken boxes.

 

Hopefully this issue can be resolved sooner rather than later. Or I might have to ditch the project entirely.

 

Osten24

5 Replies

@Osten24 

I'm uncertain to whether the screenshot actually managed to make it into the post so in case I have another jpeg attached here.Excel Issue.jpg

@Osten24 

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
        }
    }
}

@Osten24 

Can't explain why the "boxes" appear, but can perhaps help get rid of them. Copy the "box" from within a cell. Select all cells. Press Ctrl- H (Find and replace). Paste the the "box" symbol in the "Find" field and leave the "Replace with" blank. Press "Replace all", OK and Close. This should remove all the "box" symbols in the entire sheet in one go.

@Riny_van_Eekelen 

Oh you almost made my day. Small issue though. As the box disappears once the cell begins edit, I can't quite only snag the box. And if I copy/paste the entire cell the box also decides to go. There is a development though, I believe this may be an issue of my script inserting the values in as var rather string or int. Possibly more a c# issue rather excel.

 

EDIT: Possible additional info. When cell is copied and pasted into outside source it is represented by quotations rather the evil rectangle. Another tidbit is my venture into variable types did not work either. 

best response confirmed by Osten24 (Copper Contributor)
Solution

@Osten24 

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.

1 best response

Accepted Solutions
best response confirmed by Osten24 (Copper Contributor)
Solution

@Osten24 

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.

View solution in original post