Results 1 to 7 of 7
  1. #1
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    85

    Specifying a MS Word range as source item for OLE object.

    I am wondering if it is possible to link an OLE object to a specific range in a word document.



    The OLE object will show the top of the document (using .SourceDoc), but I want to be able to display another location further down the document.

    For example, I want the OLE object to show me the contents of the 3rd table, 1st Row, 1st Cell.

    The MS website appears to show how it might work with excel - according to the MS website:
    Code:
    ' Specify source file. 
    OLE1.SourceDoc = "C:\Excel\Oletext.xls"
    ' Specify data to create link to. 
     OLE1.SourceItem = "R1C1:R5C5"
    


    Following this idea my first effort was:
    Code:
    Dim WrdTbl As Word.Table
    Set WrdTbl = ActiveDocument.Tables(3)
    With Forms!Form1.OLETest
        .SourceItem = WrdTbl.Rows(1).Cells(1).Range
    End With
    And then I came to the conclusion that I had zero idea about how to approach the problem. So I'm wondering if anybody out there has a thought.

    Cheers

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    In Word, the Range object is defined by start and end character positions or between 2 objects (e.g. paragraphs). Have never done this, but your range reference looks incomplete based on that definition.

    https://learn.microsoft.com/en-us/of...api/word.range
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    85
    Thanks for the input. Having read that article, it states that Range applies to the start and end position of a paragraph, but can also apply to a cell. Surely it must be easier to refer to a table cell than to a paragraph over which you has almost no control.

    Thanks again.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    I don't think that assessment will always apply. Surely there are cases (e.g. when writing to a document with code) where you have complete control over content and formatting. I guess in your case you use the Cell object.

    Code:
    If ActiveDocument.Tables.Count >= 1 Then _ 
     ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Copy
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,235
    Can you please show us your entire code? Where do you want to this, an Access form or report?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    85
    This following links the word document to the OLE control.
    The Control is placed on a form.
    The word document name is returned via a dialogue function
    The document is linked to the OLE control

    Code:
    'Assume Options and Declarations
    Set WrdTbl = ActiveDocument.Tables(3)
    strDocPath = DocumentSelect 'Dialog Function to return a document path
    Set FrmForm = Me.Form
    With FrmForm.OLEControl
         .OLETypeAllowed = acOLELinked
         .SourceItem = ' ? ? ? is this where and how I identify the range I need - WrdTbl.Rows(1).Cells(1).Range
         .SourceDoc = strDocPath
         .Action = acOLECreateLink
         .SizeMode = acOLESizeClip
    End With
    This displays the document from the beginning of that document. It works just fine.

    I want to display table information location further down the document. Based on the detail referred to in my first post, there is a method for excel that will reference a range.

    I would like to reference a particular range within a Word document - and not just display the beginning of the document.

    I hope this is clear.

  7. #7
    alborg is offline Novice
    Windows 11 Office 365
    Join Date
    Dec 2022
    Location
    Virginia, USA
    Posts
    11
    Quote Originally Posted by journeyman View Post
    This following links the word document to the OLE control.
    The Control is placed on a form.
    The word document name is returned via a dialogue function
    The document is linked to the OLE control

    Code:
    'Assume Options and Declarations
    Set WrdTbl = ActiveDocument.Tables(3)
    strDocPath = DocumentSelect 'Dialog Function to return a document path
    Set FrmForm = Me.Form
    With FrmForm.OLEControl
         .OLETypeAllowed = acOLELinked
         .SourceItem = ' ? ? ? is this where and how I identify the range I need - WrdTbl.Rows(1).Cells(1).Range
         .SourceDoc = strDocPath
         .Action = acOLECreateLink
         .SizeMode = acOLESizeClip
    End With
    This displays the document from the beginning of that document. It works just fine.

    I want to display table information location further down the document. Based on the detail referred to in my first post, there is a method for excel that will reference a range.

    I would like to reference a particular range within a Word document - and not just display the beginning of the document.

    I hope this is clear.
    You really can't link an Access table directly to Word. If you try it manually, clicking on External Data-> New Data Source in the top left corner of your screen you won't be able to link into a Word document manually as you can with a comma delineated text file or to Excel.

    If you want to populate your Access table on the fly with information in a Word document's table, you can loop through a document's table, gather the information, then post it into an Access table using code such as this:

    ' to test this code, put in words into all Column(1) cells, such as "Fido", "Oreo", "Toto", "Gilbert", "Biden".

    For Each acell In .ActiveDocument.Tables(1).Columns(1).Cells
    MsgBox acell.Range.Text ' this will serially pop up the names as your code loops through the table. You can do the same for Columns(2), Columns(3) etc, to populate your Access table in a matter of seconds.
    Next acell

    I tried it, and it works perfectly. I hope that this technique can help you...

    I got the code from https://learn.microsoft.com/en-us/of...d.range.tables

    Regards,
    Al

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error - method 'item' of object 'forms' failed
    By onlylonely in forum Programming
    Replies: 2
    Last Post: 03-12-2020, 08:38 PM
  2. Replies: 4
    Last Post: 10-28-2019, 05:33 PM
  3. Replies: 22
    Last Post: 10-14-2017, 04:23 AM
  4. Replies: 3
    Last Post: 06-27-2017, 12:06 PM
  5. Call word object and import word fields
    By silverspr in forum Programming
    Replies: 3
    Last Post: 12-10-2012, 11:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums