InsertFile filename

thierno

New member
Local time
Yesterday, 21:23
Joined
May 1, 2005
Messages
7
dears members,

I m trying to add document2.doc to document1.doc by clicking a button on a form.

I use the next VBA code.

Selection.InsertFile filename:="document2.doc", Range:="", ConfirmConversions :=False, Link:=False, Attachment:=False

I get the following message error:

Err.number = 5174
Err.Description = "The file cannot be found"
Try one of the following things:
* Check if you write the name good
* try an other file name (document2.doc)

The file document2.doc does really exist and is saved in the same directory as document1.doc

the problem is sometime I get the error message some times not in this last case it works very good.

can any body help me ?

Thanks in advance.
 
The problem depends on how you opened document1. I'll try to be specific.

If you used an "Open File" dialog box (which is something you could have picked up in this forum or many other sources), your document1 file gets opened 'cause you browsed for it. If the name in your DB has a path, again you can open the file from anywhere. If you used a filefind or similar method to find the file, particularly if you allowed searching of subfolders, there is no telling where you found it. But in the VBA sample you specified, document2 in quotes has to be in the folder where your app's attention is directed, and this is not necessarily the folder where document1 resides depending on how you found document1.

The correct way to do this is to always specify a path. Since you are in VBA anyway, you might be able to do something like this: Get the name of document1 from Word. It is something like CurrentDocument.Name or whatever the shortcut is for the document that currently has "focus." It has been too long since I did any of that. In order to activate "Selection" you had to specify something in this document anyway so you probably know what I'm suggesting.

Now, starting from the tail end of that name string, using the MID$ function to extract one character at a time (see MID$ function in Help) find a \ character. (Or is it /?) In any case, take all of the characters from position 1 to the position of that rightmost slash (see LEFT$ function in Help) and build a string containing that device & path plus "document2.doc" - then instead of the quoted string for document2's name, use the string that you just built containing the fully qualified device, path, name, and type.
 

Users who are viewing this thread

Back
Top Bottom