none
I really need help! (Macro for Word) RRS feed

  • Question

  • Hello.

    I happen to work as a Structural Engineer at a somewhat new office and we make structural evaluations for new and old projects. Thing is for each project we have to deliver a report, wich includes having several images (sometimes a lot of them) in the document. I found a macro for adding all the pitures, found one for changing the size and made one (almost didn't believe it myself) for giving them a certain style.

    Here comes the problem...

    I need to add a picture on top of those pictures i already added, at a certain position and with a certain size. If anyone could help me with this it'd be great. I have a report of almost 400 pictures to make right now!

    Thursday, January 19, 2017 8:17 PM

All replies

  • Hello.

    I happen to work as a Structural Engineer at a somewhat new office and we make structural evaluations for new and old projects. Thing is for each project we have to deliver a report, wich includes having several images (sometimes a lot of them) in the document. I found a macro for adding all the pitures, found one for changing the size and made one (almost didn't believe it myself) for giving them a certain style.

    Here comes the problem...

    I need to add a picture on top of those pictures i already added, at a certain position and with a certain size. If anyone could help me with this it'd be great. I have a report of almost 400 pictures to make right now!

    Thursday, January 19, 2017 7:03 PM
  • Hello.

    I happen to work as a Structural Engineer at a somewhat new office and we make structural evaluations for new and old projects. Thing is for each project we have to deliver a report, wich includes having several images (sometimes a lot of them) in the document. I found a macro for adding all the pitures, found one for changing the size and made one (almost didn't believe it myself) for giving them a certain style.

    Here comes the problem...

    I need to add a picture on top of those pictures i already added, at a certain position and with a certain size. If anyone could help me with this it'd be great. I have a report of almost 400 pictures to make right now!

    Well for Microsoft Word Macros you probably should ask in this Forum - Word for Developers as you posted your question in a VB.Net Forum.

    Unless you are using VB.Net to automate Word using a VB.Net application you wrote which you do not mention. But even then I suppose a Macro for Word would not be a question for this forum.


    La vida loca

    Thursday, January 19, 2017 7:40 PM
  • Hi angelgermanr,

    you can try to add bookmark where you want to insert as image.

    then you can go to that bookmark and add the image there.

    please refer the example below.

    I have added the bookmark named "bm1" at the cursor position where I want to insert an image.

    then I run the code below to insert an image with specified height and width.

    Sub Example2()
    Dim iShp As InlineShape
    Selection.GoTo What:=wdGoToBookmark, Name:="bm1"
    Set iShp = Selection.InlineShapes.AddPicture(FileName:="C:\Users\v-padee\Desktop\myimg\p2.png", LinkToFile:=False, SaveWithDocument:=True)
     With iShp
            .LockAspectRatio = True
            .Height = CentimetersToPoints(2.5)
            .Width = CentimetersToPoints(2.5)
          End With
    End Sub
    

    Output:

    other thing you had mentioned that you want to insert many images. for that you can refer the example below.

    it will open a file dialog. you just need to select a folder that contains all the images and click ok.

    it will add all the images at bookmark location with specified height and width.

    Example code:

    Sub example41()
    Dim iShp As InlineShape
    Dim intResult As Integer
    Dim strPath As String
    Dim strFolderPath As String
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim i As Integer
    
    'the dialog is displayed to the user
    intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
    'checks if user has cancled the dialog
    If intResult <> 0 Then
        'dispaly message box
        strFolderPath = Application.FileDialog(msoFileDialogFolderPicker _
            ).SelectedItems(1)
        'Create an instance of the FileSystemObject
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        'Get the folder object
        Set objFolder = objFSO.GetFolder(strFolderPath)
        i = 1
        'loops through each file in the directory and prints their names and path
        For Each objFile In objFolder.Files
            'get file path
            strPath = objFile.Path
            'insert the image
            Selection.GoTo What:=wdGoToBookmark, Name:="bm1"
    
           Set iShp = Selection.InlineShapes.AddPicture(FileName:= _
               strPath, LinkToFile:=False, _
               SaveWithDocument:=True)
               With iShp
            .LockAspectRatio = True
            .Height = CentimetersToPoints(2.5)
            .Width = CentimetersToPoints(2.5)
          End With
        Next objFile
    End If
    End Sub
    
     

    Output:

    Reference taken from below and modified as per above example.

    Word VBA Insert Images

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 20, 2017 1:15 AM
    Moderator
  • Thank you!

    Something else, i need it to be on top of the other images i already added. I thought using wdWrapFront but i have no idea where to put it. Could you help with this last thing?

    Friday, January 20, 2017 2:36 PM
  • And i noticed something else, i place several bookmarks in the document but pictures are only inserting in the first one, all next to each other.
    Friday, January 20, 2017 3:36 PM
  • Hi angelgermanr,

    you had mentioned that,"And i noticed something else, i place several bookmarks in the document but pictures are only inserting in the first one, all next to each other."

    code is working correctly, there is problem in your logic.

    you had mentioned that you put several bookmarks in the document.

    did you modify the code to check all that bookmarks?

    only creating the bookmark in document is just the half thing.

    you need to check the each bookmark in the code and according to that you have to insert images.

    here if you put several bookmark then you need to use loop to go through each bookmark.

    then select it.

    then insert the image at that place.

    you have to make many changes in code mentioned above to work it with several bookmarks.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 23, 2017 6:02 AM
    Moderator
  • Unless you want to add (apparently) hundreds of bookmarks, that approach won't work. In any event, they're unnecessary - a macro can handle all of this easily enough without them. If you can tell us what:
    • wrap format the existing pictures use;
    • wrap format the new pictures should use;
    • the height, width & style of the new pictures should be,
    it should be easy enough to write a macro to do the job.

    For future reference, if you're trying to insert pictures into a document and constrain them to fit within a specified height & width, it's often best to insert them into single-cell tables of a fixed height & width. That also makes keeping any captions with the pictures as the document is edited. To that send, see: http://www.msofficeforums.com/word/16772-4-digital-images-1-page.html#post47919


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, January 23, 2017 9:01 AM