I have a report that uses Excel 2007 and I would like to be able to insert different JPEG, GIF or Bitmap onto a Word.docm using VBA from my UserForm and for it to have a behind text formatting. If possible insert image on lower left side of word.docm page.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Using VBA from Excel to insert image into a word.docm
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Using VBA from Excel to insert image into a word.docm
- This topic has 14 replies, 2 voices, and was last updated 15 years, 9 months ago.
AuthorTopicWSababenchrist
AskWoody LoungerAugust 24, 2009 at 8:04 pm #462084Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerAugust 25, 2009 at 12:41 am #1174895I’d record a macro in Word, then study the result to get an idea of the code you’ll need in Excel.
I assume you’ll be using Automation to control Word from Excel; take great care to make every Word object that you use refer directly or indirectly to the Word.Application object that you create. -
WSababenchrist
AskWoody LoungerAugust 25, 2009 at 9:05 am #1174914I’d record a macro in Word, then study the result to get an idea of the code you’ll need in Excel.
I assume you’ll be using Automation to control Word from Excel; take great care to make every Word object that you use refer directly or indirectly to the Word.Application object that you create.Hi HansV
I tried this approach. I formatted a few cells on a excel sheet to an estimated size I needed and then merged them with wrap text. I then selected copy from that cell, now I opened my word document and clicked on to my selected table and then pressed Paste Special, from there I selected Microsoft Office Excel Worksheet object and selected link and then pressed ok. I then made what ever adjustments needed to fit table.
Will this method work or do I need to make some other changes. Is there any pros or cons..
I tried using the VBA method as you described, I’m still working on that. -
WSHansV
AskWoody Lounger -
WSababenchrist
AskWoody LoungerAugust 25, 2009 at 9:44 am #1174917 -
WSababenchrist
AskWoody LoungerAugust 25, 2009 at 12:54 pm #1174933Since I don’t know what you want to accomplish, it’s hard to say whether you’ll need to do more.
Hi HansV
The linking method works good as I previously mentioned. I am now trying to use a command button on my excel worksheet1 where I can select a photo then insert photo into cell (B11) and resize to fit merged cell. This is what I have so far. I develop an error at “ActiveSheet.Pictures.Insert”Code:Private Sub CommandButton1_Click() Dim sFile As Variant, r As Range sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture") If sFile = False Then Exit Sub ActiveSheet.Pictures.Insert With ActiveSheet.Shapes(ActiveSheet.Shapes.Count) .LockAspectRatio = True .Top = r.Top .Left = r.Left .Height = r.RowHeight * r.MergeArea.Rows.Count End With End Sub
-
WSHansV
AskWoody LoungerAugust 25, 2009 at 1:32 pm #1174936 -
WSababenchrist
AskWoody LoungerAugust 25, 2009 at 2:14 pm #1174943There are two problems:
1. The line
ActiveSheet.Pictures.Insert
should be
ActiveSheet.Pictures.Insert sFile
otherwise VBA doesn’t know which file to use.
2. You don’t set the range variable r anywhere. Since you mention cell B11, you should insert a line
Set r = Range(“B11”)
Hi HansV
I made the changes and does not insert photo into cell B11, but it does insert the command buttonCode:Private Sub CommandButton1_Click() Dim sFile As Variant, r As Range Set r = Range("B11") sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture") If sFile = False Then Exit Sub ActiveSheet.Pictures.Insert sFile With ActiveSheet.Shapes(ActiveSheet.Shapes.Count) .LockAspectRatio = True .Top = r.Top .Left = r.Left .Height = r.RowHeight * r.MergeArea.Rows.Count End With End Sub
-
WSHansV
AskWoody Lounger -
WSababenchrist
AskWoody LoungerAugust 29, 2009 at 11:17 am #1175453I’d try again – I tested the code and it does insert the selected picture in the worksheet.
Hi HansV
Yes you are correct it does insert photo in the sheet.
but it seems to insert command button into selected cell (“b11”) not photo
The photo does insert just not in the correct cell.
I also wanted to add an element of danger to this code. First off I want like to be able use a command button that would first unprotect sheet named“Invoice”, then Browse to select a picture, when jpg or bmp has been selected and inserted to a pre-selected cell that worksheet will re-protected, is this possible. or am I reaching…….Code:Private Sub CommandButton1_Click() ActiveSheet.Unprotect Dim sFile As Variant, r As Range Set r = Range("B11") sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture") If sFile = False Then Exit Sub ActiveSheet.Pictures.Insert sFile With ActiveSheet.Shapes(ActiveSheet.Shapes.Count) .LockAspectRatio = True .Top = r.Top .Left = r.Left .Height = r.RowHeight * r.MergeArea.Rows.Count ActiveSheet.protect End With End Su
-
WSHansV
AskWoody LoungerAugust 29, 2009 at 11:30 am #1175454I have no idea why the code would insert a command button on the sheet.
You should unprotect the sheet AFTER the line If sFile = False Then Exit Sub, otherwise you’ll leave the sheet unprotected if the user cancels the dialog.
Try this version:
Code:Private Sub CommandButton1_Click() Dim sFile As Variant, r As Range Set r = Range("B11") sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture") If sFile = False Then Exit Sub ActiveSheet.Unprotect With ActiveSheet.Pictures.Insert(sFile).ShapeRange .LockAspectRatio = True .Top = r.Top .Left = r.Left .Height = r.RowHeight * r.MergeArea.Rows.Count End With ActiveSheet.Protect End Sub
-
WSababenchrist
AskWoody LoungerAugust 29, 2009 at 11:42 am #1175455I have no idea why the code would insert a command button on the sheet.
You should unprotect the sheet AFTER the line If sFile = False Then Exit Sub, otherwise you’ll leave the sheet unprotected if the user cancels the dialog.
Try this version:
Code:Private Sub CommandButton1_Click() Dim sFile As Variant, r As Range Set r = Range("B11") sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture") If sFile = False Then Exit Sub ActiveSheet.Unprotect With ActiveSheet.Pictures.Insert(sFile).ShapeRange .LockAspectRatio = True .Top = r.Top .Left = r.Left .Height = r.RowHeight * r.MergeArea.Rows.Count End With ActiveSheet.Protect End Sub
Thank you HansV
On the protection that makes since, I still don’t know why it inserts my command button in (“B11”) selected cell. I have tried on different sheets, always with the same result. I guess I’ll have to investagate further to see why. -
WSHansV
AskWoody LoungerAugust 29, 2009 at 11:48 am #1175456 -
WSababenchrist
AskWoody LoungerAugust 29, 2009 at 12:17 pm #1175460There must be something else going on. It’s absolutely impossible that this could would insert a command button (but if you select a picture of a command button in the open dialog that is presented, that picture would be inserted; it would not be a real command button though).
Hi HansV
I played around with it and to my surprise its working, I dont have a clue what i did or not do, but thats behind me now.
one last thing, what can I add to this code that it will format inserted photo to send to back or behind text -
WSHansV
AskWoody Lounger
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Windows 11 ad from Campaign Manager in Windows 10
by
Jim McKenna
13 hours, 21 minutes ago -
Small desktops
by
Susan Bradley
48 minutes ago -
Totally disable Bitlocker
by
CWBillow
8 hours, 13 minutes ago -
Phishers extract Millions from HMRC accounts..
by
Microfix
22 hours ago -
Windows 10 22H2 Update today (5 June) says up-to-date but last was 2025-04
by
Alan_uk
23 hours, 52 minutes ago -
Thoughts on Malwarebytes Scam Guard for Mobile?
by
opti1
1 day, 3 hours ago -
Mystical Desktop
by
CWBillow
1 day, 3 hours ago -
Meta and Yandex secretly tracked billions of Android users
by
Alex5723
8 hours, 32 minutes ago -
MS-DEFCON 2: Do you need that update?
by
Susan Bradley
1 hour, 26 minutes ago -
CD/DVD drive is no longer recognized
by
WSCape Sand
1 day, 18 hours ago -
Windows 11 24H2 Default Apps stuck on Edge and Adobe Photoshop
by
MikeBravo
1 day, 21 hours ago -
North Face and Cartier customer data stolen in cyber attacks
by
Alex5723
1 day, 19 hours ago -
What is wrong with simple approach?
by
WSSpoke36
1 day, 11 hours ago -
Microsoft-Backed Builder.ai Set for Bankruptcy After Cash Seized
by
Alex5723
2 days, 6 hours ago -
Location, location, location
by
Susan Bradley
21 hours, 13 minutes ago -
Cannot get a task to run a restore point
by
CWBillow
2 days, 8 hours ago -
Frustrating search behavior with Outlook
by
MrJimPhelps
1 day, 22 hours ago -
June 2025 Office non-Security Updates
by
PKCano
2 days, 18 hours ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
21 hours, 33 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
2 days, 18 hours ago -
How start headers and page numbers on page 3?
by
Davidhs
3 days, 5 hours ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
2 days, 8 hours ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
3 days, 13 hours ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
3 days, 14 hours ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
3 days, 3 hours ago -
Firefox 139
by
Charlie
2 days, 20 hours ago -
Who knows what?
by
Will Fastie
1 day, 22 hours ago -
My top ten underappreciated features in Office
by
Peter Deegan
1 hour, 15 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
1 day, 8 hours ago -
Misbehaving devices
by
Susan Bradley
2 days, 10 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.