Je recherche d'autres moyens de rechercher dans le document Word certains textes, puis de les remplacer par un autre texte. Mes travaux actuels utilisant la méthode de recherche et remplacement cependant je me demandais s'il y a d'autres manières de faire ceci. Une méthode que j'ai essayée était de passer paragraphe par paragraphe et de chercher que pour le texte, l'échanger, puis le coller dans un nouveau document de mot et l'enregistrer. Cependant, cela rendait les choses beaucoup plus compliquées en ce qui concerne les images, les manuels, les tableaux, etc. La mise en forme n'a pas été conservée, ce qui était un autre problème.C# Autre méthode de recherche et de remplacement de texte dans Word Document
Mon code actuel est le suivant:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace Test
{
static class Program
{
static void Main()
{
//Create a new microsoft word file
Microsoft.Office.Interop.Word.Application fileOpen = new Microsoft.Office.Interop.Word.Application();
//Open a already existing word file into the new document created
Microsoft.Office.Interop.Word.Document document = fileOpen.Documents.Open(@"C:\Users\dpatel\Desktop\Test1.docx", ReadOnly: false);
//Make the file visible
fileOpen.Visible = true;
document.Activate();
//The FindAndReplace takes the text to find under any formatting and replaces it with the
//new text with the same exact formmating (e.g red bold text will be replaced with red bold text)
FindAndReplace(fileOpen, "useless", "very useful");
//Save the editted file in a specified location
//Can use SaveAs instead of SaveAs2 and just give it a name to have it saved by default
//to the documents folder
document.SaveAs2(@"C:\Users\dpatel\Desktop\NewFile1");
//Close the file out
fileOpen.Quit();
}
//Method to find and replace the text in the word document. Replaces all instances of it
static void FindAndReplace(Microsoft.Office.Interop.Word.Application fileOpen, object findText, object replaceWithText)
{
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
//execute find and replace
fileOpen.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
}
}
Pourquoi recherchez-vous une alternative à la méthode intégrée? – stuartd
C'est pour un programme que j'écris. Je veux juste connaître les différents choix avec lesquels je dois travailler. Trouver et remplacer la méthode intégrée fonctionne mais n'est pas la meilleure solution pour mon cas. – Dylan