Visual Studio 2012, Windows 7. В программе
Код:
using System;
using System.Configuration;
using Word = Microsoft.Office.Interop.Word;
using System.Collections.Generic;
namespace CsWordCons
{
class Program
{
static void FindString()
{
List<string> strArrFind = new List<string> {"111111", "222222", "33333"};
Object missing = System.Reflection.Missing.Value;
Object confConv = false;
Object readOnly = true;
Object isVisible = false;
Object saveChanges = false;
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
word.Visible = false;
Word.Table tbl = null;
Object filename = Environment.CurrentDirectory + "\\Документ 1.docx";
Console.WriteLine(filename);
doc = word.Documents.Open(ref filename, ref confConv, ref readOnly);
try
{
tbl = doc.Tables[1];
}
catch (Exception ex)
{
tbl = null;
Console.WriteLine("\nВ документе " + filename.ToString() + " нет таблиц!\n");
Console.ReadKey();
Environment.Exit(1);
}
int numCols = tbl.Columns.Count, numRows = tbl.Rows.Count;
foreach (string strFind in strArrFind)
{
Console.WriteLine("************* Строка для поиска: {0} *************", strFind);
for (int i = 2; i <= numRows; i++)
{
for (int j = 3; j <= numCols; j++)
{
Console.WriteLine(tbl.Cell(i, j).Range.Text);
Console.WriteLine(tbl.Cell(i, j).Range.Text + " " + strFind);
if (tbl.Cell(i, j).Range.Text == strFind)
{
Console.WriteLine(strFind + " " + i.ToString() + ", " + j.ToString());
}
Console.WriteLine("---------------------------------------");
} // for j
} // for i
} // foreach
((Word._Document)doc).Close(ref saveChanges, missing, missing);
((Word._Application)word).Quit(ref saveChanges);
} // FindString()
static void Main(string[] args)
{
FindString();
Console.ReadKey();
} // Main
} //class Program
} // namespace CsWordCons
возникают следующие ошибки:
1. Вывод в консоль Console.WriteLine(tbl.Cell(i, j).Range.Text + " " + strFind); должен, как я понимаю, вывести текст из ячейки таблицы, затем пробелы, затем поисковую строку. На самом деле пропадает строка tbl.Cell(i, j).Range.Text и выводится только сдвинутая поисковая строка.
2. Условие tbl.Cell(i, j).Range.Text == strFind никогда не выполняется (при установке в теле условия точки прерывания захода в тело условия нет).
Помогите, пожалуйста, исправить ошибки. Проект прикрепил.