Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Jun 2009
    Location
    Hemet CA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking

    At the present I use this code to insert a picture to a bookmark on a 2007 word document it works great.
    Thank you HansV
    Code:
    Dim sFileName As String
      Dim ilImage As InlineShape
    
      If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
    	ActiveDocument.Unprotect "password here"
      End If
      With Dialogs(wdDialogInsertPicture)
    	.Display
    	If .Name <> "" Then
    	  sFileName = .Name
    Dim rng As Range
    Set rng = Bookmarks("bookmark name").Range
    Set ilImage = ActiveDocument.InlineShapes.AddPicture(sFileName, , True, rng)
    	  With ilImage
    		.Height = Application.InchesToPoints(1.5)
    		.Width = Application.InchesToPoints(1.75)
    	  End With
    	End If
      End With
      ActiveDocument.Protect wdAllowOnlyFormFields, True, "password here"
    The problem I have discovered is I have 48 bookmarks on this one document alone and I have 18 documents that need these changes. Is there a way that when I run this code that a input box would open, given me the option to insert the name of bookmark that I want photo to go to.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You could use code like this:

    Code:
    Dim strBookmark As String
    strBookmark = InputBox("Please enter the bookmark name")
    If Not ActiveDocument.Bookmarks(strBookmark).Exists Then
      ' User entered an invalid name - write code to handle this
      ...
    End If
    Set rng = Bookmarks(strBookmark).Range

  3. #3
    3 Star Lounger
    Join Date
    Jun 2009
    Location
    Hemet CA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi HansV
    Thank you once again, it works fantastic
    This will save so much time...................

  4. #4
    3 Star Lounger
    Join Date
    Jun 2009
    Location
    Hemet CA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi HansV
    The code works great with a command button, but when I attach this code to my UI Ribbon control, it doesnt want work.
    it comes back as an error. It says Sub or Function not defined, Here is how it looks
    Code:
    'Callback for customButton8 onAction
    Sub RunPicture(control As IRibbonControl)
    Dim strBookmark As String
    strBookmark = InputBox("Insert Photo Location Name Below Then Press OK", "Inspection Photos")
    If Not ActiveDocument.Bookmarks(strBookmark).End Then
    End If
    Set rng = Bookmarks(strBookmark).Range
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
    	ActiveDocument.Unprotect ""
      End If
      With Dialogs(wdDialogInsertPicture)
    	.Display
    	If .Name <> "" Then
    	  sFileName = .Name
    
    Set ilImage = ActiveDocument.InlineShapes.AddPicture(sFileName, , True, rng)
    	  With ilImage
    		.Height = Application.InchesToPoints(1.5)
    		.Width = Application.InchesToPoints(1.75)
    	  End With
    	End If
      End With
      ActiveDocument.Protect wdAllowOnlyFormFields, True, ""
    End Sub
    This part was highlighted in yellow
    Code:
    Sub RunPicture(control As IRibbonControl)
    And this part was also highlighted
    Code:
    Set rng = Bookmarks(strBookmark).Range
    I tried a couple changes with no such luck

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    I don't have Office 2007 so I can't help you with the ribbon aspect of the code.

    The line

    If Not ActiveDocument.Bookmarks(strBookmark).End Then

    should be

    If Not ActiveDocument.Bookmarks(strBookmark).Exists Then

    (see my previous reply)

    The line

    Set rng = Bookmarks(strBookmark).Range

    should be

    Set rng = ActiveDocument.Bookmarks(strBookmark).Range

    (that was an omission on my part - sorry about that).

  6. #6
    3 Star Lounger
    Join Date
    Jun 2009
    Location
    Hemet CA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi HansV
    Thank you once again
    I did change this part of the code
    Code:
    Set rng = Bookmarks(strBookmark).Range
    with this
    Code:
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    and everything work perrrrrrrfect
    but from Visual Basic editor as I was checking the code, there was no drop down option for "Exists" in that location, the closest I thought was End
    here are the other choices
    Application, Column, Copy, Creator, Delete, Empty, End, Name, Parent, Range, Select, Start and StoryType

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Try

    If Not ActiveDocument.Bookmarks.Exists(strBookmark) Then

  8. #8
    3 Star Lounger
    Join Date
    Jun 2009
    Location
    Hemet CA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='795926' date='01-Oct-2009 12:58']Try

    If Not ActiveDocument.Bookmarks.Exists(strBookmark) Then[/quote]

    Hi HansV
    this works
    how is it that even though I had "End' on the code it was still able to function in the same manor.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    As long as the bookmark name that you enter is valid, the code worked OK. But you'd get an error message if you entered an invalid name.

  10. #10
    3 Star Lounger
    Join Date
    Jun 2009
    Location
    Hemet CA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you HansV
    That makes total since

Posting Permissions

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