WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Insert Wave File into a Word Document


Question:

I don't know if anyone will be able to figure this out but this one seems a bit difficult to implement.

Here's a short overview of what it is:
On a MS Word document menu bar, you can hit Insert>Object>{Select Media Clip}
and then within the clip menu bar, you can hit Insert Clip>Sound>{Choose a wave file} and insert it.

It allows you to play a wave file within MS Word.

I am wondering if Winbatch might be able to perform the above set of operations and if so, what type of an approachdo you suggest I should take. I need it to insert a set of wave files (given the directory of the files) individually in MS Word documents and save the word documents as the same filename as the wave files in a specified directory. Please let me know if you are confused. I'll try to explain it more in details.

Answer:

First I recommend writting the code to deal with one file, you can then modify the code to deal with multiple Word docs and wav files.

You will need to get familiar with the OLE objects, methods and properties exposed by Word.

Launch Word, select the Tolls|Macro|Visual Basic Editor menu option.

Next select the Tools|Object Browser menu option.

On the project/library dropdown list, choose Word.

You should now be able to do keyword searches, based on what you need to accomplish.

Sometimes it helps the actually record a macro in word that does what you need, then translate into WinBatch code.

The code *might* look something like this:

 
;Name of wav file
mediafile = "C:\WINDOWS\tada.wav"
;name of existing doc file to open
file      =  "c:\temp\new.doc"
if !FileExist(file)
	message("Error","File not found")
	exit
endif  

;obtain application object
oWORD     = ObjectOpen("Word.Application")
;obtain Documents object
oDOCS     = oWORD.Documents
;open .doc file
oDOC      = oDOCS.Open(file)
;obtain Inlineshapes object
objinlineshapes   = oDOC.Inlineshapes

;insert media file
objinlineshapes.AddOLEObject(::ClassType="MPlayer", FileName="%mediafile%", LinkToFile=@False, DisplayAsIcon=@False)

;Save to new file
oDOC.SaveAs( StrCat(dirget(),"revised.doc") )
;close document
oDOC.Close()
;quit word
oWORD.Quit()

;close object handles
ObjectClose(objinlineshapes)
ObjectClose(oDOC)
ObjectClose(oDOCS)
ObjectClose(oWORD)
Message("Operation Complete","See the revised.doc file for changes.")
Exit



Article ID:   W16144
File Created: 2004:03:30:15:42:58
Last Updated: 2004:03:30:15:42:58