0

我正在使用 Word.Interop 库。如果我为“Word.Shape.Name”分配的字符少于 18 个,那么它可以完美运行,但是当我分配的字符超过 18 个时,“Word.Shape.Name”会抛出异常。例如

Word.Shape.Name = "This is a test value to assign";

抛出异常

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

我应该怎么做才能解决这个问题?整个方法是

 objTargetDocument, ref Scripting.Dictionary dicMarkers, ref Alias.Document objSourceDocument)
    {
        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();
            objTargetDocument.Variables.Add(m_strModelMarker + strModel);


            // .TextFrame.ContainingRange

            //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; // This Line Trowing Exception
            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

2 回答 2

1

我无法在该Name属性中找到任何关于 18 个字符限制的参考。此外,经过快速测试,它似乎工作正常(即使输入更长的长度):

Word.Shape oShape = oDoc.Shapes.AddLine(0, 0, 0, 0);
oShape.Name = "This is a test value to assign - This is a test value to assign";

因此,您提到的错误很可能是由代码的不同部分引起的。

无论如何,我不确定您是否理解该Name属性的确切含义。这根本不是您在 Word 文档中看到的内容。这是出于“内部参考目的”的东西。该Shapes数组实际上是一个Dictionary可以通过键入给定形状的索引或名称来访问的;也就是说,如果oShape如上所述,是文档中的第一个形状,我可以使用以下任何选项访问它:

Word.Shape oShape2 = oDoc.Shapes[1];
Word.Shape oShape2 = oDoc.Shapes["This is a test value to assign - This is a test value to assign"];

因此,无论如何都不需要写太长的名称。

于 2013-09-12T11:15:51.813 回答
1

这里有一个关于 Word 2003 的旧讨论,似乎很相似。

在该讨论中,它建议将名称存储在文档变量对象中(如 中Document.Variables)。

就像是:

Variables vars = Doc.Variables;
var = vars.Add("myShapeName", "This is a test value to assign");

然后以某种方式分配var.Value给。Word.Shape.Name

编辑

在重新阅读该链接讨论时,无法直接将 a 分配Doc.Variable给该Name属性。您可以将形状设置Name为一个简短的唯一标识符,并使用它从Variables您需要的任何地方检索您的长字符串。

void SetLongName(Shape shape, string uniqueId, string longName)
{
    shape.Name = uniqueId;
    Variables vars = Doc.Variables;
    var = vars.Add(uniqueId, longName);
}

string GetLongNameOfShape(Shape shape)
{
    return GetLongNameById(shape.Name);
}

string GetLongNameById(string uniqueId)
{
    Variables vars = Doc.Variables;
    return vars.get_Item(uniqueId).Value;
}
于 2013-09-12T11:41:39.230 回答