PictureContentControl does not accept Image property with insufficient permissions

0

I'm developing a vsto-word addin and am inserting remote images into a word document in a picturecontentcontrol.

it all works fine, except on machines where the logged in user is connected to a remote login domain.

I get no information about the error. it just stops execution .. the image and an empty content control are inserted in the word document, but as two objects, so the contentcontrol is not taking it's image property. that's also the point where the code just stops executing:

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

Right now, I use a temp file to store the image, as I was wondering if non-admin users may not have write permissions to memory ... I'm also using a temp file (with html) to insert a table, which works fine, also as limited domain-access user ... so I guess this should work for images too !?

I tried a bunch of stuff, including:

  • Using StreamReaders and Memory streams to make the image
  • Locked ContentControls have a similar behavoir, just stopping to work, so I make sure they're all unlocked
  • Transfering the image base64 encoded, with a memory stream too... but the same here too ..

i asked this question also on MSDN

update X: i idenfified the error, which is hresult 0x80004005 (E_FAIL: Unspecified failure) that does not help a lot ... damn.

stacktrace:

Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range) at Microsoft.Office.Tools.Word.PictureContentControlImpl.SetImage(Image image) at Microsoft.Office.Tools.Word.PictureContentControlImpl.set_Image(Image value) at XXX.ThisAddIn.resultImage(Result r, Dictionary`2 wizard)

it's definitly a permission problem, how can I check/set proper permissions .. ?!!

c#
.net
ms-word
vsto
word-contentcontrol
asked on Stack Overflow Feb 1, 2016 by rémy • edited Dec 21, 2018 by Cindy Meister

2 Answers

1

I tried to get as close as I could be with the missing code parts you used in your question. The error must be caused by one of these missing parts (getting the Range, getting the Image or even the part after inserting)

So adopting as many of your code as could be done in an isolated Office Addin did work correctly:

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

So try to isolate the individual parts and see if any of those return something unexpected ...

answered on Stack Overflow Feb 1, 2016 by Maarten van Stam
1

I found a simple solution ... which works also on this domain-login machines .. just add an inlineshap to a range, and make the contentcontrol out of this range, like:

Word.Range rng = getCCRange();

string tempPath = Fetcher.getImage(r.data);
rng.InlineShapes.AddPicture(tempPath);

PictureContentControl picture = getVSTODocument().Controls.AddPictureContentControl(rng, this.getRandomControlName());
answered on Stack Overflow Feb 6, 2016 by rémy

User contributions licensed under CC BY-SA 3.0