Automatically insert date from inserted file

B

BitMan

Hi

I am new to VBA programming - and need to solve the following:

When a user inserts a JPG picture from then "Insert/picture" menu - I need
to automatically insert the date from the file below the picture. Is it
possible to achieve this?
Any hints for good examples are very appreciated.


/BitMan
 
D

DA

Hi Bitman

When you say "..the date from the file", do you mean just
the plain old file date or are you dealing with digital
camera photos trying to include the date the picture was
taken?

If you just want the file date, you can use something
like:
myFileDateVar = FileDateTime("filename")

If you want to get at the date the picture was taken then
the data you want is in the EXIF header of the image.

Here's where the problem starts. As usual, there's more
than one way of skinning a cat, so don't take this as
being the 'right' way of going about it. There seems to
be a few utilities out there for extracting EXIF data,
and there's probably even a library that lets you address
the attributes directly from VBA.

I've had a go at simply extracting the data by doing a
binary read on the image. I found that reading the first
200 characters of the header normally included the date.
All I had to do then was search that return string for
the date and strip the other crap away.

To read the data use something like:
Open "filename.jpg" For Binary Access Read As #1
DataIn = Input(200, #1)
Close #1

Hope this has been of some help.
Dennis
-----Original Message-----
Hi

I am new to VBA programming - and need to solve the following:

When a user inserts a JPG picture from
then "Insert/picture" menu - I need
 
B

BitMan

Hi

Thanks for you answer. It's only the file date from the file - so you first
solution should be enough.

I have been looking at events in Word - and it seems like there's no event
to detect on when the user inserts an image - whats the best way to detect
that event?

/BitMan
 
D

DA

I'm not entirely sure if you can capture the event.
Perhaps one of the MVPs might have an answer for you on
that.

Another way to handle this is to write a routine that
replaces the default insert picture menu item (sample
routine at end of this message). Make a template file
which either replaces that menu choice entirely or just
place a new macro button on the toolbar. I often do this
in order to replace the default Bullets and Numbering
menu and buttons with my own macro.

Make a new .dot template file, then write your macro and
put it into a Sub routine (not private). On your toolbar,
right-click and choose Customize. From here you can not
only make a new macro button or toolbar, but also change
the existing Word menu entries (such as Insert Pic from
File).

I don't want to turn this into a long story since I'm not
sure what your skill level is. There's a lot of things to
consider, so if you're new to this type of customization
I'd suggest you do a search on this forum for things
you're not sure about.

You may also want to check out the Word MVP site
(http://word.mvps.org), which is arguably the best
collection of Word & macro related info on the web.

Give it a go and if you get stuck, post a new question on
this forum.

All the best,
Dennis

---Sample----
Sub InsertPicAndDate()
Dim strPicVar As String
Dim myFileDateVar As Date

With Dialogs(wdDialogInsertPicture)
.Display
strPicVar = .Name
End With

ActiveDocument.InlineShapes.AddPicture (strPicVar)
myFileDateVar = FileDateTime(strPicVar)
ActiveDocument.Range.InsertAfter vbCrLf & myFileDateVar
End Sub
 
B

BitMan

Hi Dennis

Thanks for you input. I'v been programming VB for a couple of years, so with
your sample as a beginning, I should be able to do rest.


Thanks again.


/BitMan
 

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