Inserting an Image and Text into Word Using Excel VBA

KMacattack

New Member
Joined
Nov 24, 2008
Messages
11
I am trying to paste a couple cell from excel to word as an image and then insert text underneath the image. The only way that I have been able to get text underneath the image is to use a bunch of line breaks. Is there a way to find the point after the image, it seems like the cursor stays at the top of the page when inserting the image though.


Rich (BB code):
Sub CopyWorksheetsToWord<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
'This will copy the cell to word as a enhanced metafile.<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
Dim WdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet<o:p></o:p>
<o:p></o:p>
'Creates a New Microsoft Word Document<o:p></o:p>
Application.StatusBar = "Creating new document..."<o:p></o:p>
Set WdApp = New Word.Application<o:p></o:p>
Set wdDoc = WdApp.Documents.Add<o:p></o:p>
<o:p></o:p>
'Copies The ranges and will paste into the crated microsoft word document<o:p></o:p>
Set ws = ActiveSheet<o:p></o:p>
        Application.StatusBar = "Copying data from " & ws.Name & "..."<o:p></o:p>
        ws.Range("B4:H27").Copy<o:p></o:p>
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.PasteSpecial DataType:=wdPasteEnhancedMetafile<o:p></o:p>
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter<o:p></o:p>
    <o:p></o:p>
'Inserts Text into the Word Doc for the particular Points for discussion.<o:p></o:p>
    wdDoc.Content.InsertAfter  Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13) & Chr(13)
    wdDoc.Content.InsertAfter  "My Text Here"
<o:p></o:p>
Set ws = Nothing<o:p></o:p>
Application.StatusBar = "Cleaning up..."
<o:p></o:p>
'Sets the Word Doc view to Print View and Zoom 85%<o:p></o:p>
WdApp.ActiveWindow.ActivePane.Zooms(wdPrintView).Percentage = 85<o:p></o:p>
WdApp.Visible = False<o:p></o:p>
        <o:p></o:p>
Set wdDoc = Nothing<o:p></o:p>
Set WdApp = Nothing<o:p></o:p>
Application.StatusBar = False
 
Last edited:
vladimir,

Okayyy! This is getting to be very good stuff! I noticed that, if the field# I chose it to be put in was field01, the pasted picture would actually go OVER field02 (yes, by design, I know that), so, to overcome that, I put field01 by itself into a table, re-sized the table, and the code correctly put in the pasted picture into Word in the table, all while NOT covering over field02!

This is a BIG step :)

Now, a couple of things here


1. Can you modify the code so that it takes all the totals from column b here and puts them into their respective "FIELD" position in the Word document?

Here's sample data...


<TABLE style="WIDTH: 423pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=563 x:str><COLGROUP><COL style="WIDTH: 72pt; mso-width-source: userset; mso-width-alt: 3413" width=96><COL style="WIDTH: 178pt; mso-width-source: userset; mso-width-alt: 8419" width=237><COL style="WIDTH: 173pt; mso-width-source: userset; mso-width-alt: 8192" width=230><TBODY><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl25 height=18 width=96>COLUMN A</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl25 width=237>COLUMN B</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl25 width=230>COLUMN C</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD#</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237>Total to put into Word Doc</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>What worksheet is it located in</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD01</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>10</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD02</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>25</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD03</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>66</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD04</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>133</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD05</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>200</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD06</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>150</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD07</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>250</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD08</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237>Formatted range of sheet 1</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>sheet1</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD09</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>1255</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD10</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>500</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD11</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>5200</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD12</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>112</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD13</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237>Formatted range of sheet 2</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>sheet2</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD14</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>1</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD15</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>3</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 72pt; HEIGHT: 13.8pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl24 height=18 width=96>FIELD16</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 178pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=237 x:num>89</TD><TD style="BORDER-BOTTOM: #e0dfe3; BORDER-LEFT: #e0dfe3; BACKGROUND-COLOR: transparent; WIDTH: 173pt; BORDER-TOP: #e0dfe3; BORDER-RIGHT: #e0dfe3" class=xl26 width=230>Totals</TD></TR></TBODY></TABLE>


Also, can the code be modified to paste either a range or cells OR A CHART?
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What, once again? ;)
You’ve written that single cells should not be copied, but just the group one. Please be more consistent in you task.

Ok,
For changing of single destination FIELD# just change this line of code:
Const FieldName = "FIELD01" ' <-- Change to suit
This line was written specially for such purpose.

Any type of selectable object can be copied by the code.
Try to select cells, graphs, auto-shapes etc to test that it’s true.

According to your new (or old?) task - it’s not clear for me in what cells (its exact addresses) the sample data are stored. Is it A2:C17 of what sheet?

What do the values of column B mean?
If it is the values which have to be copied individually to the mentioned fields, then for what the data in column C are?

What means "Formatted range of sheet 1"?
How code should recognize it?

Why not use the named ranges at all for all source ranges? In this case sheets list of column C is not required.

Wish you become VbaPro soon, :)
Vladimir
 
Last edited:
Upvote 0
Hi and sorry for the confusion,

Well, I want both.

I want both single values from column B and range values from other worksheets to go into their respective field in Word.

For example, the value of "10" in Column B should populate into "FIELD01" in the Word document. I know this because, to the left, it says "FIELD01". These "Totals" in Excel are populated from other code, so what I'm left with is what you see. Field position in column A, the value I want copied in Column B, and the worksheet the data is on in column C.

So here's more detail...


The value of Row 2b ("10") from Excel to FIELD01 in Word
The value of Row 3b ("25") from Excel to FIELD02 in Word
Keep going for all the rest of the values.


Basically, I'm trying to combine your awesome code to paste a formatted Excel range as a picture along with this code here...

Look at FIELD07 below, that is exactly what I am looking to do.


Code:
.FormFields("FIELD01").Result = ws.Range("E01").Value
.FormFields("FIELD02").Result = ws.Range("E02").Value
.FormFields("FIELD03").Result = ws.Range("E03").Value
.FormFields("FIELD04").Result = ws.Range("E04").Value
.FormFields("FIELD05").Result = ws.Range("E05").Value
.FormFields("FIELD06").Result = ws.Range("E06").Value
[COLOR=darkred][B].FormFields("FIELD07").Result = Formattedrange of sheet2[/B][/COLOR]
.FormFields("FIELD08").Result = ws.Range("E08").Value
.FormFields("FIELD09").Result = ws.Range("E09").Value
.FormFields("FIELD10").Result = ws.Range("E10").Value

So that I can have one sub that will allow me to change the value of all of these fields.

Also, I did try to paste a chart, but that didn't work, the formatted range again, DID work though. Sorry for the confusion. I hope that helps.
 
Last edited:
Upvote 0
Ok, but I don’t understand your task exactly.

For me you haven’t answered my questions:
What is Formattedrange? – nothing said.
Why not named cells/ranges? - the same,
etc.

And for me there are some inconsistent in explanations, for example in this:
The value of Row 2b ("10") from Excel to FIELD01 in Word
and in that info:
.FormFields("FIELD01").Result = ws.Range("E01").Value
B2 is not E01 !
2b is B2 cell?
For what are the round brackets and quotes in Row 2b ("10")?
We haven’t waste a lot of time & the Board’s spaces on such trifles :)

So, now please let’s follow to my rules.
Firstly, we have to fill the form of requirements:

1. The aim is in copying of the picture of individual Excel cells over the specified text fields of Word template (specify another aim if it’s different).

2. There are ________(count) of fields in the Word form named as FIELD01, FIELD02 and so on.

3. All cells, which shapes (pictures) would be copied to Word Form, are in the workbook _____________(specify its name) in worksheet _____________(specify sheet’s name)

4. Specification of Fields and its referenced cells is in the ________(same?) workbook, in the sheet ___________(specify sheets name) on range _____________(specify like A2:B17), which 1st column cells stays for Field’s names, and the individual cells in the same row of the 2nd column would be copied to the form as pictures as follows:
Field01 of Word form would be replaced by the shape (picture) of ______(B2?) cell
Field02 - by the shape (picture) of _______(B3?) cell
And so on.

Secondly, we will try to realize that task.
And nothing else in this thread, please - it's huge enough.

As to the copy of diagram issue:
../ I did try to paste a chart, but that didn't work, the formatted range again, DID work though ..
You frighten me ;) - it really works, try example in attached archive: FromExcelToWordForm_03.zip

Vladimir
 
Last edited:
Upvote 0
vladimir,

Ahh, I'm really sorry, I HATE being a newbie!

Okay, I'm going to really spend some time on this and get back to you hopefully today, but by Wednesday at the latest. I'm going to try and use that upload link you gave me and provide you with a link to a working Word doc and Workbook from there.

For now though, "formatted range" means copy the range from a particular worksheet (including formatting) and paste it as a picture into a certain field in the Word document. So, formatted range means, copy the range, including formatting.

And my quote..

The value of Row 2b ("10") from Excel to FIELD01 in Word...

should've said...

The value of Row B02 ("10") from Excel to FIELD02 in Word

<TABLE cellSpacing=0 cellPadding=6 width="100%" border=0><TBODY><TR><TD class=alt2 style="BORDER-RIGHT: 1px inset; BORDER-TOP: 1px inset; BORDER-LEFT: 1px inset; BORDER-BOTTOM: 1px inset">.FormFields("FIELD02").Result = ws.Range("B02").Value </TD></TR></TBODY></TABLE>


"10" is from the chart from 2 posts ago..

<TABLE style="WIDTH: 423pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=563 border=0 x:str><TBODY><TR style="HEIGHT: 13.8pt" height=18><TD class=xl25 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 72pt; BORDER-TOP-COLOR: #e0dfe3; HEIGHT: 13.8pt; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=96 height=18>COLUMN A</TD><TD class=xl25 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 178pt; BORDER-TOP-COLOR: #e0dfe3; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=237>COLUMN B</TD><TD class=xl25 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 173pt; BORDER-TOP-COLOR: #e0dfe3; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=230>COLUMN C</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD class=xl24 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 72pt; BORDER-TOP-COLOR: #e0dfe3; HEIGHT: 13.8pt; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=96 height=18>FIELD#</TD><TD class=xl26 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 178pt; BORDER-TOP-COLOR: #e0dfe3; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=237>Total to put into Word Doc</TD><TD class=xl26 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 173pt; BORDER-TOP-COLOR: #e0dfe3; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=230>What worksheet is it located in</TD></TR><TR style="HEIGHT: 13.8pt" height=18><TD class=xl24 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 72pt; BORDER-TOP-COLOR: #e0dfe3; HEIGHT: 13.8pt; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=96 height=18>FIELD01</TD><TD class=xl26 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 178pt; BORDER-TOP-COLOR: #e0dfe3; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=237 x:num>10</TD><TD class=xl26 style="BORDER-LEFT-COLOR: #e0dfe3; BORDER-BOTTOM-COLOR: #e0dfe3; WIDTH: 173pt; BORDER-TOP-COLOR: #e0dfe3; BACKGROUND-COLOR: transparent; BORDER-RIGHT-COLOR: #e0dfe3" width=230>Totals</TD></TR></TBODY></TABLE>

I'll have more for you as soon as I can really make this all clear. I will make sure that I answer all of your questions fully as well. I think the Word doc and workbook will do most of that though.

Thanks soooo much :)
 
Upvote 0
Hello Vladimir,

Unbelievable! After some loonnng hard work, I got this puppy to work!

Thanks Soooo much for that hard work and helping me out. I was really getting frustrated there but my persistence (and your patience :) ) paid off!

You are the man!

You, Tom, Jbeaucaire, Peter, Norie (and anyone else I forgot)...You guys (and girls?) are all awesome! I'm so jealous!

I friggin LOVE VBA!

Thanks again Vladimir! :p:cool::biggrin:
 
Upvote 0
Good news VBAProIWish!

Hard work with VBA can save you much free time in future ;)
Word VBA is new for me too but due to your questions I've acquired some new knowledge, so thanks for that. If you will have issues in any variants of this thread code just PM me, and I shall try to help.

Best regards,
Vladimir
 
Upvote 0
Vladimir or ZVI,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Are you still subscribed to this post, hope so for I have a similar problem:<o:p></o:p>
<o:p></o:p>
I have codes much like FIELDxx in Excel as follows<o:p></o:p>
<o:p></o:p>
Pictures are in the "ThisWorkbook.path \ images" directory and are all .jpg (say D:\ASE\Images\CDlarge.jpg, etc)<o:p></o:p>
<o:p></o:p>
Code Picture name<o:p></o:p>
Field01 CDlarge<o:p></o:p>
Field02 Excel<o:p></o:p>
etc<o:p></o:p>
<o:p></o:p>
I would place corresponding codes in Word Tables (as a TEXT field?). I will probably use 30-40 codes like that. It is a huge document.<o:p></o:p>
<o:p></o:p>
Then going through all the Excel definitions reading code and picture name and finally place the jpg in Word.<o:p></o:p>
When completed, I will need to remove UNUSED codes in word. Using Text Replace method.<o:p></o:p>
<o:p></o:p>
Can your code be used for that? Right now it uses "Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture". Based on your Aug-30-2009 post #29 (page 3). <o:p></o:p>
<o:p></o:p>
Would appreciate your help<o:p></o:p>

Henry
 
Upvote 0
Hi Henry,
Try:
Rich (BB code):

' ZVI:2010-09-17 http://www.mrexcel.com/forum/showthread.php?t=355225
' Henry post#38: http://www.mrexcel.com/forum/showpost.php?p=2444901&postcount=38
Sub FromExcelToWordForm_05()
  
  Dim objWord As Object, objDoc As Object, TemplateFolder$, PicturesFolder, x
  
  ' --> User settings, change to suit
  Const TemplateFile = "MyTemplate.dot"           ' Template file name
  Const FieldName = "FIELD"                       ' The first 5 symbols of bookmark name
  Const TopCodeCell = "A2"                        ' The topmost cell address with "FIELDnn" code
  TemplateFolder = ThisWorkbook.Path              ' The folder with Template file
  PicturesFolder = ThisWorkbook.Path & "\Images"  ' The folder with JPG files
  ' <-- End of user settings
  
  ' Fix folders names
  If Right(TemplateFolder, 1) <> "\" Then TemplateFolder = TemplateFolder & "\"
  If Right(PicturesFolder, 1) <> "\" Then PicturesFolder = PicturesFolder & "\"
  
  ' Open the template file
  On Error Resume Next
  Set objWord = GetObject(, "Word.Application")
  If Err <> 0 Then Set objWord = CreateObject("Word.Application"): Err.Clear
  Set objDoc = objWord.Documents.Open(TemplateFolder & TemplateFile, , True)
  If Err <> 0 Then MsgBox "Can't open: " & TemplateFolder & TemplateFile, vbExclamation: GoTo exit_
  
  ' Activate Microsoft Word window
  objWord.Visible = True
  objWord.Tasks("Microsoft Word").Activate
  
  ' Insert JPG pictures, listed in Excel range, onto "FIELDnn" bookmarks
  With objDoc
    ' Place the pictures at "FIELDnn" bookmarks positions
    For Each x In Range("A2", Cells(Rows.Count, Left(TopCodeCell, 1)).End(xlUp))
      .InlineShapes.AddPicture PicturesFolder & x.Offset(, 1) & ".jpg", False, True, .FormFields(x).Range
    Next
    ' Delete all "FIELD*" like bookmarks
    For Each x In .FormFields
      If UCase(Left(x.Name, 5)) = "FIELD" Then x.Delete
    Next
  End With
  
  ' Trap Word error
  If Err <> 0 Then MsgBox Err.Description, vbCritical, "Error": GoTo exit_
  
  ' SaveAs DOC file to release the template, add date-time stamp to the filename
  objDoc.SaveAs Filename:=Replace(UCase(TemplateFolder & TemplateFile), ".DOT", Format(Now, "_yyyy-mm-dd_hh-mm-ss") & ".DOC"), FileFormat:=0
  
exit_:
  ' Release the memory
  Set objDoc = Nothing
  Set objWord = Nothing
  
End Sub
Regards,
 
Last edited:
Upvote 0
Amended:
Rich (BB code):

' ZVI:2010-09-17 http://www.mrexcel.com/forum/showthread.php?t=355225
' Henry post#38: http://www.mrexcel.com/forum/showpost.php?p=2444901&postcount=38
Sub FromExcelToWordForm_05()
  
  Dim objWord As Object, objDoc As Object, TemplateFolder$, PicturesFolder, x
  
  ' --> User settings, change to suit
  Const TemplateFile = "MyTemplate.dot"           ' Template file name
  Const FieldName = "FIELD"                       ' The first 5 symbols of bookmark name
  Const TopCodeCell = "A2"                        ' The topmost cell address with "FIELDnn" code
  TemplateFolder = ThisWorkbook.Path              ' The folder with Template file
  PicturesFolder = ThisWorkbook.Path & "\Images"  ' The folder with JPG files
  ' <-- End of user settings
  
  ' Fix folders names
  If Right(TemplateFolder, 1) <> "\" Then TemplateFolder = TemplateFolder & "\"
  If Right(PicturesFolder, 1) <> "\" Then PicturesFolder = PicturesFolder & "\"
  
  ' Open the template file
  On Error Resume Next
  Set objWord = GetObject(, "Word.Application")
  If Err <> 0 Then Set objWord = CreateObject("Word.Application"): Err.Clear
  Set objDoc = objWord.Documents.Open(TemplateFolder & TemplateFile, , True)
  If Err <> 0 Then MsgBox "Can't open: " & TemplateFolder & TemplateFile, vbExclamation: GoTo exit_
  
  ' Activate Microsoft Word window
  objWord.Visible = True
  objWord.Tasks("Microsoft Word").Activate
  
  ' Insert JPG pictures, listed in Excel range, onto "FIELDnn" bookmarks
  With objDoc
    ' Place the pictures at "FIELDnn" bookmarks positions
    For Each x In Range(TopCodeCell, Cells(Rows.Count, Left(TopCodeCell, 1)).End(xlUp))
      .InlineShapes.AddPicture PicturesFolder & x.Offset(, 1) & ".jpg", False, True, .FormFields(x).Range
    Next
    ' Delete all "FIELD*" like bookmarks
    For Each x In .FormFields
      If UCase(Left(x.Name, 5)) = "FIELD" Then x.Delete
    Next
  End With
  
  ' Trap Word error
  If Err <> 0 Then MsgBox Err.Description, vbCritical, "Error": GoTo exit_
  
  ' SaveAs DOC file to release the template, add date-time stamp to the filename
  objDoc.SaveAs Filename:=Replace(UCase(TemplateFolder & TemplateFile), ".DOT", Format(Now, "_yyyy-mm-dd_hh-mm-ss") & ".DOC"), FileFormat:=0
  
exit_:
  ' Release the memory
  Set objDoc = Nothing
  Set objWord = Nothing
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,792
Messages
6,161,997
Members
451,735
Latest member
Deasejm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top