Microsoft.Office.Interop.Word.Application replace with list<string>


S

seb

hi i have some file MSWORD
and i have list<string> in code
i want to replace some one word ex."example" in MSWORD to this
list<string> and every record from list<string> paste in new line in msword

how?
 
Ad

Advertisements

Joined
Jun 25, 2005
Messages
65
Reaction score
0
Create a c# forms application in visual studio. To reference add
Microsoft.Office.Interop.Word then put the following code in Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MSWordReplace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


//https://www.wwco.com/~wls/blog/2010/07/03/find-and-replace-in-word-using-c-net/
private static void
searchAndReplaceInStory(Microsoft.Office.Interop.Word.Range rngStory,
string strSearch, string strReplace) // Helper
{
object missing = System.Reflection.Missing.Value;
rngStory.Find.ClearFormatting();
rngStory.Find.Replacement.ClearFormatting();
rngStory.Find.Text = strSearch;
rngStory.Find.Replacement.Text = strReplace;
rngStory.Find.Wrap =
Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object arg1 = missing; // Find Pattern
object arg2 = missing; //MatchCase
object arg3 = missing; //MatchWholeWord
object arg4 = missing; //MatchWildcards
object arg5 = missing; //MatchSoundsLike
object arg6 = missing; //MatchAllWordForms
object arg7 = missing; //Forward
object arg8 = missing; //Wrap
object arg9 = missing; //Format
object arg10 = missing; //ReplaceWith
object arg11 =
Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; //Replace
object arg12 = missing; //MatchKashida
object arg13 = missing; //MatchDiacritics
object arg14 = missing; //MatchAlefHamza
object arg15 = missing; //MatchControl
rngStory.Find.Execute(ref arg1, ref arg2, ref arg3, ref
arg4, ref arg5, ref arg6, ref arg7, ref arg8, ref arg9, ref arg10, ref
arg11, ref arg12, ref arg13, ref arg14, ref arg15);
}
// Main routine to find text and replace it,
// var app = new Microsoft.Office.Interop.Word.Application();
public static void
FindReplaceAnywhere(Microsoft.Office.Interop.Word.Application app,
string findText, string replaceText)
{
// http://forums.asp.net/p/1501791/3739871.aspx
var doc = app.ActiveDocument;
// Fix the skipped blank Header/Footer problem
//
http://msdn.microsoft.com/en-us/library/aa211923(office.11).aspx
Microsoft.Office.Interop.Word.WdStoryType lngJunk =
doc.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.StoryType;
// Iterate through all story types in the current document
foreach (Microsoft.Office.Interop.Word.Range rngStory in
doc.StoryRanges)
{
// Iterate through all linked stories
var internalRangeStory = rngStory;
do
{
searchAndReplaceInStory(internalRangeStory,
findText, replaceText);
try
{
// 6 , 7 , 8 , 9 , 10 , 11 --
http://msdn.microsoft.com/en-us/library/aa211923(office.11).aspx
switch (internalRangeStory.StoryType)
{
case
Microsoft.Office.Interop.Word.WdStoryType.wdEvenPagesHeaderStory: // 6
case
Microsoft.Office.Interop.Word.WdStoryType.wdPrimaryHeaderStory: // 7
case
Microsoft.Office.Interop.Word.WdStoryType.wdEvenPagesFooterStory: // 8
case
Microsoft.Office.Interop.Word.WdStoryType.wdPrimaryFooterStory: // 9
case
Microsoft.Office.Interop.Word.WdStoryType.wdFirstPageHeaderStory: // 10
case
Microsoft.Office.Interop.Word.WdStoryType.wdFirstPageFooterStory: // 11
if (internalRangeStory.ShapeRange.Count
> 0)

{
foreach
(Microsoft.Office.Interop.Word.Shape oShp in internalRangeStory.ShapeRange)
{
if (oShp.TextFrame.HasText != 0)
{

searchAndReplaceInStory(oShp.TextFrame.TextRange, findText, replaceText);
}
}
}
break;
default:
break;
}
} catch {
// On Error Resume Next
}
// ON ERROR GOTO 0 --
http://www.harding.edu/fmccown/vbnet_csharp_comparison.html
// Get next linked story (if any)
internalRangeStory = internalRangeStory.NextStoryRange;
} while (internalRangeStory != null); //
http://www.harding.edu/fmccown/vbnet_csharp_comparison.html
}
}

private void button1_Click(object sender, EventArgs e)
{
object missing = System.Reflection.Missing.Value;
object readOnly = true;
object isVisible = true;
object fileName = Application.StartupPath +
"\\MyDocument.docx";
object newFileName = Application.StartupPath +
"\\MyDocument-replaced.docx";
var applicationWord = new
Microsoft.Office.Interop.Word.Application();
applicationWord.Visible = true;
Microsoft.Office.Interop.Word.Document myDoc = new
Microsoft.Office.Interop.Word.Document();
try
{
myDoc = applicationWord.Documents.Open(ref fileName,
ref missing, ref readOnly, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref
isVisible, ref missing, ref missing, ref missing, ref missing);
myDoc.Activate();
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.Write(ex);
myDoc.Application.Quit(ref missing, ref missing, ref
missing);

}
string find_text="test";
List<string> replaceList = new List<string> {"hi","there"};
string replace_text = "";
foreach (string listItem in replaceList) {
replace_text += listItem + " ";
}
replace_text = replace_text.Trim();
FindReplaceAnywhere(applicationWord, find_text, replace_text);
myDoc.SaveAs(newFileName, missing, missing, missing,
missing, missing, missing, missing, missing, missing);
myDoc.Application.Quit(ref missing, ref missing, ref missing);
}
}
}

On 21/09/2014 4:38 AM, seb wrote:
> hi i have some file MSWORD
> and i have list<string> in code
> i want to replace some one word ex."example" in MSWORD to this
> list<string> and every record from list<string> paste in new line in msword
>
> how?
 
Ad

Advertisements

Joined
Jun 25, 2005
Messages
65
Reaction score
0
Actually my code won't put it on a new line. You need to put

Environment.NewLine

instead of " "

On 21/09/2014 4:38 AM, seb wrote:
> hi i have some file MSWORD
> and i have list<string> in code
> i want to replace some one word ex."example" in MSWORD to this
> list<string> and every record from list<string> paste in new line in msword
>
> how?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top