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
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
ASKER
How do you enable the OLE?
myDoc.Enabled = True Doesn't work.
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.
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!OLEUnbound 1.Enabled = True
Forms!DummyForm!OLEUnbound 1.Locked = False
Forms!DummyForm!OLEUnbound 1.SourceDo c = "C:\temp\test1.doc"
Forms!DummyForm!OLEUnbound 1.Action = acOLECreateLink
Forms!DummyForm!Command2.S etFocus
Forms!DummyForm!OLEUnbound 1.Enabled = False
Forms!DummyForm!OLEUnbound 1.Locked = True
Me!ReportOLEObject.OLEData = Forms!DummyForm!OLEUnbound 1.OLEData
End If
A bit of a clumsy workaround but it should do the trick.
If Me!SomeField = SomeValue Then
Forms!DummyForm!OLEUnbound
Forms!DummyForm!OLEUnbound
Forms!DummyForm!OLEUnbound
Forms!DummyForm!OLEUnbound
Forms!DummyForm!Command2.S
Forms!DummyForm!OLEUnbound
Forms!DummyForm!OLEUnbound
Me!ReportOLEObject.OLEData
End If
A bit of a clumsy workaround but it should do the trick.
ASKER
shanesuebsahakarn,
The code errors out on the last line:
Me!ReportOLEObject.OLEData = Forms!DummyForm!OLEUnbound 1.OLEData
Error 2196. Can't retrieve the value of this property.
The code errors out on the last line:
Me!ReportOLEObject.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.
ASKER
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.
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.
I'll play around with it a bit more but I'm not hopeful.
ASKER
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.
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.
ASKER
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.
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.
ASKER
Well I tried to accept your answer.
Does this mean I HAVE to assign a grade to close out a question?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.