Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-11-2023, 07:38 PM
Guessed's Avatar
Guessed Guessed is offline Loop through pages from cursor position to end of pages Windows 10 Loop through pages from cursor position to end of pages Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

When you say you want to fill the shape, are you intentionally cropping the images to fit the shape of the box?



I would suggest you try the following which doesn't crop the images but allows you to specify a maximum size for the shape width or height.
Code:
Sub ImportFolderOfImages()
  Dim objFSO As Object, objFolder As Object, objFile As Object, sPath As String
  Dim aDoc As Document, aPict As InlineShape, iMaxSize As Integer, dblRatio As Double
  
  Set aDoc = ActiveDocument
  iMaxSize = CentimetersToPoints(6)                          'Set your maximum width
  sPath = SelectFolder                                       'Prompt user to select a path
  If Len(sPath) = 0 Then Exit Sub                            'Exit if folder not selected
  Set objFSO = CreateObject("Scripting.FileSystemObject")    'Create late bound instance FileSystemObject
  Set objFolder = objFSO.GetFolder(sPath)                    'Get the folder object
  
  'loop through each file in the directory and import any graphics
  For Each objFile In objFolder.Files
    Debug.Print objFile.Name, objFile.Type
    On Error GoTo SkipFails   'will fail if not importable graphic format
    Set aPict = aDoc.Paragraphs.Last.Range.InlineShapes.AddPicture(FileName:=objFile.Path)
    If Not aPict Is Nothing Then
      aPict.LockAspectRatio = msoTrue
      If aPict.Width > aPict.Height Then
        aPict.Width = iMaxSize
      Else
        aPict.Height = iMaxSize
      End If
      aPict.AlternativeText = objFile.Name
      aDoc.Range.InsertAfter vbCr
    End If
SkipFails:
  Next objFile
End Sub

Function SelectFolder(Optional sTitle As String = "Choose a Folder:", Optional sInitialPath As String) As String
  With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Title = sTitle
    .InitialFileName = sInitialPath
    .Show
    SelectFolder = .SelectedItems(1)
  End With
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
macro, ms-word, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to extract specific pages (Category) or arrange pages in a mail merge document? kitlwy Mail Merge 7 09-30-2021 04:50 AM
Loop through pages from cursor position to end of pages Number Pages - not total pages, but actual pages. Kiminator321 Word 8 04-29-2020 03:07 PM
Scroll through pages on loop ndearing Word VBA 5 12-17-2015 08:46 AM
Loop through pages from cursor position to end of pages Placing the same object/picture on all pages but on alternative (mirror position) - alceste Drawing and Graphics 1 09-29-2013 06:28 PM
Loop through pages from cursor position to end of pages create letter template with fixed position fields and follow up pages Lynn O'Shea Word 3 05-18-2010 12:32 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft