How to Search and Manipulate the contents of a Word document in Access 2007

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • walid geagea
    New Member
    • May 2011
    • 8

    How to Search and Manipulate the contents of a Word document in Access 2007

    Hello,
    I have written the following codes in order to open a word document from an Access 2007 form.
    The document was openned correctively but the problem is that I want to be able to go to a specific chapter inside this document based on a search filter field that I will already define in my Access Form.
    Then i want to Load this chapter inside my form, manipulate it, then save back in the original Word document.

    Can anyone help me finding a solution??


    Code:
    Public Sub OpenWordDoc(strDocName As String)
        Dim objApp As Object
    
        'Opens the document
    
        Set objApp = CreateObject("Word.Application")
        objApp.Visible = True
        objApp.Documents.Open strDocName
    
     
    End Sub
    
    
    Private Sub cmdOpenWordDoc_Click()
    'Check to see that there is a document file path associated with the record
        If IsNull(FilePath) Or FilePath = "" Then
            MsgBox "Please Ensure There Is A File Path Associated " & _
            "For This Document", _
                   vbInformation, "Action Cancelled"
            Exit Sub
        Else
            'Check that the document specified in the file path is actually there
            If (Dir(FilePath) = "") Then
                MsgBox "Document Does Not Exist In The Specified Location", _
                       vbExclamation, "Action Cancelled"
                Exit Sub
            Else
                'Opens document in Word
                Call OpenWordDoc(FilePath)
                
     
            End If
        End If
      
    End Sub
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    Walid,

    The scope of your question is far too broad. No-one is going to do the whole thing for you. Many would be happy to answer specific questions where they are able, but this is not such a one. Let's start from the stub of code you have posted.

    Does it work as far as you're concerned (I know it doesn't do the whole job but forget that for now - Does it behave as intended)?

    We need to break the whole project down into manageable chunks. When we get past this we can look at other details, but looking at it all at once is too complicated and the information provided is nowhere near enough for that anyway.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      Another point to make, which is a good general tip, is to develop your MS Word based code within MS Word to start with. There is much more specific help available there for you. It may also be worth checking out Application Automation, but you clearly already know some, if not all, of the information there.

      Comment

      • walid geagea
        New Member
        • May 2011
        • 8

        #4
        Dear NeoPa.
        Thank you for your reply.
        In fact, I just need someone to help me doing the link between the value that is chosen from a drop down list fldCode in an access 2007 form and search for that txt value in the Worddocument that I have chosen.
        Any similar application code would be appreciated.
        I will modify it if necessary

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          If you need assistance you will need to do at least :
          1. Answer questions that have been asked.
          2. Provide information that is necessary even to understand what you are trying to do.
          3. Try to do what you want to do before asking for assistance.

          You are nearly there on #3, but what you have posted doesn't match the problem you are asking for help with. #1 and #2 you have not even started with.

          Coding needs details. Coding won't work by trying to tell the computer a general idea of what you need, so we cannot help with code if there are no details of exactly what you need doing.

          Comment

          • walid geagea
            New Member
            • May 2011
            • 8

            #6
            Dear Neo
            1- The code that I have written is working so far, so the word document opens correctively based on the path that i have already defined.
            2- I have a Form(Masterform atCodfication) field (code) that contains the list of chapters code in a word document.
            3- All that I want is to create a search function in order to search for the value that the user choose in the fldCode and locate this value inside the document after the document is opened, so that the user will make changes in that specific chapter
            I don't have any idea about how to start with that function

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Well Walid, it seems you've at least attempted to answer the questions, and although you haven't yet attempted the code yourself, I can understand that it is of such complexity that even knowing where to start may be difficult.

              I'm not the world's best Word expert as far as the object structure goes (which is fundamental to a question of this type) but I'm game to learn some more about it if you are (and the experience may serve me well later anyway). If this sounds of interest to you then let me know, and attach a copy of what you have so far (starting from scratch would be both heavily time-consuming as well as unhelpful as my solutions may not then fit into your project). That would need to include both the Access database and the Word document. Another limitation is that I currently only work with Office 2003, so you would need to save versions of them in 2003 format.

              This is not something that is likely to progress very quickly, but it may well prove a learning experience for both of us. I'm game if you are.

              Comment

              • walid geagea
                New Member
                • May 2011
                • 8

                #8
                Dear,
                I found out a solution and here is my final code
                Code:
                Option Compare Database
                
                Public Sub OpenWordDoc(strDocName As String)
                    Dim objApp As Object
                
                    'Opens the document
                
                    Set objApp = CreateObject("Word.Application")
                    objApp.Visible = True
                    objApp.Documents.Open strDocName
                    
                End Sub
                
                
                Sub Find()
                 
                [B]Selection.Find.ClearFormatting[/B]   
                 With Selection.Find
                        .Text = code.Value
                        .Replacement.Text = ""
                        .Forward = False
                        .Wrap = wdFindContinue
                        .Format = False
                        .MatchCase = False
                        .MatchWholeWord = False
                        .MatchKashida = False
                        .MatchDiacritics = False
                        .MatchAlefHamza = False
                        .MatchControl = False
                        .MatchWildcards = False
                        .MatchSoundsLike = False
                        .MatchAllWordForms = False
                    End With
                    Selection.Find.Execute
                End Sub
                
                
                
                Private Sub OpenWord_Click()
                DoCmd.Save acForm, "Edit"
                
                'Check to see that there is a document file path associated with the record
                    If IsNull(Me.FilePath) Or Me.FilePath = "" Then
                        MsgBox "Please Ensure There Is A File Path Associated " & _
                        "For This Document", _
                               vbInformation, "Action Cancelled"
                        Exit Sub
                    Else
                        'Check that the document specified in the file path is actually there
                        If (Dir(Me.FilePath) = "") Then
                            MsgBox "Document Does Not Exist In The Specified Location", _
                                   vbExclamation, "Action Cancelled"
                            Exit Sub
                        Else
                            'Opens document in Word
                            Call OpenWordDoc(Me.FilePath)
                
                             Call Find
                    
                     
                        End If
                    End If
                End Sub


                The problem is that when I click on the OpenWord_Click, and when the systems search for the value of the code field and locate it in the Word file, sometimes it works and sometimes it is not and it gives me a Run Time Error '462' the Remote Server machine does not exist or is unavailable.
                I think that I shall define something missing in the word.applicatio n or word.document in the Find Application just before the code highlighted in Bold but do not know what is it??

                Anywyas thank you for your help

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  I'm not sure where you are with this but it doesn't seem the finished article.

                  I suggest we go back to exploring this together, which means we go at the same pace and I will lead you (Going off at your own pace will be unhelpful). If you're happy with this then I suggest that you attach your Word doc to your next post and I will look at designing some example code for you. Please respond directly (Not quickly necessarily, but respond to what I post if you can).

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    To start with, let's assume we have a form with some controls :
                    TextBox - txtWordDoc
                    CommandButton - cmdBrowse
                    MS Common Dialog - mcdBrowse
                    TextBox - txtSearch

                    The following code runs when cmdBrowse is clicked and allows the operator to select a file :

                    Code:
                    Option Compare Database
                    Option Explicit
                    
                    Private Sub cmdBrowse_Click()
                    On Error GoTo Err_cmdBrowse_Click
                        With Me.mcdBrowse
                            .Filter = "All Files (*.*)|*.*|" & _
                                      "Word Documents (*.doc; *.docx)|*.doc;*.docx"
                            .FilterIndex = 2
                            .Action = 1
                            If .FileName > "" Then Me.txtWordDoc = .FileName
                        End With
                        Exit Sub
                    
                    Err_cmdBrowse_Click:
                        Call MsgBox(Prompt:=Err.Description, Title:="cmdBrowse_Click")
                    End Sub
                    I may attach an example database later when we've made some more progress. I really need a copy of a word document that we can work on together though. It helps if we both see the same results.

                    PS. I also had to add a couple of references to the project using the VBA IDE. Here's a screen-shot of my current references. Notice the two at the bottom of the ticked list :

                    [imgnothumb]http://bytes.com/attachments/attachment/5209d1307744347/references.jpg[/imgnothumb]
                    Attached Files
                    Last edited by NeoPa; Jun 10 '11, 10:19 PM. Reason: Added pic.

                    Comment

                    Working...