Craig Johnson
asked on
Word Macro -- Batch Load Several Hundred PNG files in a Word Document
Hey Guys....This is a Tough Question!
I'm trying to develop a Word Macro that will automatically load several hundrend PNG files into a single document. I only want one PNG file (chart image) per page and the I need the capability to specify in the Macro the exact size and placement of the graphic on the word page.
Also, would like to have a user prompt that would allow me to specific what month to show in the page heading. Something like "My Publication Title (trade mark symbol) | Month, Year". Then a horizontal line below the title and a horizontal line at the bottom footer.
Ideally, I would like to see the PNG graphic (stock chart) place just slightly below the top horizontal line.
If possible the Macro would create a new document file each time it is run.
Bottom line Goal--Load several hundred stock charts into Word so that I can mark on them up via Words inking capabilities.
Thanks for HELP.....I know this is a tough one!
CWJ
I'm trying to develop a Word Macro that will automatically load several hundrend PNG files into a single document. I only want one PNG file (chart image) per page and the I need the capability to specify in the Macro the exact size and placement of the graphic on the word page.
Also, would like to have a user prompt that would allow me to specific what month to show in the page heading. Something like "My Publication Title (trade mark symbol) | Month, Year". Then a horizontal line below the title and a horizontal line at the bottom footer.
Ideally, I would like to see the PNG graphic (stock chart) place just slightly below the top horizontal line.
If possible the Macro would create a new document file each time it is run.
Bottom line Goal--Load several hundred stock charts into Word so that I can mark on them up via Words inking capabilities.
Thanks for HELP.....I know this is a tough one!
CWJ
Do you need a prompt for each graphic, or will the heading be the same for the whole document?
When you say page heading, do you mean Header?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If the names of the png files can be consistent and predictable, you can build a document using the INSERTPICTURE field to load each one. (Make one page the way you want it, copy it, and edit the link; repeat for as many as you need -- or use a macro to automate the name changing.) When you have the document set up the way you want it the first time, all you need to do from then on is replace the images with a new one with the same name. Word will link to the file by name so it will load the new images in.
If you have control over the png file names, choose a name pattern that makes it easier to edit within the fields. If not, consider building the field codes from a list of the file names. If the png files change each time, a different solution might be better -- but in any case, avoid embedding so many images into a Word document by using the "Insert as link" feature (which brings the image in via the field code).
If you have control over the png file names, choose a name pattern that makes it easier to edit within the fields. If not, consider building the field codes from a list of the file names. If the png files change each time, a different solution might be better -- but in any case, avoid embedding so many images into a Word document by using the "Insert as link" feature (which brings the image in via the field code).
Nice Graham! Your post went up while I was putting my comment together! Can your code be tweaked to insert the images as links instead? A Word document with "hundreds" of images embedded within it can become gigantic and pretty awkward to work with...
Yes Eric. The 'False' in this line refers to the LinkToFile parameter, so it could well be changed to suit the situation.
doc.Shapes.AddPicture strFolder & "\" & strFile, False, True _
, PictureTop, PictureLeft, , , doc.Paragraphs.Last.Range
doc.Shapes.AddPicture strFolder & "\" & strFile, False, True _
, PictureTop, PictureLeft, , , doc.Paragraphs.Last.Range
ASKER
Follow-up....
1) Don't want it to prompt for each image
2) PNG extenstion
3) Heading = Header
4) Pictures names WILL change evertime.....picture folder will also change so we need to be prompted for the correct directory
Thanks
CWJ
1) Don't want it to prompt for each image
2) PNG extenstion
3) Heading = Header
4) Pictures names WILL change evertime.....picture folder will also change so we need to be prompted for the correct directory
Thanks
CWJ
ASKER
Would it also be possible to crop the picture in addition to sizing it on the page?
CWJ
CWJ
Yes. In fact I omitted to demonstrate the resizing, so this code resizes to a width of 15 centimeters after trimming off
1 cm all round.
1 cm all round.
This is the new code:
Sub AddGraphics()
Dim strFile As String
Dim doc As Document
Dim c As Integer
Dim rng As Range
Dim hdr As HeaderFooter
Dim ftr As HeaderFooter
Dim HdrLineLeft As Single
Dim HdrLineTop As Single
Dim HdrLineWidth As Single
Dim FtrLineLeft As Single
Dim FtrLineTop As Single
Dim FtrLineWidth As Single
Dim PictureLeft As Single
Dim PictureTop As Single
Dim strDate As String
Dim dt As Date
Dim sh As Shape
Dim sGraphicWidth As Single
HdrLineLeft = CentimetersToPoints(1)
HdrLineTop = CentimetersToPoints(1)
HdrLineWidth = CentimetersToPoints(15)
FtrLineLeft = CentimetersToPoints(1.5)
FtrLineTop = CentimetersToPoints(1.5)
FtrLineWidth = CentimetersToPoints(15.5)
PictureTop = CentimetersToPoints(1.5)
PictureLeft = CentimetersToPoints(1.5)
Const strFolder = "G:\My Pictures\Photos\127"
sGraphicWidth = CentimetersToPoints(15)
Set doc = Documents.Add
strFile = Dir$(strFolder & "\*.jpg")
Set hdr = doc.Sections(1).Headers(wd HeaderFoot erPrimary)
Do
strDate = InputBox("Please give month and year")
Loop Until IsDate("1 " & strDate)
dt = CDate("1 " & strDate)
hdr.Range.Text = "My Publication Title (™) | " & Format(dt, "MMMM, YYYY")
hdr.Shapes.AddLine HdrLineLeft, HdrLineTop, HdrLineLeft + HdrLineWidth, HdrLineTop, hdr.Range.Paragraphs.Last. Range
Set ftr = doc.Sections(1).Footers(wd HeaderFoot erPrimary)
ftr.Shapes.AddLine FtrLineLeft, FtrLineTop, FtrLineLeft + FtrLineWidth, FtrLineTop, ftr.Range.Paragraphs.Last. Range
Do Until strFile = ""
If c > 0 Then
doc.Bookmarks("\EndOfDoc") .Range.Ins ertBreak wdPageBreak
End If
doc.Bookmarks("\EndOfDoc") .Range.Ins ertParagra ph
Set sh = doc.Shapes.AddPicture(strF older & "\" & strFile, False, True, _
PictureTop, PictureLeft, , , doc.Paragraphs.Last.Range)
sh.PictureFormat.CropBotto m = CentimetersToPoints(1)
sh.PictureFormat.CropTop = CentimetersToPoints(1)
sh.PictureFormat.CropLeft = CentimetersToPoints(1)
sh.PictureFormat.CropRight = CentimetersToPoints(1)
sh.LockAspectRatio = True
sh.Width = sGraphicWidth
strFile = Dir$()
c = c + 1
Loop
End Sub
Sub AddGraphics()
Dim strFile As String
Dim doc As Document
Dim c As Integer
Dim rng As Range
Dim hdr As HeaderFooter
Dim ftr As HeaderFooter
Dim HdrLineLeft As Single
Dim HdrLineTop As Single
Dim HdrLineWidth As Single
Dim FtrLineLeft As Single
Dim FtrLineTop As Single
Dim FtrLineWidth As Single
Dim PictureLeft As Single
Dim PictureTop As Single
Dim strDate As String
Dim dt As Date
Dim sh As Shape
Dim sGraphicWidth As Single
HdrLineLeft = CentimetersToPoints(1)
HdrLineTop = CentimetersToPoints(1)
HdrLineWidth = CentimetersToPoints(15)
FtrLineLeft = CentimetersToPoints(1.5)
FtrLineTop = CentimetersToPoints(1.5)
FtrLineWidth = CentimetersToPoints(15.5)
PictureTop = CentimetersToPoints(1.5)
PictureLeft = CentimetersToPoints(1.5)
Const strFolder = "G:\My Pictures\Photos\127"
sGraphicWidth = CentimetersToPoints(15)
Set doc = Documents.Add
strFile = Dir$(strFolder & "\*.jpg")
Set hdr = doc.Sections(1).Headers(wd
Do
strDate = InputBox("Please give month and year")
Loop Until IsDate("1 " & strDate)
dt = CDate("1 " & strDate)
hdr.Range.Text = "My Publication Title (™) | " & Format(dt, "MMMM, YYYY")
hdr.Shapes.AddLine HdrLineLeft, HdrLineTop, HdrLineLeft + HdrLineWidth, HdrLineTop, hdr.Range.Paragraphs.Last.
Set ftr = doc.Sections(1).Footers(wd
ftr.Shapes.AddLine FtrLineLeft, FtrLineTop, FtrLineLeft + FtrLineWidth, FtrLineTop, ftr.Range.Paragraphs.Last.
Do Until strFile = ""
If c > 0 Then
doc.Bookmarks("\EndOfDoc")
End If
doc.Bookmarks("\EndOfDoc")
Set sh = doc.Shapes.AddPicture(strF
PictureTop, PictureLeft, , , doc.Paragraphs.Last.Range)
sh.PictureFormat.CropBotto
sh.PictureFormat.CropTop = CentimetersToPoints(1)
sh.PictureFormat.CropLeft = CentimetersToPoints(1)
sh.PictureFormat.CropRight
sh.LockAspectRatio = True
sh.Width = sGraphicWidth
strFile = Dir$()
c = c + 1
Loop
End Sub
ASKER
Nice Work! However, can there be prompt for the picture (PNG) folder...browse?
Thanks
CWJ
Thanks
CWJ
ASKER
GrahamSkan--
This is great! Couple of follow up questions.
1) Can the publication title be centered? The title should be bold, but not the date.....
2) Can the font be changed to Garmond and 11 point?
3) Can the Date "March 2006" be in italic?
4) I'm having trouble setting the top/bottom line pages to 7.5 inchs accross...any ideas?
5) From the top of the page to the bottom of the first header line....it should be no more than 1/2 inch....the bottom line should be 1/2 inch off the bottom.
6) The placement of the picture should be 1/16th below the top header line...
7) The picture should be 1/8 the inside each side of the headerline
8) The picture its self should be 5 1/2 inches tall
Sorry for all the detail....but the exact specifications of placing these pictures on the page is extremely exact and outside my knowlege level of VBA (at this point).
Thanks...for all your help!
CWJ
This is great! Couple of follow up questions.
1) Can the publication title be centered? The title should be bold, but not the date.....
2) Can the font be changed to Garmond and 11 point?
3) Can the Date "March 2006" be in italic?
4) I'm having trouble setting the top/bottom line pages to 7.5 inchs accross...any ideas?
5) From the top of the page to the bottom of the first header line....it should be no more than 1/2 inch....the bottom line should be 1/2 inch off the bottom.
6) The placement of the picture should be 1/16th below the top header line...
7) The picture should be 1/8 the inside each side of the headerline
8) The picture its self should be 5 1/2 inches tall
Sorry for all the detail....but the exact specifications of placing these pictures on the page is extremely exact and outside my knowlege level of VBA (at this point).
Thanks...for all your help!
CWJ
ASKER
One more follow-up...
The publication title is font: ScalaSANS - bold 10 point
The publication data is SabonMT - Italic 10 point
CWJ
The publication title is font: ScalaSANS - bold 10 point
The publication data is SabonMT - Italic 10 point
CWJ
What title, what font?
All the code that I gave you was to deal with graphics. If there is text in a graphical form, then that cannot be treated within Word. As far as Word is concerned, that is just a picture. If you want graphical text to be interpreted as text, you have to invoke some sort of OCR programme.
I'll take a bit more time to look at the graphical placing and sizing requests
All the code that I gave you was to deal with graphics. If there is text in a graphical form, then that cannot be treated within Word. As far as Word is concerned, that is just a picture. If you want graphical text to be interpreted as text, you have to invoke some sort of OCR programme.
I'll take a bit more time to look at the graphical placing and sizing requests
ASKER
GrahamSkan--
Sorry for the confussion...the macro prompts for the date. In that prompt we type in the date and that is the combined with the title of the publication. I was wondering if that page header could be specifcally defined with the above mentioned fonts, sizes and centered on the page?
Thanks!
CWJ
Sorry for the confussion...the macro prompts for the date. In that prompt we type in the date and that is the combined with the title of the publication. I was wondering if that page header could be specifcally defined with the above mentioned fonts, sizes and centered on the page?
Thanks!
CWJ
ASKER
Hey Graham...any luck on tweeking the placement of the graphs?