Results 1 to 10 of 10
  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ms Word Automation from VB (Office 2003)

    I am trying to automate Word from an Access application with VB. I am trying to search a Word Document for a user chosen word, and return the paragraph containing the word. The code below works okay, but it only returns with the first paragraph it finds, and I want all. I don't know if this is the correct forum but here goes.

    The Code
    Dim WD As Object

    LWordDoc = "c:LoggingTest.doc"

    Set WD = CreateObject(Class:="Word.Application")
    WD.Visible = False 'True

    'Open the Document
    WD.Documents.Open Filename:=LWordDoc, ReadOnly:=True

    WD.Selection.Find.ClearFormatting
    With WD.Selection.Find
    .Text = Me.Text0
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False

    End With

    WD.Selection.Find.Execute

    If WD.Selection.Find.Found Then

    WD.Selection.Expand Unit:=wdParagraph
    strWordData = WD.Selection.Range
    Me.FoundWord.Caption = Left(strWordData, Len(strWordData))

    Else
    Me.FoundWord.Caption = "Not Found"

    End If

    WD.Quit
    Set WD = Nothing

    End If


    Thanks

  2. #2
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ms Word Automation from VB (Office 2003)

    Welcome to Woody's Lounge

    I am not too familiar with Access, but have modified your code in Word. Perhaps the attached document's code will assist.
    Attached Files Attached Files
    Regards
    Don

  3. #3
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ms Word Automation from VB (Office 2003)

    Thanks Don, however I opened the attachment and founf The Quick Brown Fox.......... repeated 20 times??

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: ms Word Automation from VB (Office 2003)

    Ahh, but one of the paragraphs is not the same as the others (no. 12). <img src=/S/duck.gif border=0 alt=duck width=23 height=23>

    Don suggested you look in the code attached to the document, not the document itself. Module 1 shows the relevant section of code which expands the found selection to the full paragraph.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ms Word Automation from VB (Office 2003)

    Hi Andrew, thanks for comming in. I have looked everywhere for module 1, any code, macros etc, but all I see is the document. Sorry to be dumb but I know very little about MS Word itself - think it shows? Can you tell me where to go (not literally) thanks

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ms Word Automation from VB (Office 2003)

    Select Tools | Macro | Visual Basic Editor or press Alt+F11 to activate the Visual Basic Editor.
    There are two code modules in the document: Module1 and NewMacros - you'll see them in the Project Explorer on the left hand side. Double click one of these to see the code it contains.

  7. #7
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ms Word Automation from VB (Office 2003)

    Many thanks to all, found it. I will try and put it into access now, and let you know. Thanks again.

  8. #8
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ms Word Automation from VB (Office 2003)

    Many thanks Don - Just what I wanted, works a treat. Thanks to the others as well for getting me off the ground. Best regards

  9. #9
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ms Word Automation from VB (Office 2003)

    My apologies; I should have removed the "New Macros" module.

    You should run the macro from Module1 in Word. noting that I have set the search text to "pig". I would recommend that you:
    <UL><LI>open the Word document which I sent;
    <LI>open VBE in accordance with Hans' instructions;
    <LI>arrange your windows such that you can see both the document and the code;
    <LI>In VBE open the "Immediate " window (Ctrl+G);
    <LI>place the cursor within the Module1 code;
    <LI>single step through the code (F8), and see what happens in the Word document with each command.[/list]When you comprehend what is going on here, you should be able to revise your Access code.

    H.T.H.
    Regards
    Don

  10. #10
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ms Word Automation from VB (Office 2003)

    Many thanks Don for the added info, I think my reply crossed over with yours. I am very happy as it does what I wanted in Access. Thanks again, I had been trying to solve it for days. Best regards

Posting Permissions

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