Automate stuffing jpgs into Word, saving and exiting

P

Pete Dixon

Hi all,

To sum up, I'm just trying to automate stuffing jpgs into empty
documents, saving them and exiting.

I'm trying to create 3 documents ( templates?) where each executes a
macro on start-up that inserts a picture ( always the same name) and
saves the resulting document in a specific directory, again with a set
name. Word then should automatically close.

E.g.

rptcomp1.doc inserts comp1.jpg saves as c:\return\comp1.doc
rptcomp2.doc inserts comp2.jpg saves as c:\return\comp2.doc
rptcomp3.doc inserts comp3.jpg saves as c:\return\comp3.doc

The user opening one of these rptcomp documents would just see a flash
as Word opens and closes. The net result being that the return file is
generated. For what it's worth the jpg is a screenshot of a report from
another program. They want to see docs instead of jpgs for some reason.

The problem I'm facing is that I can't get the macros to autoexecute
even though they are called AutoExec. I also don't know if the starting
document should be a doc or a dot file. Also, when I'm writing the macro
I want to do an alt-file exit. That seems to abort the macro recording
prematurely and the resulting macro doesn't exit at the end like it's
supposed to. What I have now does the import and saves the file
correctly. Just doesn't start automatically or quit at the end.

Also I worry about the end product. I don't want the document file
that's eventually created to have a macro in it. Those people are
supposed to be able to see the JPG. That's the whole point of the
exercise.

Any suggestions gratefully received.
 
G

Graham Mayor

The following should work. Save the following macro in document template
(dotm template in Word 2007) . Change the path to that where the images are
stored. Double click the filename from Windows Explorer to open Word and run
the macro which will create the three documents where indicated and then
quit Word.

Sub AutoNew()
Dim DocA As Document
Dim DocB As Document
Dim DocC As Document
Dim sPath As String
'Define path containing the pictures
sPath = "D:\My Documents\My Pictures\"
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
Application.ScreenUpdating = False
Set DocA = Documents.Add
Set DocB = Documents.Add
Set DocC = Documents.Add
DocA.Range.InlineShapes.AddPicture FileName:= _
sPath & "Comp1.jpg", LinkToFile:=False, _
SaveWithDocument:=True
DocA.SaveAs "c:\return\comp1.doc"
DocB.Range.InlineShapes.AddPicture FileName:= _
sPath & "Comp2.jpg", LinkToFile:=False, _
SaveWithDocument:=True
DocB.SaveAs "c:\return\comp2.doc"
DocC.Range.InlineShapes.AddPicture FileName:= _
sPath & "comp3.jpg", LinkToFile:=False, _
SaveWithDocument:=True
DocC.SaveAs "c:\return\comp3.doc"
Documents.Close
Application.ScreenUpdating = True
Application.Quit
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top