Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Display Word Document In Form

From Wiki

Jump to: navigation, search

This function shows how a Word document could be displayed in an unbound control on a form.

You will need:

1. A form with:

a. Unbound control, let us call it oleShowDoc

It is probably easiest to choose Create Object From File.

b. A textbox for the document file name. let us call this txtFileName

c. Some code associated with the form, let us use the Current Event.

  Private Sub Form_Current()
     DisplayDoc Me.oleShowDoc, Me.txtFileName
  End Sub

2. A Function

  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
  
  With ctlDocControl
      If Trim(strDocPath & " ") = "" Then
          .Visible = False
          strResult = "No document name specified."
      Else
          If InStr(1, strDocPath, "\") = 0 Then
              ' Path is relative
              strDatabasePath = CurrentProject.FullName
              intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
              strDatabasePath = Left(strDatabasePath, intSlashLocation)
              strDocPath = strDatabasePath & strImagePath
          End If
          .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
          ' Range statement--optional for Excel worksheet.
          '.SourceItem = "R1C1:R7C4"
          ' Create the linked object.
          .Action = acOLECreateLink
          ' Optional size adjustment.
          .SizeMode = acOLESizeZoom
          strResult = "Document found and displayed."
      End If
  End With
      
  Exit_DisplayDoc:
      DisplayDoc = strResult
      Exit Function
  
  Err_DisplayDoc:
      Select Case Err.Number
          Case 2101       ' Can't find the picture.
              ctlDocControl.Visible = False
              strResult = "Can't find document."
              Resume Exit_DisplayDoc:
          Case Else       ' Some other error.
              MsgBox Err.Number & " " & Err.Description
              strResult = "An error occurred displaying document."
              Resume Exit_DisplayDoc:
      End Select
  End Function
  

Further Information

How to display images from a folder in a form, a report, or a data access page: http://support.microsoft.com/kb/285820/

ACC2000: How to Programmatically Link or Embed an Object on a Form: http://support.microsoft.com/kb/209990

416 Rating: 2.3/5 (15 votes cast)