0

MS word 文件如下所示

#A# #B# #C# #D# #E#

我已经实现了下面的代码来使用 C# 替换 MS word 文件中的文本。

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;
                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<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 = "MVC Developer";
                }
                else if (i == 4)
                {
                    ObjReplacerSearch.Find = "#E#";
                    ObjReplacerSearch.Replace = "D:\\ajax_loader.gif";
                }

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

我的 MS 字是这样的,

[图片]#ABCD##EFGH##IJKL##MVC 开发人员##E#

我需要在#E# 的位置而不是开始时的图像。

4

1 回答 1

1

我得到了我的解决方案。

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;
        }
于 2013-05-10T10:48:37.057 回答