Я пытаюсь скопировать и вставить программным способом определенные поля из одного файла документа Word в другой.Файл для копирования был преобразован в слово из PDF-файла изображения.Он имеет следующий формат:
Журнал строительства
Шаблон слова для копирования имеет следующий формат:
Журнал контроля
Мой код может правильно скопировать, вставить данные для первой страницы журнала строительства в журнал контроля, но не для второй и третьей, так далее ...
Вот мой код:
using System;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
namespace LogConverter
{
class Program
{
static void Main()
{
try
{
var fileName = Application.StartupPath + @"\Construction Logs.docx";
var wordApp = new Word.Application();
wordApp.Visible = true;
var document = wordApp.Documents.Open(fileName);
string filePath = Application.StartupPath + @"\Supervision Logs.dotx";
Word.Application LogApp = new Word.Application();
Word.Document wordDoc = wordApp.Documents.Add(filePath);
object oMissing = System.Reflection.Missing.Value;
wordApp.Visible = true;
Word.Selection selection = wordApp.Selection;
Word.Range rangeDoc = wordDoc.Range();
int PageCnt = 1;
SearchPerPage(wordDoc, oMissing, selection, rangeDoc, document, PageCnt);
PageCnt++;
wordApp.Documents.Close();
LogApp.Documents.Close();
wordApp.Quit();
LogApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
Console.ReadKey();
}
}
static void SearchPerPage(Word.Document wordDoc, object oMissing, Word.Selection selection, Word.Range rangeDoc, Word.Document document, int pageCnt)
{
foreach (Word.Range storyRange in document.StoryRanges)
{
var range = storyRange;
// I have changed the while loop to loop till end of the document i.e last paragraph
while (range != null &&
range!=document.Paragraphs.Last)
{
//Get string between two strings in a string
String St = storyRange.Text;
if (!St.Contains("Date")) continue;
//Get Date portion of record
int DateFrom = St.IndexOf("Date:") + "Date:".Length;
int DateTo = St.IndexOf("Weather:");
string Date = St.Substring(DateFrom, DateTo - DateFrom);
//Get Temperature portion of record
int TempFrom = St.IndexOf("Temperature:") + "Temperature:".Length;
int TempTo = St.IndexOf("I .Construction");
string Temperature = St.Substring(TempFrom, TempTo - TempFrom);
//Get Construction work portion of record
int ConsFrom = St.IndexOf("Construction Work") + "Construction Work".Length;
int ConsTo = St.IndexOf("II. Construction");
string Construction = St.Substring(ConsFrom, ConsTo - ConsFrom);
//Get Construction machinery portion of record
int MachFrom = St.IndexOf("Construction Equipment and Machinery") + "Construction Equipment and Machinery".Length;
int MachTo = St.IndexOf("Construction Materials\r");
string Machinery = St.Substring(MachFrom, MachTo - MachFrom);
//Get Construction personnel portion of record
int PersFrom = St.IndexOf("Construction Personnel") + "Construction Personnel".Length;
int PersTo = St.IndexOf("Construction Equipment and Machinery");
string Personnel = St.Substring(PersFrom, PersTo - PersFrom);
//Get Quality inspection portion of record
int QCFrom = St.IndexOf("Quality Inspection") + "Quality Inspection".Length;
int QCTo = St.IndexOf("Constructional Safety");
string Quality = St.Substring(QCFrom, QCTo - QCFrom);
//Get Safety inspection portion of record
int SafeFrom = St.IndexOf("Constructional Safety") + "Constructional Safety".Length;
int SafeTo = St.IndexOf("and no phenomenon of improper operations.");
string Safety = St.Substring(SafeFrom, SafeTo - SafeFrom);
//Write the Supervision Log
LogWriter(wordDoc, oMissing, selection, rangeDoc, pageCnt, Date, Temperature, Construction, Machinery, Personnel, Quality, Safety);
//if (range.ShapeRange.Count > 0)
//{
// foreach (Word.Shape shape in range.ShapeRange)
// {
// if (shape.TextFrame.HasText != 0)
// {
// LogWriter(wordDoc, oMissing, selection, rangeDoc, pageCnt, Date, Temperature, Construction, Machinery, Personnel, Quality, Safety);
// }
// }
//}
range = range.NextStoryRange;
}
}
}
static void LogWriter(Word.Document wordDoc, object oMissing, Word.Selection selection, Word.Range rangeDoc, int pageCnt, string Date, string Temperature, string Construction, string Machinery, string Personnel, string Quality, string Safety)
{
//Copy the template page
wordDoc.Bookmarks[@"\Page"].Range.Copy();
//inserting a page break: first go to end of document
selection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove);
//insert a page break
object breakType = Word.WdBreakType.wdPageBreak;
selection.InsertBreak(ref breakType);
//Replace the text in the correct fields.
rangeDoc.Find.Execute(FindText: "{{Date}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Date);
rangeDoc.Find.Execute(FindText: "{{TemperatureRecord}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Temperature.Replace("\r",""));
rangeDoc.Find.Execute(FindText: "{{ConstructionWork}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Construction.Replace("\r", ""));
rangeDoc.Find.Execute(FindText: "{{ConstructionMachinery}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Machinery.Replace("\r", ""));
rangeDoc.Find.Execute(FindText: "{{ConstructionPersonnel}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Personnel.Replace("\r", ""));
//rangeDoc.Find.Execute(FindText: "{{QualityInspection}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Quality.Replace("\r", ""));
rangeDoc.Find.Execute(FindText: "{{SafetyInspection}}", Replace: Word.WdReplace.wdReplaceAll, ReplaceWith: Safety.Replace("\r", ""));
rangeDoc.Find.Execute("{{QualityInspection}}");
rangeDoc.Text = Quality.Replace("\r", "");
//Paste the template page onto a new one
selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting);
}
}
}
Может ли кто-нибудь помочь с исправлением, чтобы прочитать другие страницы журнала строительства и правильно вставить их?