i m creating some application with diffrent kind of users.
in my application i want to open a word document.
problem is that some users can view and update the file and others can only view it.
anyone know how can i open the word file with read write permissions??
thanks,
ohad.
Iouri
August 9th, 2001, 03:05 PM
Before opening the document read the user name and decide if he/she is allowed to modify it.
If a user is allowed to modify the file you can just open it. If user is not allowed to modify the document, copy it first into different one and open it. When user has finished with it delete it.
Iouri Boutchkine
iouri@hotsheet.com
Cimperiali
August 10th, 2001, 02:47 AM
'Tis open a doc readonly, but user can modify it and save it with
' a different name
Option Explicit
Dim wdapp As word.Application
Dim wdocs As word.Documents
Private Sub Command1_Click()
Set wdapp = New word.Application
wdapp.Visible = True
Set wdocs = wdapp.Documents
'open sintax: the_pathName_to_doc,confirm_Conversions,readonly
wdocs.Open "c:\doc1.doc", False, True
End Sub
'you have to correct manage these commands:
wdocs.Close 'only if documents.count >0
Set wdocs = Nothing
wdapp.Quit ' only if not closed within word (you can find it
'declaring word withevents and intercepting the quit event.
'Beware, there is a difference between ide and compiled)
Set wdapp = Nothing
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
ohadbp
August 10th, 2001, 03:14 AM
what control/object do i need to add to my project so i could use this code?
thanks,
ohad.
Cimperiali
August 10th, 2001, 03:19 AM
Put a reference to Microsoft word 8.0 (or 9.0) object library
(via menu -> project ->;references)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Cimperiali
August 10th, 2001, 03:21 AM
..and add a command button to a form.
By teh way, to see how to use event Quit, have a look here:
http://codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=vb&Number=58136&page=&view=&sb=
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
ohadbp
August 10th, 2001, 03:51 AM
i add the word 9.0 tlb and i tryed running your code but i get an automation error about the line:
set wdApp=new word.application
any suggestions?
thanks,
ohad
Cimperiali
August 10th, 2001, 03:58 AM
reference microsoft word 9.0 Object library (file should be "MSWORD9.OLB"). This require word to be installed on your PC. Iouri once said you may do the same without having Word installed (you should ask him if this is the case)
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
Jason Bennison
September 29th, 2003, 05:40 PM
I have the following dilemma.
I made an app using VB6, Win XP (O/S is not really relevant anyway), and Office 2002 Pro installed and it needs to call MS Word to load a document. The app works fine, and its installables are fine on any PC with Office 2002 installed on it.
I installed the app on a second PC (again I added the MSWORD10.OLB to the references, and shipped it with the distributables), with that PC having Word 2000 and Office 2002 installed. That too was fine.
I uninstalled Office 2002 on the second PC, re-installed (to put MSWORD10.OLB back on it) re-run the app, and – CRASH! An error – at the following VB line.
Set WordApp = New Word.Application
Same result with running it as executable and from within VB6.
I then added MSWORD9.OLB (because it was Word 2000) to the references, and this had no effect, - still not working. If I re-install Office 2002 on the 2nd PC and re-run the app, – it’s OK again…
I need to have this app to work on a PC having Word 2000 or Word 2002. (and earlier versions of Word would be good) Where Am I going wrong??
VB6 doesn’t seem to like me having a reference to both MSWORD9.OLB and MSWORD10.OLB with the same app. It just takes MSWORD10.OLB as the referenced object if I attempt to apply both, but I can ship both objects with the App.
I need help! And I am open to suggestions here. – How do I get an app to work with any (almost) version of Word installed on the client PC?, If possible, I would like some magic code :-s
Jason.
Jason Bennison
September 29th, 2003, 05:53 PM
Oops i forgot,
the error message is "Runtime Error 48 - Error in loading dll" (from VB6)
J.
Twodogs
September 29th, 2003, 05:57 PM
I think you would have to code for the different versions of word in your application, then have your setup file detect which version of word was installed....which means using something like WISE installer (I'm not sure if Visual Studio Installer can be coded to check this out - probably could, its just that finding the info on how to do it could be difficult!!!)
I also think you'd have to dynamically reference the object library in your application too - perhaps by interrogating the registry.
Good luck :)
Jason Bennison
September 29th, 2003, 06:06 PM
Right,
Just been trawling the MSDN site, and this says i should not need to distribute the MSWORD.OLB at all. These are already installed on the client PC where Word 2000 or Word 2002 is installed.
I tested the app by recompiling it without any MSWORD.OLB's at all and installed and run the app on a 3rd PC having Word 2002 only, and it works fine.
It still doesnt work with a machine having oly Word 2000 on it..
Im stuck !!!
J.
Twodogs
September 29th, 2003, 06:17 PM
I think (untested) that you would have to have ALL relevant OLB's installed on your development machine and dynamically reference them according to the registration information that you get out of the registry.
You will then have to 'late bind' the application object to the relevant OLB as determined by what comes out of the registry.
From MSDN
Late binding queries the application you are automating at run time, which makes it possible for you to declare object variables by using the generic Object or Variant data type. In general, late binding is useful if you are writing generic code to run against any of several applications and won't know the type of object you are working with until run time. Note that the additional overhead of querying an application at run time can slow down the performance of your solution.
Note Some applications and components that support automation support only late binding. All Office XP applications and most contemporary applications that support automation support both early and late binding. However, scripting languages such as VBScript and Microsoft® JScript® don't support early binding because they don't support references or specific object data types (for example, in VBScript only the Variant data type is supported).
Late-Bound Declarations
Late binding makes it possible for you to declare a variable as an Object or a Variant data type. The variable is initialized by calling the GetObject or CreateObject function and specifying the application's programmatic identifier. For example, in the following code fragment, an Object variable is declared and then set to an instance of Microsoft® Access by using the CreateObject function:
Dim objApp As Object
Set objApp = CreateObject("Access.Application")
Late binding is the friendly name for what C programmers used to call IDispatch binding, and was the first method of binding implemented in applications that can control other applications through automation. For this reason, you can use late binding to maintain backward compatibility with older applications. However, late binding uses a lot of overhead; it is faster than dynamic data exchange (DDE), but slower than early binding.
Tip DDE is a protocol that was established before OLE for exchanging data between Windows applications. There is no need to use DDE to exchange data between Office applications because of their support for automation. However, you might have to use DDE from some other application that doesn't support automation code to work with data from an Office application. For more information about using DDE, search the Visual Basic Reference Help for the Office application you want to work with.
The CreateObject function must also be used to work with objects from any automation component from script. This is because scripting has no method of establishing references to type libraries to support early binding.
Jason Bennison
September 29th, 2003, 09:02 PM
Can yu provide me with the code snippet to dynamically query the registry for the version of the .OLB used, and how to dynamically reference the OLB present on the client machine.
have a feeling this route might fix the problem.
J.
sotoasty
September 29th, 2003, 09:37 PM
Here is a bit of code that I used to find the version of word that is installed on the computer. Please note it is old now so it will have to be updated.
' find ms word or worviewer
Private Sub FindWord()
Dim R As New RegClass ' set up a new registry class
ShowRefs = True ' set the default to show the references
' check for office 2000
If Not R.OpenRegistry(HKEY_LOCAL_MACHINE, "Software\Microsoft\Office\9.0\Word\InstallRoot") Then ' check to see if the registry key we need can be opened
' check for office 97
If Not R.OpenRegistry(HKEY_LOCAL_MACHINE, "Software\Microsoft\Office\8.0\Word\InstallRoot") Then ' check to see if the registry key we need can be opened
' check for word viewer
If Not R.OpenRegistry(HKEY_CURRENT_USER, "Software\Microsoft\Word Viewer\8.0\Options") Then ' check to see if the registry key we need can be opened
' check for word viewer again
If Not R.OpenRegistry(HKEY_LOCAL_MACHINE, "Software\CLASSES\CLSID\{00020906-0000-0000-C000-000000000046}\DefaultIcon") Then ' check to see if the registry key we need can be opened
' display a message that no viewer was found
MsgBox "Unable to Find a Word Document Viewer. References will not be available."
' set default to not show references
ShowRefs = False
Else
WP = R.RegGetVal("") ' set the wordprocessor variable
WP = Left(WP, Len(WP) - 2)
If InStr(1, UCase(WP), "WORDVIEW.EXE") < 1 Then
MsgBox "Unable to Find a Word Document Viewer. References will not be available."
' set default to not show references
ShowRefs = False
End If
End If
Else
WP = R.RegGetVal("PROGRAMDIR") + "\wordview.exe" ' set the wordprocessor variable
End If
Else
WP = R.RegGetVal("Path") + "\winword.exe" ' set the wordprocessor variable
End If
Else
WP = R.RegGetVal("Path") + "\winword.exe" ' set the wordprocessor variable
End If
R.CloseRegistry ' close the registry
Haven't got too much time - I've attached a file "utility.bas" that has a few registry functions in it...select the one you want and use that (I think GetRegistrySetting will be the one you want).
I'm not sure what the actual version or word you'll be looking for will be (I was only 'logic-ing' your question....) but I'm sure you can find that.
My earlier post should be able to help you with the late binding...just put the reference to the olb inside a select case statement where the cases are the possible words to come out of the registry.
Hope this helps:)
Twodogs
September 29th, 2003, 10:04 PM
This is what happens when you press "send" and then go off to lunch without checking that it all went through ok.....I forgot to make it a zip and the board doesn't like .bas files. Sorry about the delay!
Jason Bennison
September 30th, 2003, 08:26 AM
Right, this is what I have managed to do so far.
The first post Ive managed to use for determining what version of Word is present on the system – so it does the job!
The difficulty is this. I installed the app to run on 2 different PC;s. one has Word 2000 only, and this still doesn’t work. The other – and heres the funny bit - also has word 2000 installed and it works fine – but! – it also has word 2002 installed (using 2000 as the default word processor)
I have looked at another post, which says not to reference any .OLB at all. That’s great, but I am using early binding because I am using the range object within the app. - which is part of the word.application object. Late binding is causing me no-end of problem, and would require an extensive rebuild of the entire design of how the program works in producing its Word documents (it is a Last Will and Testament compiler – it writes your Will! – UK Law)
At present, so long as Word 2002 or higher is present, its fine, but the question is – Can I actually stipulate Word 2002 as the minimum spec – the program’s users are all Lawyers and legal buffs and wouldn’t even know Word 2002 if they saw it. I do know many are using Word 2000 (Win2K on their networks etc..)
I am now looking for suggestions for >>ANY<< workaround to get this thing to work correctly under word 2000, or word 2002 and up.
Microsoft’s website is telling me to simply distribute the MSWORD.OLB (for 2002 and up) with the app., or simply don’t reference any .OLB at all (for late bind) But neither solution is working.
I’m pulling my hair (what’s left of it now) out here!!
Jason.
Jason Bennison
September 30th, 2003, 08:29 AM
Forgot to include the error message.. (on the Word 2000 machine)
it is 'Error in loading DLL' - different from last time, so we might be onto something
Jason Bennison
September 30th, 2003, 02:13 PM
Well done sotoasty and Twodogs.
The problem has been fixed.
I needed to create the word object (with a reference to msword9.OLB - and not MSWORD.OLB) before creating the properties of the word.application object, all within the sub/functions that use the word object instead of a public global word.application object.
The program has to use early binding because it uses properties of the word.aplication object (the range object) - as Microsofts website states, otherwise late binding would suffice without any reference to any .OLB object at all as Cimperiali's earlier post and code example explains.
For anyone in a similar situation, i also found the following useful.
It was a bit of a fiddle cleaning up the code, but at least it now does the job.
Thanks once again.
Jason.
Twodogs
September 30th, 2003, 06:45 PM
Good to hear you have it fixed....I could've probably helped a little more if I was unscrupulous enough to have kept the code from a previous job - it made financial plans (word doc) after going out and getting all the info from various stock exchanges around the world...from the sound of it, it was doing exactly what you are!
(Oh - you still could've used late binding, you just don't get the 'auto-complete' happening, so you have to know the object model pretty much inside out!)
Jason Bennison
October 13th, 2003, 05:49 PM
I know this is an old thread. But i finally got to the bottom of it.
I had a problem using automation with word (I had to use early binding - I was using the range object - but it made no difference if I used late-binding anyway)
Word 2002 (and MSWORD.OLB) worked fine. But it just wouldn’t work with Word 2000 (with MSWORD9.OLB).
I uninstalled MS Word 2002 (which VB reference MSWORD.OLB) and installed Word 2000 and allowed VB to reference MSWORD9.OLB be default on the development machine. Compiled and packaged it, and it worked! on everything! Win 98 and up. Word 2000 and up - you name it!
If you find a similar problem, just use an earlier version of Word on the development machine. Microsoft are aware of a back-compatibility problem (XP returns a message 'illegal instruction and needs to close') with Word and VBA, but do stress it is forward compatible.
Once again, thanks for everyone that helped me out.
J.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.