CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Guest

    I need Simple Clarification on opening a Word document

    I want to do a simple task of opening up a Word document (ex. c:\testprint.doc), go to a bookmark (ex. testmark) , insert some text ("John Smith"), print the document and close the document without saving the document

    I would also like to get fancy and check if Word is already up and running and just open the testprint.doc in it so I don't have multiple copies of Word running, which would speed things up not having to fire it up each time.

    I am a rookie and have tried to learn on my own, but now I am just confused and don't know the correct way of doing it. I would like to find out what is the best way and stick with it to be consistent.
    I have looked at different places for examples, put they all seem different
    I have listed some example below:

    An example that has the word "New" in front of "Word.Application" and doesn't use a "set" statement and opens the file with an "Open Filename=" statement
    Dim Docname As String
    Dim Wrd As New Word.Application
    Docname = "c:\testprint.doc"
    Wrd.Documents.Open Filename=Docname


    Another example that has a "Dim" and a "Set" statement and makes the objWord visible (does it need to be visible?)
    Dim objWord as Word.Application
    on error GoTo ErrSection
    set objWord = new Word.Application
    objWord.Visible = true
    ErrSection:
    MsgBox Err.Description
    MsgBox Str(Err.Number)

    Another example that uses "Object" and "CreateObject" and "Quit" and "set to nothing" (what does this do?)
    Dim xlApp as Object
    set xlApp = CreateObject("word.application")
    xlApp.Visible = true
    ' do something
    xlApp.Quit
    set xlApp = nothing

    Another example that opens the file with "Open("???.doc")" statement
    Dim oWordObj as Object
    Dim oWordDoc as Object
    '
    set oWordObj = CreateObject("Word.Application")
    set oWordDoc = oWordObj.Documents.Open("c:\test.doc")


    Any help on either an quick example or at least clarification on why some people are doing things differently would be GREATLY appreciated,

    Thanks,
    Trevor




  2. #2
    Join Date
    May 2000
    Location
    Indore (MP) INDIA.
    Posts
    356

    Re: I need Simple Clarification on opening a Word document

    Try this :


    option Explicit
    Dim appWord as Word.Application

    private Sub Command1_Click()
    ' Don't Forget to Include Reference of Library
    ' (Microsoft Word 8.0 Object Library), to do this -
    ' Select "References" from "Project" Menu.
    on error resume next ' Needed, because the next statement will
    ' cause an error if Word is not Running.
    set appWord = GetObject("Word.Application")
    ' IF Word already Running, Give me its Memory Address.
    If appWord = null then
    set appWord = CreateObject("Word.Application")
    ' Otherwise Load Application MS-Word.
    End If
    appWord.Visible = false

    appWord.Documents.Add ' Add a new empty Document for me,
    appWord.Documents.Open ... ' or Open a Document ...


    - - - -
    - - - - ' Do some Stuff ...
    - - - -

    appWord.ActiveDocument.Close false ' false means "Do not Prompt to Save"
    appWord.Quit ' May not be Required, as per your Condition.
    End Sub






    By this you can Open MS-Word, without creating its New Instance, If it is already running, You'll get its Object, otherwise It will load MS-Word.

    Opening a Document is same as you specified in your code.
    To Assign some text to the Document, you can use Object.ActiveDocument.Content="Assign Text to Word Doc", where "Object" can be appWord or some thing else.

    As far as using different forms of code is concerned, Well every one has a different style of writting Programs.

    Hope this Helps you.




    -Vipul Pathak.
    Pathak Developers Pvt. Ltd.
    Indore, (MP) INDIA.


    PS: If this helps you then Rate it, so let me know How much is this helpful to you.
    (*Vipul)() ;

  3. #3
    Guest

    Re: I need Simple Clarification on opening a Word document

    Thanks, that helps alot.
    Two quick question thought (in case someone every asks me this)

    1) With the "Open" statement
    is it prefered
    appWord.Documents.Open("c:\test.doc") or

    Dim Docname As String
    Docname = "c:\testprint.doc"
    appWord.Documents.Open Filename=Docname

    or does it not matter about the "Filename" parameter? (Filename keyword assumed or personal coding preference)

    Also I noticed that some people had the word "NEW" in their statement
    Dim Wrd As New Word.Application
    What does the "new" do?

    Thanks Again,
    Trevor


  4. #4
    Join Date
    May 2000
    Location
    Indore (MP) INDIA.
    Posts
    356

    Re: I need Simple Clarification on opening a Word document

    Well, I never used it in the following way -
    appWord.Documents.Open FileName="XYZ.Doc"
    Today morning when i tried it, It had Flashed an Error. So, I don't Know how people are using this "Syntax".

    What I am using by past 1 year, (and MSDN also suggest the Same) is -

    appWord.Documents.Open "C:\My Documents\ABC.Doc", false, false, false



    The 1st "False" Will Eliminate displaying the Convert-Document Dialog Box.
    The 2nd "False" Will Open File in Read/Write mode, If True: then ReadOnly.
    The 3rd "False" Will Stop MS-Word from Entering the FileName into its Recent FileList.

    Many other options are also there, but I use only this much, [refer MSDN for more]

    For your second Query -
    How the following 2 Statements are different ?

    Dim appWord as Word.Application
    Dim appWord as new Word.Application




    The First Statement is Only Declaring the Object appWord, this is what i did in my previous Post. The New Operator Creates an Implicit Object as soon as you Declare the Object.

    To Clarify this, Take a Look on the Following Codes -
    1)

    Dim appWord as new Word.Application




    and
    2)

    Dim appWord as Word.Application
    set appWord = CreateObject("Word.Application")




    I need not to say that, Both of the Above will do the Same.
    As Both will Create the Word Instance, and It is clear that the First one is shorter Method, (so people prefer it).
    But I told you the Second Method, Because the New Operator never checks for an Existing Running Instance of MS-Word, it will simple Spawn a New Instance.
    We checked for an Existing Instance of Word by first checking it, using -

    GetObject("Word.Application")



    If GetObject() returns Null, that means We have to now use CreateObject() Method.


    I hope this is now very Clear to you. Feel free to ask me if you again have some Query in the Above.

    Enjoy VB Programming ........

    -Chow.




    -Vipul Pathak.
    Pathak Developers Pvt. Ltd.
    Indore, (MP) INDIA.


    PS: If this helps you then Rate it, so let me know How much is this helpful to you.
    (*Vipul)() ;

  5. #5
    Guest

    Re: I need Simple Clarification on opening a Word document

    Thanks for your reply, Everything works great, except the GETOBJECT line:

    set appWord = GetObject("Word.Application")
    ' IF Word already Running, Give me its Memory Address.
    If appWord = null then
    set appWord = CreateObject("Word.Application")
    ' Otherwise Load Application MS-Word.

    Everytime I run this (regardless) how many version of Word are open, appWord is always = "nothing" and executes the "If appWord = null then" statement and runs the "set appWord = CreateObject("Word.Application")" statement and opens up another version of Word.

    I am running Word 2000. Is it different in Word 97 than from Word 2000.
    Any suggestions?

    Thanks for all your help,
    Trevor


  6. #6
    Join Date
    May 2000
    Location
    Indore (MP) INDIA.
    Posts
    356

    Re: I need Simple Clarification on opening a Word document

    Oh,
    Well, I suspect Why Its not Running ? Well, I have till yet used Office-97 only, and have no experience of how Office-2000 behaves, when Creating Object.
    But I think, It should not behave much differently.

    Check-out your Code, that both the Statements GetObject() and CreateObject() are having the Same Parameter ?

    Check-out also, that which Library you have Included, (For Word in Office-97, I include "Word 8.0 Object Library" from the Project->References Menu).

    I Think, You'll find some solution, when you'll check the Above.

    -Chow.



    -Vipul Pathak.
    Pathak Developers Pvt. Ltd.
    Indore, (MP) INDIA.


    PS: If this helps you then Rate it, so let me know How much is this helpful to you.
    (*Vipul)() ;

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