MS Office Forum / Word / Programming / May 2006
Automatic generation of template files
|
|
Thread rating:  |
Nathan_Ekstrom - 27 Apr 2006 22:15 GMT I have 1,000 images that each need to be set as a background image for a Word template file probably as a watermark or background image. Is there a way I can do this programmatically instead of doing each on by hand?
Helmut Weber - 28 Apr 2006 09:43 GMT Hi Nathan,
> I have 1,000 images that each need to be set as a background image for a Word > template file probably as a watermark or background image. Is there a way I > can do this programmatically instead of doing each on by hand? Yes, in theory. In reality, IMHO, this would require a lot of testing and error-handling. The problem isn't so much how to do it, but what to do, if an error occurs. And there may be lots of different erros.
Do You really want to create 1000 different templates, each with a different picture as watermark?
Do you have a list with the names of the 1000 pictures? Without typos, of course. What names should the templates have?
 Signature Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA "red.sys" & chr(64) & "t-online.de" Word 2002, Windows 2000
Nathan_Ekstrom - 28 Apr 2006 14:19 GMT > Hi Nathan, > [quoted text clipped - 10 lines] > Do You really want to create 1000 different templates, > each with a different picture as watermark? My company is basically creating online stationary. You come to our site and either pay a subscription or buy a specific stationary page. So yes I need 1,000 different files. I already have all of the images and could just sell those but we would prefer to provide a word template file for people to download so they didn't have to do it themselves.
I don't really care if the image is set as the watermark or as the background. I just noticed in my personal testing that it gets sized better as a watermark. If you know how to size the background image that would be great also.
> Do you have a list with the names of the 1000 pictures? > Without typos, of course. > What names should the templates have? I can generate a list of input and output filenames without any difficulty and without typos. The name of the templat file would just be the same as the image except with .dot on the end instead of .jpg. Thanks for any help and thanks for replying.
Helmut Weber - 28 Apr 2006 20:38 GMT Hi Nathan,
for creating x dots from x jpgs, where all jpgs are in the same folder, and all the dots in another folder:
Sub Test0796() Dim sPth As String ' Source Path Dim tPth As String ' Target Path Dim sFil As String ' Source File Dim tFil As String ' Target File sPth = "c:\test\jpg\" tPth = "c:\test\dot\" sFil = Dir(sPth & "*.jpg", vbNormal) While sFil <> "" tFil = sFil tFil = left(tFil, Len(tFil) - 3) tFil = tFil & "dot" With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) .Shapes.AddPicture _ FileName:=sPth & sFil, _ LinkToFile:=False, _ SaveWithDocument:=True End With ActiveDocument.SaveAs tPth & tFil sFil = Dir Wend
End Sub
>If you know how to size the background image >that would be great also. Sizing is not a problem, but to what values?
Like, add these lines:
.Shapes(1).LockAspectRatio = msoFalse .Shapes(1).Height = CentimetersToPoints(25) ' etc
Analyzing the jpg beforehand and acting according to the results would be a not too small project, if possible at all.
If the picture is too small, how to enlarge? What about distortion? etc....
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Nathan_Ekstrom - 29 Apr 2006 00:50 GMT > Hi Nathan, > [quoted text clipped - 43 lines] > If the picture is too small, how to enlarge? > What about distortion? etc.... Thanks for your help. Thankfully all of the pictures are the same size. One more question though. I'm assuming the code is a VB script. Can I just create it in the Visual Basic Editor and run it? Or do I do something else? Thanks again for your help.
Nathan
Nathan_Ekstrom - 29 Apr 2006 01:04 GMT > > Hi Nathan, > > [quoted text clipped - 50 lines] > > Nathan Yep that seems to work. I just run it in the VB editor in word. Thanks again Helmut.
Nathan
Nathan_Ekstrom - 29 Apr 2006 01:17 GMT > > > Hi Nathan, > > > [quoted text clipped - 55 lines] > > Nathan I've run into another problem. I need the images to fill the enire page. They are sized to 8.5x11 inches with 200x200 dpi so they should fit right, at least in the US. Currently the script has the image scaled down to fit in the margins. How do I make it fill the entire page?
Nathan
Helmut Weber - 29 Apr 2006 10:07 GMT Hi Nathan,
software development is a process of trial and error. More or less, of course.
With starting from a blank document, inserting a picture, saving as, and repeating all, a new picture would be _added_ to the header with each run.
So deleting the inserted picture is an essential step, which I didn't think of, at first.
Have a close look at the section, which sizes and places the picture.
This one seems to work quite right.
Though I'd strongly advise, not to try to create all 1000 dots in one go.
Sub Test0796()
Dim sPth As String ' Source Path Dim tPth As String ' Target Path Dim sFil As String ' Source File Dim tFil As String ' Target File sPth = "c:\test\jpg\" tPth = "c:\test\dot\" sFil = Dir(sPth & "*.jpg", vbNormal) While sFil <> "" tFil = sFil tFil = Left(tFil, Len(tFil) - 3) tFil = tFil & "dot" With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) .Shapes.AddPicture _ FileName:=sPth & sFil, _ LinkToFile:=False, _ SaveWithDocument:=True .Shapes(1).LockAspectRatio = msoFalse .Shapes(1).RelativeHorizontalPosition = wdRelativeHorizontalPositionPage .Shapes(1).RelativeVerticalPosition = wdRelativeVerticalPositionPage .Shapes(1).Height = InchesToPoints(11) .Shapes(1).Width = InchesToPoints(8.5) .Shapes(1).Top = InchesToPoints(0) .Shapes(1).Left = InchesToPoints(0) ActiveDocument.SaveAs tPth & tFil .Shapes(1).Delete End With sFil = Dir Wend
End Sub
Beware of linebrakes by the newsreader.
HTH
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Helmut Weber - 01 May 2006 08:54 GMT hmm...
to define the filetype, seems like a good idea, too.
Like:
ActiveDocument.SaveAs _ FileName:= StringVariable, _ FileFormat:=wdFormatTemplate
Helmut Weber
Jonathan West - 28 Apr 2006 11:20 GMT >I have 1,000 images that each need to be set as a background image for a >Word > template file probably as a watermark or background image. Is there a way > I > can do this programmatically instead of doing each on by hand? I would take a different approach. Have a single template file, and have a macro that displays the Insert Picture dialog when the user creates a new document based on the template, and the user then gets to choose which of the 1000 images he will use for that particular document. Then the code can insert the image behind text in the header so that it appears on every page.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
Nathan_Ekstrom - 28 Apr 2006 14:22 GMT > >I have 1,000 images that each need to be set as a background image for a > >Word [quoted text clipped - 7 lines] > the 1000 images he will use for that particular document. Then the code can > insert the image behind text in the header so that it appears on every page. Thanks for the reply but sadly that won't work for us because we will be selling each template file individually so the users won't actually have the pictures handy. Instead they will be able to download them off of our site and put them into their documents instead of buying fancy stationary paper. We would prefer to provide them template files however instead of forcing them to download and set the picture themselves.
|
|
|