Results 1 to 1 of 1

Insert all images if folder name matches same cell value (col a below loop)

Threaded View

  1. #1
    Registered User
    Join Date
    02-16-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    52

    Insert all images if folder name matches same cell value (col a below loop)

    Hi, I have a task that is tedious. I hope you can help me with automating it. I have an excel sheet ROW A contains info. I also have a folder (which contains the pictures) with same value. Using macro need to match "same/ identical" folder name and insert it to same Word document. Right now, I have to manually pick using Folder Dialog and macro (see the code below):

    Sub InsertSpecificNumberOfPictureForEachPage()
      Dim strFolder As String
      Dim strFile As String
      Dim objDoc As Document
      Dim dlgFile As FileDialog
      Dim objInlineShape As InlineShape
      Dim nResponse As Integer
      Dim strPictureNumber As Integer
      Dim strPictureSize As String
      Dim n As Integer
    
    Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)
      
      With dlgFile
        If .Show = -1 Then
          strFolder = .SelectedItems(1) & "\"
        Else
          MsgBox ("No Folder is selected!")
          Exit Sub
        End If
      End With
    
      strFile = Dir(strFolder & "*.*", vbNormal)
      strPictureNumber = InputBox("Input the number of the picture for each page", "Picture Number", "For exemple: 1")
      n = 1
    
      While strFile <> ""
        Selection.InlineShapes.AddPicture FileName:=strFolder & strFile, LinkToFile:=False, SaveWithDocument:=True
        Selection.TypeParagraph
        Selection.Collapse Direction:=wdCollapsEnd
        'Selection.TypeText Text:=Left(strFile, InStrRev(strFile, ".") - 1)
        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        If ActiveDocument.InlineShapes.Count = strPictureNumber * n Then
    '      Selection.InsertNewPage
    '      Selection.TypeBackspace
          n = n + 1
        End If
        Selection.TypeParagraph
        strFile = Dir()
      Wend
    
      For Each objInlineShape In ActiveDocument.InlineShapes
        objInlineShape.Select
        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
      Next objInlineShape
    
      nResponse = MsgBox("Do you want to resize all pictures?", 4, "Resize Picture")
      If nResponse = 6 Then
        strPictureSize = InputBox("Input the height and width of the picture, seperated by comma", "Height and Width", "For exemple:500,500")
        For Each objInlineShape In ActiveDocument.InlineShapes
          objInlineShape.Height = Split(strPictureSize, ",")(0)
          objInlineShape.Width = Split(strPictureSize, ",")(1)
        Next objInlineShape
      End If
    End Sub
    From above strFolder should be same as cell value (excel sheet col A). Also, please tweak the codes so that the pictures inserted will fit one page regardless of how many. I will do the layout manually. Thanks everyone for the advance help!
    Last edited by jhoelski; 09-20-2019 at 02:26 AM. Reason: code tags

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA to insert images in a cell with dynamic URL
    By tnyssd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2019, 04:16 PM
  2. Assign Hyperlink to cell if data matches files from a folder/sub folder
    By f_htn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2018, 04:03 PM
  3. Loop through folders and execute code if folder name matches specified names
    By hopeful_positive in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-20-2018, 04:40 AM
  4. Loop through folders and only implement code if folder name matches from specified names
    By hopeful_positive in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2018, 11:46 AM
  5. Batch insert images in cell comments
    By Saighead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2018, 03:58 PM
  6. [SOLVED] Macro to insert url images into cell ?
    By jkm500 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-21-2015, 08:29 AM
  7. Macro to insert Google images into cell
    By mhisan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-19-2014, 01:59 PM

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