How to insert an automatically updating picture that re-sizes and re-orients itself

Hello. I am hoping to find a macro which will allow me to insert an object (which will be a picture) that will automatically update itself, re-size itself and center itself upon opening. This will be used for large word documents that have references to data sheet images which change. When these data sheets change i would save them as the same name and file type as the original and then move the original file to an obsolete folder, eventually repeating this process once the data sheets change again. I currently have some bits of code which I am not sure how to integrate but this code to follow adds a image as an inline shape, upon running the file explorer opens so the user can select what image to place into the word document. After selecting the desired image it is re-sized and re-oriented. I am looking for a macro that will open the 'object' command in the insert tab, open the browse prompt for 'create from file' and then make it so the 'link to file' box is checked. At which point I would like the image re-sized and re-oriented. PLEASE HELP! ANY INPUT IS GREATLY APPRECIATED! 

 Sub Insert_picture_and_format()
'
' Insert_picture_and_format Macro
'
'
Dim oDialog As Dialog
 Dim strFile As String
 Dim oImage As Object
 Dim oRng As Object
     Set oDialog = Dialogs(wdDialogInsertPicture)
     With oDialog
         .Display
         If .Name <> "" Then
             strFile = .Name
         End If
     End With
     Set oImage = Selection.InlineShapes.AddPicture(strFile)
     With oImage
         .LockAspectRatio = msoFalse
         .Height = CentimetersToPoints(25)
         .Width = CentimetersToPoints(19)
         Set oRng = .ConvertToShape
     End With
     With oRng
         .RelativeHorizontalPosition = _
         wdRelativeHorizontalPositionPage
         .RelativeVerticalPosition = _
         wdRelativeVerticalPositionPage
         .Left = CentimetersToPoints(1.25)
         .Top = CentimetersToPoints(1.25)
     End With
 Set oDialog = Nothing
 Set oImage = Nothing
 Set oRng = Nothing
End Sub

I also use a macro for updating file paths as soon as a document is opened (usually in reference to the document filepath itself, so that the location can be easily found by another person) I am not sure if this macro would come in handy for updating the object (which would be a image in this case) or if the linked image would automatically update upon opening the word document.

Sub AutoOpen()
'
' AutoOpen Macro
'
'
Dim aStory As Range
   Dim aField As Field

   For Each aStory In ActiveDocument.StoryRanges

      For Each aField In aStory.Fields
         aField.Update
      Next aField

   Next aStory
   
End Sub

Answer
Answer

Doug's version of the AddPicture call is functionally identical to mine, except for passing the arguments by position rather than by name.

The reason your version "did not work" is that you missed the point of the line continuation underscores that made all three of my lines into one statement (http://www.word.mvps.org/FAQs/MacrosVBA/_AtEndOfLine.htm). Then, when you put the LinkToFile and Range arguments outside the parentheses and made separate statements with them, VBA assumed that you were simply assigning values to (undeclared) variables -- and those had no effect on the AddPicture call or on oImage. In order to have a linked picture, the LinkToFile argument in the AddPicture call must be set to True.

One other misstatement: The code you originally posted and the second post both include the line

   Dim oImage As Object

so that variable most certainly is declared as an Object rather than as an InlineShape. The fact that the AddPicture method returns an InlineShape, which is then assigned to oImage, doesn't change that declaration. The line should be

   Dim oImage As InlineShape

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 964 Applies to: