Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-13-2018, 05:47 PM
puff puff is offline VBA to insert an image and centralize it (code included) Windows 7 64bit VBA to insert an image and centralize it (code included) Office 2013
Advanced Beginner
VBA to insert an image and centralize it (code included)
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Question VBA to insert an image and centralize it (code included)

Hi all. My target is fairly simple and it's just like use alt+I to open the insert image dialog, choose an image to insert and then centralize the image. My code so far:

Code:
Sub InsertPic()
    Dim oDialog As Dialog
    Dim strFile As String
    Dim oImage 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 = msoTrue
            .Height = 0.5 * .Height
            .Width = 0.5 * .Width 
            Set oRng = .ConvertToShape
        End With
        
        With oRng
            oRng.Select
            Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        End With
    Set oDialog = Nothing
    Set oImage = Nothing
    Set oRng = Nothing
End Sub
Questions:
1. The image inserted by my code has that blue anchor on the upper left corner and the centralizing part doesn't work. How can I get rid of that and make the image embedded in the texts to centralize the image? I think I messed up the difference between inline shape and shape objects.

2. Is that possible for me to set a default file location when the insert image dialog is open? For example, C:\Users\atom\Desktop\PDF

3. My code also includes a part where I want to resize the picture to 50% after insertion, but the resulting image is super small. Did I make a mistake there?

4. What changes do I need to make if I want to insert all the images from a file and process them as described above?



Thank you very much for your consideration. Any improvement of the original code is also welcomed.
Reply With Quote
  #2  
Old 01-13-2018, 11:25 PM
gmayor's Avatar
gmayor gmayor is offline VBA to insert an image and centralize it (code included) Windows 10 VBA to insert an image and centralize it (code included) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If you want to centralise the image it needs to be an inline shape. You can convert it, if you must, after you have centred it.

The argument lists for built in dialog boxes can be found at https://msdn.microsoft.com/en-us/lib...or=-2147217396 from which you will see it is not possible to set the start location for this dialog. You would need to use a different dialog to set the location such as Application.FileDialog(msoFileDialogFilePicker) e.g.

Code:
Sub InsertPic()
Dim fDialog As FileDialog
Dim strFile As String
Dim oImage As InlineShape
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .Title = "Select image to insert"
        .InitialFileName = "C:\Users\atom\Desktop\PDF\"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Images", "*.jpg,*.png,*.gif"
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then GoTo err_Handler:
    End With
    strFile = fDialog.SelectedItems.Item(1)
    Set oImage = Selection.InlineShapes.AddPicture(strFile)
    With oImage
        .LockAspectRatio = msoTrue
        .ScaleHeight = 50
        .ScaleWidth = 50
        .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
        .ConvertToShape
    End With
lbl_exit:
    Set fDialog = Nothing
    Set oImage = Nothing
    Application.WindowState = wdWindowStateNormal
    '    Documents.Add DocumentType:=wdNewBlankDocument
    '    ShowVisualBasicEditor = True
    Exit Sub
err_Handler:
    Err.Clear
    GoTo lbl_exit
End Sub
Inserting multiple images and then converting them to shapes is a minefield. The shapes are not part of the document text (unlike inline shapes) and thus you would have to position them or they will tend to do their own thing.

It would be better if you inserted the images into the cells of a table (with fixed width cells) and left them as inline images. That would fixe their positions on the page and also negate the need to adjust the size, which would adapt to the cell width.

See http://www.gmayor.com/photo_gallery_template.html
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-13-2018, 11:58 PM
macropod's Avatar
macropod macropod is offline VBA to insert an image and centralize it (code included) Windows 7 64bit VBA to insert an image and centralize it (code included) Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmayor View Post
If you want to centralise the image it needs to be an inline shape. You can convert it, if you must, after you have centred it.
That's not necessary, actually:
Code:
Sub InsertPic()
Dim strFile As String, iShp As InlineShape, Shp As Shape, Rng As Range
On Error Resume Next
With Dialogs(wdDialogInsertPicture)
  .Display
  If .Name <> "" Then
    strFile = .Name
  Else
    Exit Sub
  End If
End With
Set Rng = Selection.Range: Rng.Collapse wdCollapseEnd
Set iShp = ActiveDocument.InlineShapes.AddPicture(strFile, False, True, Rng)
Set Shp = iShp.ConvertToShape
With Shp
  .LockAnchor = True
  .LockAspectRatio = msoTrue
  .Height = .Height / 2
  .RelativeVerticalPosition = wdRelativeVerticalPositionLine
  .Top = 0
  .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
  .Left = wdShapeCenter
End With
Set Rng = Nothing: Set iShp = Nothing: Set Shp = Nothing
End Sub
puff: Multiple shapes can also be inserted this way, but the issue that then needs to be addressed is how their relative vertical positions are to be managed. See also:
https://www.msofficeforums.com/word-...html#post47919
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-17-2018, 04:18 PM
puff puff is offline VBA to insert an image and centralize it (code included) Windows 7 64bit VBA to insert an image and centralize it (code included) Office 2013
Advanced Beginner
VBA to insert an image and centralize it (code included)
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

The code has an error on .Display If .Name <> "" Then
Reply With Quote
  #5  
Old 01-17-2018, 04:19 PM
puff puff is offline VBA to insert an image and centralize it (code included) Windows 7 64bit VBA to insert an image and centralize it (code included) Office 2013
Advanced Beginner
VBA to insert an image and centralize it (code included)
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
If you want to centralise the image it needs to be an inline shape. You can convert it, if you must, after you have centred it.
You code works fine except that the image inserted float above the texts. I deleted ".ConvertToShape" and it looks great to me. Is this the right way of making it embedded into the text?
Reply With Quote
  #6  
Old 01-17-2018, 04:32 PM
macropod's Avatar
macropod macropod is offline VBA to insert an image and centralize it (code included) Windows 7 64bit VBA to insert an image and centralize it (code included) Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by puff View Post
The code has an error on .Display If .Name <> "" Then
Those are two separate code lines:
Code:
  .Display
  If .Name <> "" Then
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to insert an image and centralize it (code included) Issue of 2 fixed bullet points indent setting work together (code included) puff Word VBA 5 12-17-2017 05:52 PM
VBA to insert an image and centralize it (code included) image appering as code TedW Excel 1 11-17-2015 10:11 PM
VBA to insert an image and centralize it (code included) insert Image ?? Jazz Outlook 1 09-10-2015 04:34 AM
VBA to insert an image and centralize it (code included) Insert am Image phamh PowerPoint 1 02-25-2015 04:40 AM
Background image vs. insert image lilaria PowerPoint 0 04-18-2011 08:45 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:39 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