Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-10-2023, 10:45 AM
nubuki nubuki 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 2021
Novice
Loop through pages from cursor position to end of pages
 
Join Date: Dec 2023
Posts: 3
nubuki is on a distinguished road
Default Loop through pages from cursor position to end of pages

I'm trying to create a vba to loop through current page to the end of page and paste a copied shape on each of said pages.
I tried:

Code:
Sub PasteAndAlignItems()

    Dim totalPages As Integer

    Dim currentPage As Integer

    Dim startPosition As Range

    totalPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)

    Set startPosition = Selection.Range

    For currentPage = 1 To totalPages

        startPosition.Select

        Selection.PasteSpecial Placement:=wdInLine

        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1

    Next currentPage

End Sub
Works for text but not shapes; shapes get pasted on the same page with the cursor
Anyone knows what's wrong?
It would be very helpful if someone could add a snippet to position the pasted shape with horizontal alignment (left relative to page), vertical alignment (top relative to page).

Last edited by nubuki; 12-10-2023 at 10:47 AM. Reason: Grammatical fixes
Reply With Quote
  #2  
Old 12-10-2023, 03:46 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: 3,977
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

I can't see how it works for text because on each iteration you are returning to the original startPosition just before pasting at the selection location. It would make more sense to just remove the two lines that use startPosition.

When it comes to a shape in the clipboard that you are pasting, this can be complicated because Word appears to forget its positioning settings unless you include the paragraph it was anchored to in your clipboard. It is also tricky to just reposition the shape with code since there isn't an obvious way to work out which shape you just pasted. Maybe check the range of the current paragraph to see if it has any shapes anchored - but what happens if there is two or more?

Can you explain the reason you want code to do this? Perhaps there is a better way than using the clipboard and selection objects. If you create the shape with vba it is easy to get a handle for it so that you can position it consistently.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-11-2023, 04:43 AM
nubuki nubuki 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 2021
Novice
Loop through pages from cursor position to end of pages
 
Join Date: Dec 2023
Posts: 3
nubuki is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
I can't see how it works for text because on each iteration you are returning to the original startPosition just before pasting at the selection location. It would make more sense to just remove the two lines that use startPosition.

When it comes to a shape in the clipboard that you are pasting, this can be complicated because Word appears to forget its positioning settings unless you include the paragraph it was anchored to in your clipboard. It is also tricky to just reposition the shape with code since there isn't an obvious way to work out which shape you just pasted. Maybe check the range of the current paragraph to see if it has any shapes anchored - but what happens if there is two or more?

Can you explain the reason you want code to do this? Perhaps there is a better way than using the clipboard and selection objects. If you create the shape with vba it is easy to get a handle for it so that you can position it consistently.
Thanks a lot!
I moved the set start position into the loop
It works but skips the second page and paste on the rest of the pages
Edit:
I removed it completely like you said.
And it worked.

Copying with the paragraph works as you said

I'm trying to paste a bunch of shapes then fill them with consecutive pictures in a folder
I'm currently inserting the pictures manually.
Is there any method to achieve this through vba?

Regards, Nubuki.

Last edited by nubuki; 12-11-2023 at 07:00 AM.
Reply With Quote
  #4  
Old 12-11-2023, 02:26 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: 3,977
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

I think VBA will be relatively straightforward if the shape is always the same. I would be inclined use inline shapes though as it takes a lot of the complexity away.

If you post a sample document showing a couple of the graphics as you expect to see them then I can probably offer some code that would reproduce that from a folder of images.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 12-11-2023, 04:09 PM
nubuki nubuki 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 2021
Novice
Loop through pages from cursor position to end of pages
 
Join Date: Dec 2023
Posts: 3
nubuki is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
I think VBA will be relatively straightforward if the shape is always the same. I would be inclined use inline shapes though as it takes a lot of the complexity away.

If you post a sample document showing a couple of the graphics as you expect to see them then I can probably offer some code that would reproduce that from a folder of images.
Basically the rectangular shape should fill the page except the footer then the picture should fill the shape.
I'm using a shape instead of inserting directly because each photo size is dynamic.

I managed to write a vba that fill all shapes in a document with specified pictures:
Code:
Sub FillShapesWithPictures()
    Dim dialog As FileDialog
    Dim selectedFiles As FileDialogSelectedItems
    Dim i As Integer
    
    Set dialog = Application.FileDialog(msoFileDialogFilePicker)
    dialog.Title = "Select multiple JPG files"
    dialog.Filters.Add "JPEG files", "*.jpg; *.jpeg", 1
    
    If dialog.Show = -1 Then
        Set selectedFiles = dialog.SelectedItems
        
        i = 1
        For Each shape In ActiveDocument.Shapes
            If i <= selectedFiles.Count Then
                shape.Fill.UserPicture selectedFiles(i)
                i = i + 1
            Else
                Exit For
            End If
        Next shape
    End If
End Sub
So I use a combination of both vbas then copy the resulting images in shapes as paste them in the main document.
Any way to combine them?

Regards, Nubuki.
Reply With Quote
  #6  
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: 3,977
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 04:05 PM.


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