Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! problem opening Word from VB if extension is not doc

Status
Not open for further replies.

caguila

MIS
Mar 20, 2002
28
GB
Does anyone know how to overcome the problem of opening Word documents where the extension is not "doc"?


Dim objWordApp As Word.Application
Dim objWordDoc As Word.Document
yourdoc = "c:\test.0" 'is a word document given a different extension
Set objWordApp = New Word.Application
objWordApp.Visible = False
Set objWordDoc = objWordApp.Documents(yourdoc)

--this code generates an error
 
One slight modification needed:


Set objWordDoc = objWordApp.Documents.Open(yourdoc)

missed the .Open

Enjoy!

*J*
 
jyschaefer ,

Sorry , pasted wrong code. Yes, even with the open it does not work.

Christel
 
It works for me....
Do you have a reference to the Microsoft Word 8.0 library in your project?

*J*
 
simple solution just use an if statement

dim documentName as string ' stores the documents name

trim$(documentName)

If Left$(documentName, 4) = ".doc" then
code
code
else
documentName = documentName & ".doc"
trim$(documentName)
end if

 
craig_optisof,

The document's extenstion cannot be altered, they have been programmed to have numerical extensions. Thanks anyway.

jyschaefer,
Thanks, it does work on a normal VBA / VB environment but for some reason, from an ocx/usercontrol. If I pasted the value of the variable on the line documents.open "c:\test.0", it worked, but when I replaced it with the variable, the runtime error 5121...Make sure the document has a .DOC extension was again generated. I have tried the variable with a "doc" extension and it did give the error as well. Even when the value of the variable was "c:\test.doc", it generated an error. Ive tried it again on a standard vb app with a variable containing the document name, & still generates the error.

 
Thanks again for all your replies, I have now managed to sort out the problem. I was using a Microsoft dll to load the documents properties and until that reference is set to nothing, the document will not open. It is just unfortunate that the message was misleading although appropriate because I was using a different file extension. Basically if the document is locked by something or open by another user, it generates the invalid document extension error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top