Click here to Skip to main content
14,694,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have MS word file and shown like below format,

#A#

#B#

#C#

#D#

#E#

I have implemented below code to replace text in MS word file using C#.

public void ReplaceWordDoc()
{
object missing = Missing.Value;
Document doc = Globals.ThisAddIn.Application.ActiveDocument;

List search = GetSearchList();

foreach (var searchItem in search)
{
foreach (Range tmpRange in ((Document)doc).StoryRanges)
{
tmpRange.Find.ClearFormatting();
tmpRange.Find.Text = searchItem.Find;
tmpRange.Find.Replacement.ClearFormatting();
tmpRange.Find.Replacement.Text = searchItem.Replace;
object replaceAll = WdReplace.wdReplaceAll;

if (searchItem.Replace.Contains("gif"))
{
tmpRange.InlineShapes.AddPicture(searchItem.Replace, Type.Missing, Type.Missing, tmpRange);
}
else
{
tmpRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
}
}
}

private List GetSearchList()
{
List ilst = new List();
for (int i = 0; i <= 4; i++)
{
ReplacerSearch ObjReplacerSearch = new ReplacerSearch();
if (i == 0)
{
ObjReplacerSearch.Find = "#A#";
ObjReplacerSearch.Replace = "ABCD";
}
else if (i == 1)
{
ObjReplacerSearch.Find = "#B#";
ObjReplacerSearch.Replace = "EFGH";
}
else if (i == 2)
{
ObjReplacerSearch.Find = "#C#";
ObjReplacerSearch.Replace = "IJKL";
}
else if (i == 3)
{
ObjReplacerSearch.Find = "#D#";
ObjReplacerSearch.Replace = "MVC Developer";
}
else if (i == 4)
{
ObjReplacerSearch.Find = "#E#";
ObjReplacerSearch.Replace = "D:\\ajax_loader.gif";
}

ilst.Add(ObjReplacerSearch);
}
return ilst;
}


My MS word out put is like,

[Image File] #ABCD#

#EFGH#

#IJKL#

#MVC Developer#

#E#

I need image at place of #E# not on the start.


Thanks,

Nirav
Posted

1 solution

I got my solution.

public void ReplaceWordDoc()
        {
            object missing = Missing.Value;
            Document doc = Globals.ThisAddIn.Application.ActiveDocument;

            List<replacersearch> search = GetSearchList();

            foreach (var searchItem in search)
            {
                foreach (Range tmpRange in ((Document)doc).StoryRanges)
                {
                    tmpRange.Find.ClearFormatting();
                    tmpRange.Find.Text = searchItem.Find;
                    tmpRange.Find.Replacement.ClearFormatting();
                    tmpRange.Find.Replacement.Text = searchItem.Replace;
                    if (searchItem.Replace.Contains("gif"))
                    {
                        tmpRange.Find.Replacement.Text = "";
                        Object end = doc.Characters.Count;
                        Range tmpRange1 = doc.Range(0, ref end);
                        Find fnd = tmpRange1.Find;
                        fnd.ClearFormatting();
                        fnd.Text = searchItem.Find;
                        fnd.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                        tmpRange1.InlineShapes.AddPicture(searchItem.Replace, Type.Missing, Type.Missing, Type.Missing);
                    }
                    tmpRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                  ref missing, ref missing, ref missing, WdReplace.wdReplaceAll, ref missing, ref missing, ref missing, ref missing);
                }
            }
            doc.Close(ref missing, ref missing, ref missing);
        }





        private List<replacersearch> GetSearchList()
        {
            List<replacersearch> ilst = new List<replacersearch>();
            for (int i = 0; i <= 4; i++)
            {
                ReplacerSearch ObjReplacerSearch = new ReplacerSearch();
                if (i == 0)
                {
                    ObjReplacerSearch.Find = "#A#";
                    ObjReplacerSearch.Replace = "ABCD";
                }
                else if (i == 1)
                {
                    ObjReplacerSearch.Find = "#B#";
                    ObjReplacerSearch.Replace = "EFGH";
                }
                else if (i == 2)
                {
                    ObjReplacerSearch.Find = "#C#";
                    ObjReplacerSearch.Replace = "IJKL";
                }
                else if (i == 3)
                {
                    ObjReplacerSearch.Find = "#D#";
                    ObjReplacerSearch.Replace = "Developer";
                }
                else if (i == 4)
                {
                    ObjReplacerSearch.Find = "#E#";
                    ObjReplacerSearch.Replace = "D:\\ajax_loader.gif";
                }

                ilst.Add(ObjReplacerSearch);
            }
            return ilst;
        }

Thanks,
Nirav
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900