Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Mar 2001
    Location
    Atlanta, Georgia, USA
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change Word Doc from VBA

    This is probably a simple problem, but it seems to be eluding me. I want to write a VB program (not a macro) that will open a Word document, make changes, which would include AutoSummarize, then save the file and move on to another document. This seems trivial, but I am stumbling around. Any good examples?

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Change Word Doc from VBA

    Briana,

    It may be that you'll need to be a bit more specific about what you're trying to do, in order to get any targeted help.

    For starters: are we talking about doing this with VBA (i.e. from within a VBA host app), or are we talking about doing this with a VB app? (Strictly, you don't need a VB app to do this; it can be done within Word for instance.)

    What kind of changes do you need to make, and what are the sources of problems?

    Do you need to loop through all the files in a directory, a drive, or what? - there's been code posted in this forum for looping through all the files in a directory - probably can be located by doing a Search of this forum.

    Gary

  3. #3
    cathyj
    Guest

    Re: Change Word Doc from VBA

    Hi Briana

    Try this ...

    Sub Main()
    Dim objWord As Object
    On Error GoTo ErrorHandler:

    'create new instance of word object - this will specifically load
    'Word 2000 (9)
    Set objWord = CreateObject("Word.Application.9")

    'so you can see what's happening
    objWord.Visible = True

    'open document
    objWord.Documents.Open FileName:="c:your doc.doc"


    'set document properties
    With objWord.Documents(objWord.Documents(objWord.Docume nts.Count).Name).CustomDocumentProperties
    .Add Name:="Report Name", LinkToContent:=False, Value:="sample text", Type:=msoPropertyTypeString
    .Add Name:="Report Date", LinkToContent:=False, Value:="sample date", Type:=msoPropertyTypeString
    End With

    'save & close
    objWord.Documents(objWord.Documents(objWord.Docume nts.Count).Name).Save
    objWord.Documents(objWord.Documents(objWord.Docume nts.Count).Name).Close


    'set Word object to nothing
    Set objWord = Nothing


    Exit Sub
    ErrorHandler:
    End Sub


    Good luck!

  4. #4
    Star Lounger
    Join Date
    Mar 2001
    Location
    Atlanta, Georgia, USA
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Word Doc from VBA

    Cathy:

    Thanks! That was just the push I needed. I already have the DSOFile Object library that will let me read the properties of Office documents. I had written a VB app that will find all of the DOC files on my system and list the keywords associated with each document, but I found that some of the documents lacked keywords. This should fix the problem.

    Gary: Sorry if I wasn't clear in my original post.

    Brian A

  5. #5
    Francis Winter
    Guest

    Re: Change Word Doc from VBA

    Of course, DSOFile will also allow you to *write* to properties, without opening the file, or using CreateObject, GetObject etc. Works much faster too.

  6. #6
    Star Lounger
    Join Date
    Mar 2001
    Location
    Atlanta, Georgia, USA
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Word Doc from VBA

    True, but what I'm trying to do is let Word scan the document for keywords using the Autosummarize function, then update the Properties with the keywords. So any future scans using DSOFile should show the keywords without having to open that document.

    In effect, I'm writing an app that will help find WORD documents via keywords.

    As an aside, I've discovered that DSOFile will error out in Visual Basic if a file with a .DOC extention is not a WORD document. I can catch the error under W2K, but not W98. Very strange.

  7. #7
    Bill Altmann
    Guest

    Re: Change Word Doc from VBA

    I have read this thread and all I can find on the MSDN site but I can't find out how to
    define the constants like "msoPropertyTypeString" used in the example code. My
    VB program doesn't know what to make of that constant. Which COMPONENT
    must I add in Visual Basic to have access to the definitions of "mso***" constants?

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Change Word Doc from VBA

    Bill,

    The mso constants are available when using VBA via any of the MS Office apps.
    I've never run the DSO code from VB, so I'm not sure how you hook into the Office constants from VB.

    Gary

  9. #9
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Change Word Doc from VBA

    It's the Microsoft Office 9.0 Object Library (from "Tools>References" in the VB editor).

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  10. #10
    Bill Altmann
    Guest

    Re: Change Word Doc from VBA

    Thanks for the pointer. In VB6 the menu is at Project->References instead of Tools->References.
    I'm still having a problem deleting a CustomDocumentProperty from a Word file which I've opened
    from VB. I can .open the file and list the CustomDocumentProperties from their collection. But...
    - the .delete method is not recognized by the VB editor and creates a run-time error, and
    - the .add method, while recognized, and adding to the collection, is not visible in the Explorer
    Custom Properties view. Must I do something so that in Explorer I can list the .DOC file and then
    right-click to open the Properties and then look at the Custom Properties and see the added term?

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Change Word Doc from VBA

    Can you post the relevant code you're using?

Posting Permissions

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