0

我正在研究Word AddIns和使用Interop libraryof 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;

    } 
4

1 回答 1

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;
} 

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

于 2013-09-12T06:18:14.417 回答