+ Reply to Thread
Results 1 to 10 of 10

Help with Macro to replace Shape UserPicture.

  1. #1
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Help with Macro to replace Shape UserPicture.

    I wrote the following macro to update images in Shapes in Word.

    The idea was to pass a reference to the shape object and change the Shape.Fill.UserPicture to a given image fill. I was using .PNG files.

    It runs, and returns "SUCCESS" with no errors, but the image is not updated. I can't work out why.
    Any help would be greatly appreciated.

    Note that I commented out the the error catch.

    Please Login or Register  to view this content.
    If my solution helped, please consider adding Rep

  2. #2
    Forum Guru macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,587

    Re: Help with Macro to replace Shape UserPicture.

    You've posted two functions, but not the macro that drives them.

    That said, as written, your functions will only impact the document containing them, not any other document.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Re: Help with Macro to replace Shape UserPicture.

    Quote Originally Posted by macropod View Post
    You've posted two functions, but not the macro that drives them.

    That said, as written, your functions will only impact the document containing them, not any other document.
    Yes. I wanted it to be restricted to the document the code is in. That was by design due to other factors.

    As for a driving macro, the macro I was using ran the updates via a text list generated in an excel doc that I couldn't share here.
    I built another driving macro, allowing the user to select a shape and replacement picture, calling the same functions. And frustratingly IT WORKS PERFECTLY.

    I'll now go back and try to find a functional difference between the versions.

    I've included my code, and a working example file demostrating use with both inline shapes and shapeRange shapes.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  4. #4
    Forum Guru macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,587

    Re: Help with Macro to replace Shape UserPicture.

    Since you're running this from Excel, instead of employing ThisDocument, which isn't where your code will reside, you should not only instantiate Word(e.g. Dim objWd as Object), but also explicitly tell objWd what document to work with - whether by opening that document directly or looping through the open ones to test which (if any) is the document you want, then set a reference to that document (e.g. Set objDoc = objWd.Documents(i)) then pass objDoc to your functions. For example:
    Please Login or Register  to view this content.
    Personally, though, I'd use early binding as it's much faster than late binding.

  5. #5
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Re: Help with Macro to replace Shape UserPicture.

    I am NOT running the code from excel. I merely mentioned that some of the DATA passed to the function happened to originate in Excel. But in this case, I am restricted that the image update MUST be run from word.
    This detail is not part of the problem I was trying to address in the OP. It was just the reason I had to give you a different driver macro, that didn't assume a number of prior operations to ensure matching filenames and shape titles.


    As I said before, I AM running the code from the Word document itself.
    I AM using ThisDocument to refer to tell the application which document to work with. If you look at my code, the function I am actually asking about (Private Function ReplaceShapeFill) consistently uses ThisDocument.

    I am not dimming shape as an Object for the sake of early or late binding. I am using Object because it is compatible with both ShapeRange objects and InlineShapes objects. I can not know in advance which type I will need to work with.

    So if you have any suggestions on why the first function I posted filled to replace the shape fills, but the second one succeeded, that would be helpful.
    Thanks

  6. #6
    Forum Guru macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,587

    Re: Help with Macro to replace Shape UserPicture.

    Quote Originally Posted by truk2 View Post
    I am NOT running the code from excel. I merely mentioned that some of the DATA passed to the function happened to originate in Excel.
    Well, what do you suppose one might understand from:
    the macro I was using ran the updates via a text list generated in an excel doc that I couldn't share here.
    Why even mention Excel if it's not relevant?
    Quote Originally Posted by truk2 View Post
    As I said before, I AM running the code from the Word document itself.
    You made no such statement in your previous posts.
    Quote Originally Posted by truk2 View Post
    I am using Object because it is compatible with both ShapeRange objects and InlineShapes objects. I can not know in advance which type I will need to work with.
    Not knowing beforehand is irrelevant, since you loop through both kinds anyway.

    Using Object references makes no sense at all if it's being run entirely within Word. In any event, you have:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Re: Help with Macro to replace Shape UserPicture.

    @Macropod.

    I apologise as this got a little adversarial. I'm finding it hard, as you've been fairly consistently ignoring either the context of my question (Why doesnít my function work?), , or the function of my code.

    I'll try to clarify.


    Quote Originally Posted by macropod View Post
    Well, what do you suppose one might understand from:

    Why even mention Excel if it's not relevant?
    You demanded to see the driver macro. I tried to explain that the driver macro in this project is not as friendly to testing by other users.
    I mentioned that my code works from a textfile listing the images to be processed, and that said text is generated in a separate process in excel. I did not expect you to redesign the whole process to work from excel based on the that passing mention.
    To be clear, the process is:
    -Objects in word have specific titles.
    -images to be imported are stored in same folder as the word document, with specific filenames
    -Textfile to control process contains the relevent file names and object titles, and is also stored in same folder.
    -Code in word reads the text file, and imports the relevant image files to their target objects.

    The process cannot be handled in the excel file due to various logistical issues that. Just accept that as part of the spec.

    Not knowing beforehand is irrelevant, since you loop through both kinds anyway.

    Using Object references makes no sense at all if it's being run entirely within Word. In any event, you have:
    Please Login or Register  to view this content.
    No.
    The important bit here is that GetShape() can return either a SHAPE or an INLINESHAPE as an object.
    Please Login or Register  to view this content.
    It is only after Iíve received that object that it can be tested to see which kind it is, and handle it appropriately.

    I hope that clarifies things.
    And I must say, while it didn't help with my question at all, I did appreciate the effort of the in depth guide on accessing word from excel.

    Next post will include files.

  8. #8
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Re: Help with Macro to replace Shape UserPicture.

    So getting back to the actual issue.

    Here I have two files.
    The zip file named "DoesNotWork" is an anomynised version of my project.
    It contains example image files, a textfile with formated processing instructions, and the .docm word file.

    When the "ImportData" sub is called, the text file is parsed and relevant images imported (which does not seem to work)
    Note that as this version is run by the text file, if a tester wants to try other images, they must have the same filenames as the ones provided, and be in the same folder as the docm file.


    The second file "DoesWork.docm" is a version I made to make it easier for any willing helpers to explore my code.
    I did away with the textfile control and specifically named image files.
    In this version, when "TestReplaceShapeFill" is called, the user can simply select a shape in the document, and choose an image file that they want to import.
    I CANNOT use this set for my project, but this version appears to work just fine. This is very confusing to me, as both versions call the same function, with very similar parameters. I basically changed the way a filename string is handled to suit the new driver macro.

    The relevant function in both versions is
    Please Login or Register  to view this content.
    So could anyone explain why one version works, and the other doesn't?
    That would help me a great deal.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Re: Help with Macro to replace Shape UserPicture.


  10. #10
    Forum Contributor
    Join Date
    07-01-2018
    Location
    Adelaide, South Australia
    MS-Off Ver
    Office 365, & Excel 2016 on windows 10, & 14.7 for mac, & Excel 2015 for mac
    Posts
    157

    Re: Help with Macro to replace Shape UserPicture.

    I finally worked this out.
    The shape fill userpicture method for changing an image only seems to work with a shape of type msoAutoshape (type 1). this is the same type of shape you get when you insert a shape from the ribbon by drawing a rectangle.

    My document had shapes of type 'msoChart' (type 3), and 'msoPicture' (type 13). I had to go through and replace them with type 1's. After that my code worked perfectly.

    I wrote the following function to help with deleting a shape, and replacing with as similarly sized and positioned Autoshape.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Assign macro with parameter to shape based on shape location
    By bobo3127 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-23-2014, 11:18 AM
  2. [SOLVED] A macro after setting onaction for a shape that will select the shape.
    By vonRobbo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2014, 11:34 PM
  3. [SOLVED] Use a button to control a macro that inserts an image into a shape or resets the shape
    By nwb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2013, 04:41 PM
  4. Max string length of Shape.Fill.UserPicture(PictureImage as String)?
    By AlvaroSiza in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2013, 09:36 AM
  5. Replies: 0
    Last Post: 11-30-2012, 01:29 PM
  6. Shape.Fill.UserPicture - Mapped Drive vs. UNC
    By jbruce23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2010, 12:21 PM
  7. Problem with .Comment.Shape.Fill.UserPicture
    By peter233 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2007, 12:37 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1