以下の方法でこれを行うことができます。このコードは、テキストを正確な位置に置き換えます。
Word.Application wordApp = new Word.Application();
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref outputFile, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
try
{
//----------------------Replace--------------------------------
Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find;
fnd.ClearFormatting();
fnd.Replacement.ClearFormatting();
fnd.Forward = true;
fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
string imagePath = Server.MapPath("~/data/uploads/violations/resize/Image.jpg");
var keyword = "LOGO";
var sel = wordApp.Selection;
sel.Find.Text = string.Format("[{0}]", keyword);
wordApp.Selection.Find.Execute(keyword);
Word.Range range = wordApp.Selection.Range;
if (range.Text.Contains(keyword))
{
//gets desired range here it gets last character to make superscript in range
Word.Range temprange = doc.Range(range.End - 4, range.End);//keyword is of 4 charecter range.End - 4
temprange.Select();
Word.Selection currentSelection = wordApp.Selection;
//currentSelection.Font.Superscript = 1;
sel.Find.Execute(Replace: WdReplace.wdReplaceOne);
sel.Range.Select();
var imagePath1 = Path.GetFullPath(string.Format(imagePath, keyword));
sel.InlineShapes.AddPicture(FileName: imagePath1, LinkToFile: false, SaveWithDocument: true);
}
}
catch { }
finally
{
if (doc != null)
{
((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing);
Marshal.FinalReleaseComObject(doc);
}
if (wordApp != null)
{
((_Application)wordApp).Quit();
Marshal.FinalReleaseComObject(wordApp);
}
}
おかげで上の画像にテキストを置き換えます。それは私のループの場合のように見える、Rangeは実際には文書のテキストの非常に大きなチャンクです - 範囲に画像を追加するだけですべてのテキストの前に置いている - 私は必要と思います範囲を指定することですどういうわけか? – Stian
AddPictureを返すとShapeが返されます。この[link]を参照してください(http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shape%28v=office.11%29.aspx )、 'RelativeHorizontalPosition'と 'RelativeVerticalPosition'プロパティの説明を読んでください –