问题是这样的,事先在WORD中定义好书签,共四十二项,之后在我在程序里取出数据分别插入WORD的书签中。目前的情况是插入到第十项的时候,提示该书签不存在,
而在WORD中确实有这个书签,之前一段时间是好用的。现在实在无解了,希望大家能给点建议,遇到过这种情况的高手们还请赐教。C# 插入标签部分代码:public void SetTagText(string sTagName,string sText)
{
   object oTagName = (object)sTagName; //标签名称
    object oTextLen = System.Text.Encoding.Default.GetByteCount(sText); //文本长度(汉字以2个字符计算)    //插入文本数据
    try
    {
oWordApp.Selection.GoTo(ref oWhat,ref missing,ref missing,ref oTagName).Text = sText;
    }
    catch
    {
throw new Exception("指定的标签不存在,文本插入失败!");
    }
    
    //退格处理
    oWordApp.Selection.MoveRight(ref oUnit,ref oTextLen,ref missing);
    oWordApp.Selection.Delete(ref missing,ref oTextLen);
}

解决方案 »

  1.   

    参考这个:
    http://msdn.microsoft.com/zh-cn/library/ms178792(VS.80).aspx
      

  2.   

    还有这个:
    http://msdn.microsoft.com/zh-cn/library/6b9478cs(VS.80).aspx
      

  3.   

    C#封装Word常用操作类 
    //2008-6-4  
      using System; 
        using System.Collections.Generic; 
        using System.Text; 
        using Microsoft.Office.Interop.Word; 
        using System.Diagnostics; 
        namespace OfficeManager 
        { 
            public class WordClass : IDisposable 
            { 
                #region 字段 
                private _Application m_WordApp = null; 
                private _Document m_Document = null; 
                private object missing = System.Reflection.Missing.Value; 
                #endregion 
                #region 构造函数与析构函数 
                public WordClass() 
                { 
                    m_WordApp = new ApplicationClass(); 
                } 
                ~WordClass() 
                { 
                    try 
                    { 
                        if (m_WordApp != null) 
                            m_WordApp.Quit(ref missing, ref missing, ref missing); 
                    } 
                    catch (Exception ex) 
                    { 
                        Debug.Write(ex.ToString()); 
                    } 
                } 
                #endregion 
                #region 属性 
                public _Document Document 
                { 
                    get 
                    { 
                        return m_Document; 
                    } 
                } 
                public _Application WordApplication 
                { 
                    get 
                    { 
                        return m_WordApp; 
                    } 
                } 
                public int WordCount 
                { 
                    get 
                    { 
                        if (m_Document != null) 
                        { 
                            Range rng = m_Document.Content; 
                            rng.Select(); 
                            return m_Document.Characters.Count; 
                        } 
                        else 
                            return -1; 
                    } 
                } 
                public object Missing 
                { 
                    get 
                    { 
                        return missing; 
                    } 
                } 
                #endregion 
                #region 基本任务 
                #region CreateDocument 
                public void CreateDocument(string template) 
                { 
                    object obj_template = template; 
                    if (template.Length <= 0) obj_template = missing; 
                    m_Document = m_WordApp.Documents.Add(ref obj_template, ref missing, ref missing, ref missing); 
                } 
                public void CreateDocument() 
                { 
                    this.CreateDocument(""); 
                } 
                #endregion 
                #region OpenDocument 
                public void OpenDocument(string fileName, bool readOnly) 
                { 
                    object obj_FileName = fileName; 
                    object obj_ReadOnly = readOnly; 
                    m_Document = m_WordApp.Documents.Open(ref obj_FileName, ref missing, ref obj_ReadOnly, 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); 
                } 
                public void OpenDocument(string fileName) 
                { 
                    this.OpenDocument(fileName, false); 
                } 
                #endregion 
                #region Save & SaveAs 
                public void Save() 
                { 
                    if (m_Document != null) 
                        m_Document.Save(); 
                } 
                public void SaveAs(string fileName) 
                { 
                    object obj_FileName = fileName; 
                    if (m_Document != null) 
                    { 
                        m_Document.SaveAs(ref obj_FileName, 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); 
                    } 
                } 
                #endregion 
                #region Close 
                public void Close(bool isSaveChanges) 
                { 
                    object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
                    if (isSaveChanges) 
                        saveChanges = WdSaveOptions.wdSaveChanges; 
                    if (m_Document != null) 
                        m_Document.Close(ref saveChanges, ref missing, ref missing); 
                } 
                #endregion 
                #region 添加数据 
                /// <summary> 
                /// 添加图片 
                /// </summary> 
                /// <param name="picName"></param> 
                public void AddPicture(string picName) 
                { 
                    if (m_WordApp != null) 
                        m_WordApp.Selection.InlineShapes.AddPicture(picName, ref missing, ref missing, ref missing); 
                } 
                /// <summary> 
                /// 插入页眉 
                /// </summary> 
                /// <param name="text"></param> 
                /// <param name="align"></param> 
                public void SetHeader(string text, WdParagraphAlignment align) 
                { 
                    this.m_WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 
                    this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; 
                    this.m_WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(text); //插入文本 
                    this.m_WordApp.Selection.ParagraphFormat.Alignment = align;  //设置对齐方式 
                    this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; // 跳出页眉设置 
                } 
      

  4.   

                /// <summary> 
                /// 插入页脚 
                /// </summary> 
                /// <param name="text"></param> 
                /// <param name="align"></param> 
                public void SetFooter(string text, WdParagraphAlignment align) 
                { 
                    this.m_WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 
                    this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter; 
                    this.m_WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(text); //插入文本 
                    this.m_WordApp.Selection.ParagraphFormat.Alignment = align;  //设置对齐方式 
                    this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; // 跳出页眉设置 
                } 
                #endregion 
                #region Print 
        public void PrintOut() 
                { 
                    object copies = "1"; 
                    object pages = ""; 
                    object range = WdPrintOutRange.wdPrintAllDocument; 
                    object items = WdPrintOutItem.wdPrintDocumentContent; 
                    object pageType = WdPrintOutPages.wdPrintAllPages; 
                    object oTrue = true; 
                    object oFalse = false; 
                    this.m_Document.PrintOut( 
                        ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing, 
                        ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue, 
                        ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing); 
                } 
                public void PrintPreview() 
                { 
                    if (m_Document != null) 
                        m_Document.PrintPreview(); 
                } 
                #endregion 
                public void Paste() 
                { 
                    try 
                    { 
                        if (m_Document != null) 
                        { 
                            m_Document.ActiveWindow.Selection.Paste(); 
                        } 
                    } 
                    catch (Exception ex) 
                    { 
                        Debug.Write(ex.Message); 
                    } 
                } 
                #endregion 
                #region 文档中的文本和对象 
                public void AppendText(string text) 
                { 
                    Selection currentSelection = this.m_WordApp.Selection; 
                    // Store the user's current Overtype selection 
                    bool userOvertype = this.m_WordApp.Options.Overtype; 
                    // Make sure Overtype is turned off. 
                    if (this.m_WordApp.Options.Overtype) 
                    {this.m_WordApp.Options.Overtype = false; 
                    } 
                    // Test to see if selection is an insertion point. 
                    if (currentSelection.Type == WdSelectionType.wdSelectionIP) 
                    { 
                        currentSelection.TypeText(text); 
                        currentSelection.TypeParagraph(); 
                    } 
                    else 
                        if (currentSelection.Type == WdSelectionType.wdSelectionNormal) 
                        { 
                            // Move to start of selection. 
                            if (this.m_WordApp.Options.ReplaceSelection) 
                            { 
                                object direction = WdCollapseDirection.wdCollapseStart; 
                                currentSelection.Collapse(ref direction); 
                            } 
                            currentSelection.TypeText(text); 
                            currentSelection.TypeParagraph(); 
                        } 
                        else 
                        { 
                            // Do nothing. 
                        } 
                    // Restore the user's Overtype selection 
                    this.m_WordApp.Options.Overtype = userOvertype; 
                } 
                #endregion 
                #region 搜索和替换文档中的文本 
                public void Replace(string oriText, string replaceText) 
                { 
                    object replaceAll = WdReplace.wdReplaceAll; 
                    this.m_WordApp.Selection.Find.ClearFormatting(); 
                    this.m_WordApp.Selection.Find.Text = oriText; 
                    this.m_WordApp.Selection.Find.Replacement.ClearFormatting(); 
                    this.m_WordApp.Selection.Find.Replacement.Text = replaceText; 
                    this.m_WordApp.Selection.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); 
                }             #endregion 
                #region 文档中的Word表格 
                /// <summary> 
                /// 向文档中插入表格 
                /// </summary> 
                /// <param name="startIndex">开始位置</param> 
                /// <param name="endIndex">结束位置</param> 
                /// <param name="rowCount">行数</param> 
                /// <param name="columnCount">列数</param> 
                /// <returns></returns> 
                public Table AppendTable(int startIndex, int endIndex, int rowCount, int columnCount) 
                { 
                    object start = startIndex; 
                    object end = endIndex; 
                    Range tableLocation = this.m_Document.Range(ref start, ref end); 
                    return this.m_Document.Tables.Add(tableLocation, rowCount, columnCount, ref missing, ref missing); 
                } 
                /// <summary> 
                /// 添加行 
                /// </summary> 
                /// <param name="table"></param> 
                /// <returns></returns> 
                public Row AppendRow(Table table) 
                { 
                    object row = table.Rows[1]; 
                    return table.Rows.Add(ref row); 
                } 
                /// <summary> 
                /// 添加列 
                /// </summary> 
                /// <param name="table"></param> 
                /// <returns></returns> 
                public Column AppendColumn(Table table) 
                { 
                    object column = table.Columns[1]; 
                    return table.Columns.Add(ref column); 
                } 
                /// <summary> 
                /// 设置单元格的文本和对齐方式 
                /// </summary> 
                /// <param name="cell">单元格</param> 
                /// <param name="text">文本</param> 
                /// <param name="align">对齐方式</param> 
                public void SetCellText(Cell cell, string text, WdParagraphAlignment align) 
                { 
                    cell.Range.Text = text; 
                    cell.Range.ParagraphFormat.Alignment = align; 
                } 
                #endregion 
                #region IDisposable 成员 
                public void Dispose() 
                { 
                    try 
                    { 
                        if (m_WordApp != null) 
                            m_WordApp.Quit(ref missing, ref missing, ref missing); 
                    } 
                    catch (Exception ex) 
                    { 
                        Debug.Write(ex.ToString()); 
                    } 
                } 
                #endregion 
            } 
        }
      

  5.   

    谢谢你的热情,能不能针对问题回答一样,我贴出来的代码没有问题,因为在别的页面调用中没有出错,我之前怀疑是WORD模板的问题,但是报错的那个书签也确实存在,而且标签名称前面也没有空格。