Link to home
Start Free TrialLog in
Avatar of Mach1pro
Mach1pro

asked on

Programmatically change WordDoc on a report

I have a small Word document dropped onto a report as an ole unbound object.
I want to be able to change the SourceDoc for this object depending on the value in a particular field. I can't seem to get the report to change from the original document dropped onto it. OLE type is set to "Linked".

Here's some pseudo code of what I used:

If Id = 1 Then
    myDoc.SourceDoc = "C:\Doc1.doc"
Else
    myDoc.SourceDoc = "C:\Doc2.doc"
End if
Avatar of shanesuebsahakarn
shanesuebsahakarn
Flag of United Kingdom of Great Britain and Northern Ireland image

Try putting this line after the code:
myDoc.Action = acOLECreateLink

That should change the information, but you'll have to set the control to enabled/unlocked first and then change it back afterwards.
Avatar of Mach1pro
Mach1pro

ASKER

How do you enable the OLE?

myDoc.Enabled = True   Doesn't work.
Here's some code I used (running from a command button Command2):
Me!OLEUnbound1.Enabled = True
Me!OLEUnbound1.Locked = False
Me!OLEUnbound1.SourceDoc = "C:\temp\test1.doc"
Me!OLEUnbound1.Action = acOLECreateLink
Me!Command2.SetFocus
Me!OLEUnbound1.Enabled = False
Me!OLEUnbound1.Locked = True

The SetFocus line is needed since the Action line sets the focus to the OLE control. I think it must be the Me! that made the difference.
Sorry - I somehow totally missed the part about it being on a report! That's how you'd do it on a form - let me play around with it for a while.
Ok, one way I've found of doing this is to use an unbound OLE object on a form. You can change the sourcedoc property of the OLE object on the form (using the above code), and then set the report's OLE object's OLEData property to that of the form's OLE object. Something like this in your report:

If Me!SomeField = SomeValue Then
   Forms!DummyForm!OLEUnbound1.Enabled = True
   Forms!DummyForm!OLEUnbound1.Locked = False
   Forms!DummyForm!OLEUnbound1.SourceDoc = "C:\temp\test1.doc"
   Forms!DummyForm!OLEUnbound1.Action = acOLECreateLink
   Forms!DummyForm!Command2.SetFocus
   Forms!DummyForm!OLEUnbound1.Enabled = False
   Forms!DummyForm!OLEUnbound1.Locked = True
   Me!ReportOLEObject.OLEData = Forms!DummyForm!OLEUnbound1.OLEData
End If

A bit of a clumsy workaround but it should do the trick.
shanesuebsahakarn,
The code errors out on the last line:
 Me!ReportOLEObject.OLEData = Forms!DummyForm!OLEUnbound1.OLEData

Error 2196.  Can't retrieve the value of this property.
That's weird, it works on my test A97 db. This might be a silly question, but are both ReportOLEObject and OLEUnbound1 unbound OLE objects ? I think the OLEData property only works for unbound objects, not bound ones.
shanesuebsahakarn,
I was able to get your code to work in a report that didn't have any records, but as soon as I included a recordsource to a table or a query, I got the error 2801 :The Ole  object isn't loaded because the unbound Activex control hasn't been initialized.
You're right - I get the same error. I'm starting to wonder if this is possible :(

I'll play around with it a bit more but I'm not hopeful.
shanesuebasahakam,
I'm going to accept one of your previous comments since it did work on an unbound report.  I need to close out my stale questions.  If you come up with a way to make this work on a bound report Please, let me know.
shanesuebasahakam,
I'm going to accept one of your previous comments since it did work on an unbound report.  I need to close out my stale questions.  If you come up with a way to make this work on a bound report Please, let me know.
Well I tried to accept your answer.
Does this mean I HAVE to assign a grade to close out a question?
You do have to assign a grade, but you can just ask for the Q to be deleted if you like - my answer didn't really solve your problem :(
ASKER CERTIFIED SOLUTION
Avatar of Mindphaser
Mindphaser

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial