word.shape.name抛出异常

时间:2013-09-12 05:07:33

标签: c# .net ms-word interop

我正在使用Word AddIns并使用Interop library Word,我希望为Shape指定一个名称(字符串),但是word.shape.name会抛出异常:

  

“System.UnauthorizedAccessException:拒绝访问。(HRESULT异常:0x80070005(E_ACCESSDENIED))”。

我的代码是:

        bool blnRetVal = false;
        string strModel = ndModel.Name;
        int lngModel = 0;
        int lngNextModel = 0;
        int lngStart;
        int lngEnd;
        Alias.Document objTemp;
      Microsoft.Office.Interop.Word.Range objRange;
        Shape objShape;
        Object[] astr;
        int n;
        bool bLastModel;
        /*'--------------------------------------------------------------------------------------------------
        '   1. Find model's model marker and the next marker (if any)
        '--------------------------------------------------------------------------------------------------*/
        astr = dicMarkers.Keys();
        for (n = astr.GetLowerBound(0); n <= astr.GetUpperBound(0); n++)
        {
            if (string.Compare(astr[n].ToString(), strModel, true) == 0)
            {
                lngModel = (int)dicMarkers.get_Item(astr[n]);   //PNDC  //dicMarkers.Item(astr(n))
                if (n < astr.GetUpperBound(0))
                {
                    if (string.Compare(astr[n + 1].ToString(), "#end", true) == 0)
                    {
                        lngNextModel = 0;
                        bLastModel = true;
                    }
                    else
                    {
                        lngNextModel = (int)dicMarkers.get_Item(astr[n + 1]);
                        bLastModel = false;
                    }
                }
                else
                {
                    lngNextModel = 0;
                }
                break;
            }
        }
        /*'--------------------------------------------------------------------------------------------------
        '   2. Copy model from original document to new document
        '--------------------------------------------------------------------------------------------------*/
        if (lngModel > 0)
        {
            lngStart = objSourceDocument.Sections[lngModel].Range.Start;
            if (lngNextModel == 0)
            {
                var key = "#end";
                var value = dicMarkers.get_Item(key);
                lngEnd = value;
            }
            else
                lngEnd = objSourceDocument.Sections[lngNextModel].Range.Start; //objSourceDocument.Sections.Last.Index;

            //--------------------------------------------------------------------------------------------------
            //copy original
            objSourceDocument.ActiveWindow.Selection.SetRange(lngStart, lngEnd);
            objSourceDocument.ActiveWindow.Selection.Copy();
            bool bInsertSection = false;
            //paste (append) copied model to the document
            if (objTargetDocument.Sections.First.Index == objTargetDocument.Sections.Last.Index)
            {
                //Target document only has 1 (default) section
                bInsertSection = true;
            }
            else
            {
                if (objTargetDocument.Sections.Last.PageSetup.SectionStart == WdSectionStart.wdSectionNewPage)
                {
                    //Last section is a nextpage section
                    if ((objTargetDocument.Sections.Last.Range.End - (objTargetDocument.Sections.Last.Range.Start) <= 1))
                        //Empty section
                        bInsertSection = false;
                    else
                        bInsertSection = true;
                }
                else
                {
                    //Last section isn't a nextpage
                    bInsertSection = true;
                }

            }

            objTargetDocument.ActiveWindow.Selection.Start = objTargetDocument.Range().End;
            if (bInsertSection)
            {
                objTargetDocument.ActiveWindow.Selection.InsertBreak(WdBreakType.wdSectionBreakNextPage);
                objTargetDocument.ActiveWindow.Selection.Start = objTargetDocument.Range().End;
            }

            objTargetDocument.ActiveWindow.Selection.Collapse();
            objRange = objTargetDocument.ActiveWindow.Selection.Range.Duplicate; //remember range for model marker anchor
            objTargetDocument.ActiveWindow.Selection.Paste();

            //place model marker (so that we can find our model again)
            objShape = objTargetDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationUpward, 0, 0, 0, 0, objRange);

            objShape.Name = m_strModelMarker + strModel;
            objShape.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

            UpdateFields(ref objTargetDocument, ref ndModel);
            blnRetVal = true;
        }
        else
            new Modules.Globals().MsgBoxEx("Kan het bestaande model '" + strModel + "' niet kopieren.", MessageBoxButtons.OK);
        return blnRetVal;

    } 

1 个答案:

答案 0 :(得分:1)

我无法复制您的错误,但是我可以通过一些更改来运行您的代码。如果我误解了,请告诉我。

请注意使用的Missing变量代替objRange。我还删除了不适用的变量。

using Word = Microsoft.Office.Interop.Word;
using Alias = Microsoft.Office.Interop.Word;

public Test()
{
    var doc = new Alias.Document();
    var doc2 = new Alias.Document();
    var t = this.CloneModel(ref doc, ref doc2);
}
private bool CloneModel(ref Alias.Document objTargetDocument, ref Alias.Document objSourceDocument)
{
    var missing = Type.Missing;
    Word.Shape objShape;
    objShape = objTargetDocument.Shapes.AddTextbox(
        Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationUpward, 0, 0, 0, 0, ref missing); <== note the missing here instead of objRange

    objShape.Name = "Carma DocSys~Brief"; // no longer throwing exceptions (hard coded string)
    objShape.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
    return true;
} 

如果这不是问题,那么我建议您在错误消息建议时遇到文件权限问题。