如何使用C#在MS Word文本框中插入图片
问题描述:
我想要通过替换像< Image>这样的单词来插入文本框或矩形的图片用这张图片写在形状里面
I have a picture that I want to insert into text box or a rectangle by replacing a word like <Image> which written inside the shape with this picture
我搜索了很多,发现这段代码替换了文档中的所有文本,包括文本在文本框和形状中:
I have searched much and found this code that replace the all text in the document including the text in the text boxes and shapes:
public void FindandReplace(Word.Document doc, string Findtext, string ReplaceText)
{
Word.Range myStoryRange = doc.Range();
//First search the main document using the Selection
Word.Find myFind = myStoryRange.Find;
myFind.Text = Findtext;
myFind.Replacement.Text = ReplaceText;
myFind.Forward = true;
myFind.Wrap = Word.WdFindWrap.wdFindContinue;
myFind.Format = false;
myFind.MatchCase = false;
myFind.MatchWholeWord = false;
myFind.MatchWildcards = false;
myFind.MatchSoundsLike = false;
myFind.MatchAllWordForms = false;
myFind.Execute(Replace:Word.WdReplace.wdReplaceAll);
//'Now search all other stories using Ranges
foreach(Word.Range otherStoryRange in doc.StoryRanges)
{
if(otherStoryRange.StoryType != Word.WdStoryType.wdMainTextStory)
{
Word.Find myOtherFind = otherStoryRange.Find;
myOtherFind.Text = Findtext;
myOtherFind.Replacement.Text = ReplaceText;
myOtherFind.Wrap = Word.WdFindWrap.wdFindContinue;
myOtherFind.Execute(Replace:Word.WdReplace.wdReplaceAll);
}
// 'Now search all next stories of other stories (doc.storyRanges dont seem to cascades in sub story)
Word.Range nextStoryRange = otherStoryRange.NextStoryRange;
while(nextStoryRange != null)
{
Word.Find myNextStoryFind = nextStoryRange.Find;
myNextStoryFind.Text = Findtext;
myNextStoryFind.Replacement.Text = ReplaceText;
myNextStoryFind.Wrap = Word.WdFindWrap.wdFindContinue;
myNextStoryFind.Execute(Replace:Word.WdReplace.wdReplaceAll);
nextStoryRange = nextStoryRange.NextStoryRange;
}
}
}
我想修改此代码,以便能够替换我需要更换的文本有图片
I want to modify this code to be able to replace the text I need to replace with a picture
有什么建议吗?
提前谢谢