Results 1 to 13 of 13
  1. #1
    New Lounger
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Macro to insert image into Protected Document

    Hello,

    This is my FIRST ever post after newly subscribing. I hope I have posted this is the correct forum.

    I have a word document (2007) which needs to go all over the world. Its an application form which is protected. The prospective student needs to insert a passport sized photo into the document. I have some code which I have obtained from this site actually.

    I have modified it to apply a password to my document, however when I run the code, it returns a Run-time error '5941'. Something about looking for a bookmark or collection??? The code is as follows: The Blue bold font is where the debugger thing gets stuck. I am not a macro guru so I have no idea what this means and i have been working on it for 4 days now. Please put me out of my misery.

    Code:
    Sub InsertPic(n As Integer)
    
      Dim sFileName As String
      Dim ilImage As InlineShape
      Dim rng As Range
    
      If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
        ActiveDocument.Unprotect Password:="ABC"
      End If
      With Dialogs(wdDialogInsertPicture)
        .Display
        If .Name <> "" Then
          sFileName = .Name
          Set rng = ActiveDocument.Bookmarks("Pic" & n).Range
          Set ilImage = ActiveDocument.InlineShapes.AddPicture(sFileName, , True, rng)
          With ilImage
            'set any additional properties such as left, top, etc., here
          End With
        End If
      End With
      ActiveDocument.Protect wdAllowOnlyFormFields, True, Password:="ABC"
    End Sub

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    The code is looking for a bookmark that doesn't appear to exist in the document. The following modification allows it to place the image at the selection instead if the bookmark is not found
    Code:
    Sub TestInsertPic()
      InsertPic 3
    End Sub
    
    
    Sub InsertPic(n As Integer)
    
    
      Dim sFileName As String
      Dim ilImage As InlineShape
      Dim rng As Range
    
    
      If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
        ActiveDocument.Unprotect Password:="ABC"
      End If
      With Dialogs(wdDialogInsertPicture)
        .Display
        If .Name <> "" Then
          sFileName = .Name
          If ActiveDocument.Bookmarks.Exists("Pic" & n) Then
            Set rng = ActiveDocument.Bookmarks("Pic" & n).Range
          Else
            Set rng = Selection.Range
          End If
          Set ilImage = ActiveDocument.InlineShapes.AddPicture(sFileName, , True, rng)
          With ilImage
            'set any additional properties such as left, top, etc., here
          End With
        End If
      End With
      ActiveDocument.Protect wdAllowOnlyFormFields, True, Password:="ABC"
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    TanyaV (2012-11-12)

  4. #3
    New Lounger
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thank you

    Dear Andrew

    Thank you - it works beautifully!! You are amazing!!
    I am indebted to you :-)

    Regards
    Tanya

  5. #4
    New Lounger
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Um, if I may ask one more thing.... The code works really well and I am stoked, but when the image inserts into the table, the Macrofield words "Double click to insert image" also stays inside the table next to the image. This throws the table out. Is there any way I can add to the above code a command to delete the macrofield words on the way out? If it doesn't make sense, I am happy to upload the document I am using so you can have a look at what I mean when you run the macro.

    Thanks

  6. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Since you're using Word 2007, have you considered using Content Controls instead of formfields? Included in the Content Controls set is one for inserting pictures.
    Last edited by macropod; 2013-12-02 at 18:33.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #6
    New Lounger
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    This was so helpful for me also and I had the same question as the precious poster. Is there a way to edit the macro so that the words "insert image" get deleted when pic is added? Thank you!! I have word for Mac 2011

  8. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Sorcha

    Are you intending to use a bookmark to define the location or is there some other way of determining which 'insert image' text gets deleted? We need to be very specific about where the image is meant to go when there may be more than one location in the document where the text 'insert image' could appear.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #8
    New Lounger
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you Andrew! Sorry for my delay- I thought I responded yesterday. I hadn't intended to use a bookmark but I can do so. THere will be other sections of the document where a user may insert an image but the form fields of those sections will not have default text that says "Insert Image".
    Another related question - For some reason, this Macros works fine in Word for Mac 2011 but when I tried it in word 2013 and word 2007, it does not work. Is there anything I can do about that? Thank you so much for your help!

  10. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Sorcha

    Can you tell us which line is causing the problem on Word 2007 or 2013? I suspect that you have made changes to the shown code to match your document setup (since the posted code was specific to someone else's document) and it may be one of those lines causing the problem? We need to see your code modifications to work out where the error may lie.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  11. #10
    New Lounger
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Andrew,
    Thank you for your response! To further explain, when i first inserted the macros into my Word for Mac 2011 document, I did so by clicking on Macros in the developer ribbon, Create Macros, and copied and pasted the code (exactly as above). Then I protected the document. When I sent the form to someone else with word for mac 2011, that user was able to insert an image into the document. But when i sent the form to a user with Word 2013, the user was not able to insert an image. The same thing happened when I tried on Word 2007. No error appeared- I just wasn't able to insert an image into the space where the text form field is located. Thank you so much for your help (as you can tell, I am new to this, so I hope my question/explanation is clear).

  12. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    What macro-security setting does the Word 2013 user have?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. #12
    New Lounger
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    He said he enabled all macros.

  14. #13
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Can you attach a document to a post with the fields & code in question (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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