PictureContentControl 不接受权限不足的图像属性

本文关键字:图像 属性 不接受 权限 PictureContentControl | 更新日期: 2023-09-27 18:32:40

我正在开发一个vsto-word插件,并将远程图像插入到picturecontent控件中的word文档中。

一切正常,除了在登录用户连接到远程登录域的计算机上。

我没有得到有关错误的信息。 它只是停止执行..图像和空内容控件插入到 Word 文档中,但作为两个对象,因此 ContentControl 不采用其 Image 属性。这也是代码停止执行的点:

public void resultImage(Result r, Dictionary<string, string> wizard)
{
      if (this.hasSelection())
      {
          PictureContentControl picture = getVSTODocument().Controls.AddPictureContentControl(getCCRange(), this.getRandomControlName());
          picture.LockContents = false;
          toggleActiveCCs(false);
          picture.Title = truncateCCTitle(r.title);
          picture.Tag = getWizardString(wizard, r.arrange);
                 try {
                     Image img = Fetcher.getImage(r.data)
                     picture.Image = img;
                 }
                 catch (Exception e) {
                     Log.alert(e.Message);
                 }

          afterInsert(picture.Range);
      }
    }

现在,我使用临时文件来存储图像,因为我想知道非管理员用户是否可能没有对内存的写入权限......我还使用临时文件(带有 html(插入一个表格,效果很好,也可以作为有限的域访问用户......所以我想这也应该适用于图像!?

我尝试了很多东西,包括:

  • 使用 StreamReader 和 Memory Streams 制作图像
  • 锁定的内容控件也有类似的行为,只是停下来工作,所以我确保它们都已解锁
  • 传输图像 base64 编码,也带有内存流...但这里也一样.

我也在MSDN上问了这个问题

更新 X:我确定了错误,这是 hresult 0x80004005(E_FAIL:未指定的故障(,没有多大帮助......该死的。

堆栈跟踪:

Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String fileName, Object&LinkToFile, Object&SaveWithDocument, Object&Range( at Microsoft.Office.Tools.Word.PictureContentControlImpl.SetImage(Image 图像(在 Microsoft.Office.Tools.Word.PictureContentControlImpl.set_Image(Image 值( at XXX.ThisAddIn.resultImage(Result r, 字典'2向导(

这肯定是一个权限问题,我如何检查/设置适当的权限.. ?!!

PictureContentControl 不接受权限不足的图像属性

我试图尽可能接近您在问题中使用的缺失代码部分。错误必须由这些缺失部分之一引起(获取范围,获取图像甚至插入后的部分(

因此,在隔离的 Office Addin 中尽可能多地采用代码确实可以正常工作:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    var vstodocument = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.Documents.Add());
    vstodocument.Paragraphs[1].Range.InsertParagraphBefore();
    PictureContentControl picture = vstodocument.Controls.AddPictureContentControl(vstodocument.Paragraphs[1].Range, "pictureControl2");
    picture.LockContents = false;
    picture.Title = "Title";
    picture.Tag = "Tag";
    try
    {
        // Before running put picture.bmp in C:'Users'<user>'Pictures
        string imagePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "''picture.bmp";
        System.Drawing.Bitmap bitmap1 = new System.Drawing.Bitmap(imagePath, true);
        picture.Image = bitmap1;
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show(ex.Message);
     }
 }

因此,请尝试隔离各个部分,看看其中是否有任何部分返回意外内容......

我找到了一个简单的解决方案...它也适用于此域登录计算机..只需将内联添加到一个范围,并使 ContentControl 超出此范围,例如:

Word.Range rng = getCCRange();
string tempPath = Fetcher.getImage(r.data);
rng.InlineShapes.AddPicture(tempPath);
PictureContentControl picture = getVSTODocument().Controls.AddPictureContentControl(rng, this.getRandomControlName());