Unbound OLE object in report update

Posted on 2011-10-27
Last Modified: 2013-11-05
Hi Experts,

I have i report where i link to a word file through a OLE object. But i would like to change the link to another file  through VBA code. Can someone give an example how to do that?
    LVL 12

    Accepted Solution

    You can use a Function like this below, found in this thread:

    Public Function DisplayDoc(ctlDocControl As Control, strDocPath As Variant) As String
    On Error GoTo Err_DisplayDoc
    Dim strResult As String
    Dim strDatabasePath As String
    Dim intSlashLocation As Integer
    Dim strImagePath
    With ctlDocControl
     .Visible = True
     .Enabled = True
     .Locked = False
     ' Specify what kind of object can appear in the field.
     .OLETypeAllowed = acOLELinked
     ' Class statement--optional for Excel worksheet.
     .Class = "Microsoft Word Document"
     .SourceDoc = strDocPath
     .Action = acOLECreateLink
     ' Optional size adjustment.
     .SizeMode = acOLESizeZoom
    strResult = "Document found and displayed."
    End With
    DisplayDoc = strResult
    Exit Function
     Select Case Err.Number
       Case 2101       ' Can't find the picture.
     ctlDocControl.Visible = False
       strResult = "Can't find document."
     Resume Exit_DisplayDoc:
          Case 2455
            Resume Next
          Case Else       ' Some other error.
            MsgBox Err.Number & " " & Err.Description
            strResult = "An error occurred displaying document."
          Resume Exit_DisplayDoc:
      End Select
     End Function

    Open in new window

    Then in you can do something like :
    Dim strWordDoc As String  
    Dim ctl As Access.Control  
    ' or use the Common dialog to selct the new doc
    strWordDoc = "C:\YourPathName\YourFileName.docx"
    Set ctl = Me.ctlOleDoc  'chang the OLE object nam accordingly
    DisplayDoc ctl, strWordDoc

    Open in new window

    Hope this helps,

    Author Closing Comment

    Just what i was looking for.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    In the previous article, Using a Critera Form to Filter Records (, the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now