We help IT Professionals succeed at work.
Get Started

Embed a word document on a form programmatically

1,237 Views
Last Modified: 2013-12-20
Here's what I need to do.  I have over 500 word documents that need to be shown on a report (or form... as long as I can print it it's ok).

The problem is that loading the word document takes at least 3 seconds and that there can be up to 9 documents per report making the load time completly unacceptable.

I want to make 500 subforms or subreports where I simply load the word doc and embed it and SAVE IT INTO THE FORM.  Not the db, not a file share.

Meaning that the word application wouldn't need to be called back to redisplay the doc over and over again.
Here's what I have so far.  I make a template form with an ole object on it.
I then start a loop for all the files in a directory.
I copy the master form under a different name.
I open that new form with open args (path of the file to load)... and then this code runs.
 
That works fine, but after I save it and reopen the form, the doc is gone.  I need it to stay there forever without having to reload!
 
Private Sub Form_Load()
On Error GoTo Gestion
    Dim spath As String
    Dim sVars() As String
    If Me.OpenArgs > "" Then 'time to set all the details
        sVars = Split(Me.OpenArgs, ";")
        spath = Right(sVars(0), Len(sVars(0)) - 2)
        With Me.OLEWordDoc
            .Enabled = True
            .Locked = False
            ' Specify what kind of object can appear in the field.
            .OLETypeAllowed = acOLEEmbedded
            ' Class statement for Word document.
            .Class = "Word.Document"
            ' Specify the file to be embedded.
            ' Type the correct path name.
            .SourceDoc = spath
            ' Create the embedded object.
            
            
            
            
            
            'this is where it takes 3-5 seconds
            
            .Action = acOLECreateEmbed
            ' Optional size adjustment.
            .SizeMode = acOLESizeZoom
        End With
    End If
Exit Sub
Gestion:
        MsgBox Err.Description & " : " & Err.Number
End Sub

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 23 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE