直接上代码
/// <summary>
/// 通过计算插入引文的位置格式化合适的引文序号
/// </summary>
/// <returns></returns>
public static int FormatBookMarkNoByPosition(Microsoft.Office.Interop.Word.Application WordApp)
{
try
{
int _nStart = WordApp.Selection.Start;
int _nEnd = WordApp.Selection.End;
if (_nStart != _nEnd)
{
return -99999;
}
ArrayList _listMarks = new ArrayList();
foreach (Word.Bookmark item in WordApp.ActiveDocument.Bookmarks)
{
if (item.Name.StartsWith("BIMT"))
{
_listMarks.Add(item);
}
}
Word.Bookmark _markTemp = null;
for (int i = 0; i < _listMarks.Count; i++)
{
for (int j = i; j < _listMarks.Count; j++)
{
int _nStartA = ((Word.Bookmark)_listMarks[i]).Start;
int _nStartB = ((Word.Bookmark)_listMarks[j]).Start;
if (_nStartA > _nStartB)
{
_markTemp = ((Word.Bookmark)_listMarks[i]);
_listMarks[i] = _listMarks[j];
_listMarks[j] = _markTemp;
}
}
}
int _nIndex = 1;
foreach (Word.Bookmark item in _listMarks)
{
string _strIndex = string.Format("[{0}]", _nIndex);
string _strNmae = item.Name;
Word.Range _rangeTemp = WordApp.ActiveDocument.Range(item.Start, item.End);
_rangeTemp.Text = _strIndex;
//item.Range.Text = _strIndex;
Word.Bookmark mark = WordApp.ActiveDocument.Bookmarks.Add(_strNmae, _rangeTemp);
_nIndex++;
}
object o = WordApp.ActiveDocument.Bookmarks;
}
catch (Exception ex)
{
throw;
}
return 0;
}