Photo inserted with Word VBA macro has incorrect rotation

I've written a Word macro that creates a file dialog which allows the user to select a photo.  I do that because I want to bring that photo into a new blank document they can use as an editing canvas to crop and resize the photo, then click a button to transfer that over to the main document.  But when I insert the photo programmatically into the temporary editing document, it always comes in with landscape orientation, even if in the file picker it shows as portrait.  

My guess is all jpg files start in landscape, but can be rotated to portrait by the user when in a file preview program like Windows explorer.  If you manually insert a picture in Word using regular ribbon commands, it knows to preserve the portrait rotation.  But when I use VBA to add the selected picture, it does not recognize the rotation, and always comes in landscape.  Is there a property I can access that tells me whether the photo has been rotated relative to its original orientation, so I can mimick that after importing?


Set reportdoc = ActiveDocument
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Title = "Select Photo"
fd.Filters.Add "Pictures", "*.jpg;*.gif;*.png;*.bmp;*.tif;*.tiff;*.emf;*.wmf;*.jpeg", 1
fd.InitialFileName = testprojectfolder & "\Photos\"

With fd
    If .Show = -1 Then
        pickedphoto = .SelectedItems(1)
        Exit Sub
    End If
End With
Set fd = Nothing

Set tempphotodoc = Application.Documents.Add
Set selectedpix = tempphotodoc.Shapes.AddPicture(FileName:=pickedphoto, LinkToFile:=False, SaveWithDocument:=True)

Open in new window

Bryce BassettFreelance VBA programmerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Farzad AkbarnejadDeveloperCommented:
Add the following line as last line of code of macro.

tempphotodoc.Shapes(1).IncrementRotation 90

I can't reproduce the problem (Word 2007, Windows 10).

The orientation of a JPG picture is determined by EXIF fields within the file. The relevant fields are Orientation, Image Height and Image Width. Extracting the fields is not a trivial matter, though there are free downloadable VB6 and VB.Net EXIF reader class files available. If it were a general problem, then you might have to resort to that.

In case there is a problem with your picture, can you attach an example here, so that it can be tested on other systems.

If it works properly elsewhere, you might have to fix your Word installation.
Bryce BassettFreelance VBA programmerAuthor Commented:
Thanks for the suggestion, Farzad, but your code rotates every picture.  I need to find out first whether the picture has been rotated from its original orientation, and only rotate it if necessary.

By as I think about it, is the computer ever going to be able to find the normal rotation?  What if they tilted the camera to the left as opposed to the right to take a portrait shot?  I could never be sure which way is up.  But at least I could replicate how they see it through an explorer window.  Who thought this stuff could get so complicated?

Graham, I've attached a couple of iPhone photos which show this behavior.  In my windows explorer, they are corrected oriented, one portrait and the other landscape.  But when I bring them in with the code I posted earlier, they both come in as landscape.  I think I would need something like the EXIF reader, but that's probably fancier than I want to get.  By the way, this may be a dumb question, but if I were to try this, is there an easy way to I incorporate VB code into VBA, or is it a matter of manually translating it into VBA?

My solution for now is to provide a "rotate 90 degrees" button so the user can orient the picture correctly in tempphotodoc before inserting it into the real doc.   Let me know if you have other ideas.globe portraitglobe portrait
I'm using Word 2010 on Windows 8.1

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Bryce BassettFreelance VBA programmerAuthor Commented:
That second photo should be entitled globe landscape by the way.
Bryce BassettFreelance VBA programmerAuthor Commented:
Guys, this is very closely related, so I'm going to post this continuation.  After my user has cropped and rotated and resized the photos and inserted them into the main document, they'll have a tool they can use to align and evenly distribute the images on the page.  I'm getting unexpected behavior, and it still has to do with rotation settings.

Below are four screenshots.  
The first is my three photos as I see them on the Word page.  I have selected each.  Note from the location of the rotation handles that the first two on the left are set to rotation 0, but the third is set to rotation 90.
If I apply this VBA command to the selected group:  Selection.ShapeRange.Align msoAlignTops, False, they align to the top, but the one on the right has grown wider, as if it is aligned based on its zero rotation orientation before aligning, but it doesn't actually rotate it, but stretches it.
If I apply this VBA command to the original select group:  Selection.ShapeRange.Distribute msoDistributeHorizontally, True, they space evenly, but again the one on the right is stretched before spacing.
If I apply both of the commands above, first align, then distribute, the third photo ends up looking normal again, but the distribution is not even because it was using the stretched version to distribute.
Can you help me solve this?  Do I have to abandon the built in align and distribute commands and do those things "by hand" programmatically, or is there some way to capture the orientation before I apply the commands and adjust on the back end.   Very strange behavior.

Thanks.original selected picturesAfter using VBA align commandAfter using VBA distribute commandAfter using VBA align and distribute commands
Bryce BassettFreelance VBA programmerAuthor Commented:
Discovered that for the picture on the right, which is already rotated 90 degrees, the shape.height and shape.width parameters are relative to 0 rotation.  So it ignores what the human eye sees and reports that shape to be 160 wide and 120 high.   I'm sure this is related to the strange behavior.  This would be the clue to doing this "by hand," but I'm still wondering if there is an easier way.
Bryce BassettFreelance VBA programmerAuthor Commented:
By the way, it does the same thing when you use the ribbon menu commands to align or distribute, so it's not unique to VBA
To your original problem regarding orientation:

The relevant EXIF fields that I mentioned earlier are mostly absent from your images.

The handling of orientation seems to be inconsistent between applications. Your 'portrait' picture appears on its side in my Firefox browser, but when downloaded, Windows Explorer shows it as desired in portrait format. The picture appears in landscape when inserted into a Word document. You can rotate pictures in Windows Explorer, but that doesn't affect the orientation when inserting into a Word document.

I can change the orientation in my picture management software (ThumbsPlus). If the picture had a non-digital origin, the application warns that some information along one side could be lost, since the pixel width and length must be divisible by 16. When re-orientated in this way, the orientation is maintained when inserting into a Word document.

In short, you need to ensure that the images really are in the orientation that you want, and aren't just being displayed as such.

I have just found this blog, which explains it all in more detail:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bryce BassettFreelance VBA programmerAuthor Commented:
Never really completely answered the question, but this stuff is pretty complicated.  Thanks for at least pointing me in some useful directions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.