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, 8 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
-
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
6 hours, 51 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
7 hours, 58 minutes ago -
0Patch, where to begin
by
cassel23
2 hours ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
21 hours, 35 minutes ago -
89 million Steam account details just got leaked,
by
Alex5723
9 hours, 21 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 6 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
21 hours, 7 minutes ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
8 hours, 15 minutes ago -
Installer program can’t read my registry
by
Peobody
3 hours, 12 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
18 hours, 54 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 2 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 17 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 2 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 3 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 7 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 8 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 12 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 17 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 17 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
14 hours, 58 minutes ago -
May 2025 updates are out
by
Susan Bradley
32 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
2 days, 23 hours ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
2 days, 23 hours ago -
Drivers suggested via Windows Update
by
Tex265
2 days, 23 hours ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
19 hours, 34 minutes ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
3 days, 6 hours ago -
Apple releases 18.5
by
Susan Bradley
3 days ago -
Fedora Linux 40 will go end of life for updates and support on 2025-05-13.
by
Alex5723
3 days, 7 hours ago -
How a new type of AI is helping police skirt facial recognition bans
by
Alex5723
3 days, 8 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.