CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    May 2001
    Location
    israel
    Posts
    99

    Opening word from my application

    hi,

    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.




  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Opening word from my application

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Opening word from my application

    Code:
    '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
    Last edited by Cimperiali; February 16th, 2004 at 05:40 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    May 2001
    Location
    israel
    Posts
    99

    Re: Opening word from my application

    what control/object do i need to add to my project so i could use this code?

    thanks,
    ohad.


  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Opening word from my application

    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
    Last edited by Cimperiali; February 16th, 2004 at 05:41 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Opening word from my application

    ..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/s...age=&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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    May 2001
    Location
    israel
    Posts
    99

    Re: Opening word from my application

    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


  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Opening word from my application

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Sep 2000
    Location
    London England
    Posts
    144

    Unhappy Calling MS Word from VB application

    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.

    Code:
    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.

  10. #10
    Join Date
    Sep 2000
    Location
    London England
    Posts
    144

    forgot

    Oops i forgot,

    the error message is "Runtime Error 48 - Error in loading dll" (from VB6)

    J.

  11. #11
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    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
    Be nice to Harley riders...

  12. #12
    Join Date
    Sep 2000
    Location
    London England
    Posts
    144
    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.

  13. #13
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    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.
    Be nice to Harley riders...

  14. #14
    Join Date
    Sep 2000
    Location
    London England
    Posts
    144

    Im ignoranus.

    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.

  15. #15
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452
    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.


    Code:
    ' 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
    
    End Sub
    You will also need the following class module.

    RegClass

    Hope this helps

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured