Using the ContentControlBeforeUpdate event

  • Thread starter Denis Heliszkowski
  • Start date
D

Denis Heliszkowski

Hello All,

I am stuck in a problem since last Wednesday, and I run out of time trying
different things to do what I want.

What I want:

We are currently implementing CRM, and I want to create a quote document and
fill the properties with values from CRM.

What works:

It works like a charm and is the easy part.

I've created a "Document Information Panel" that queries the database, gets
the values for the fields that I want, and updates the properties of the
document. Then the content controls get updated... It's working very well.

What doesn't:

Now I want to bring the detail records from the quote. The problem is that I
don't have a way to update tabular data from the "Document Information
Panel". After some hours of experiences, I ended up with another approach
for this part...

My current solution is to use the ContentControlBeforeUpdate event, and
there re-query the database for the detail records and insert a table with
the correct columns and rows.

Before I started, I thought that it was a simple and inelegant solution but
a working one. Anyway, after all this days of frustration, I don't know what
to do next L

My current problem:

I don't know why, but when I am in this event, there are some things (wich I
don't know what) that I can't do! One of them is simply creating a table and
updating the cells.

My question:

Is there anybody that knows what is happening behind the scenes and can help
me to solve my problem? Or just tell me why can't I change the "#$%&/()= ?
Document ? Or even better point me to another more elegant solution!

I've tried the exactly same code in a macro, and when I call it from the
ALT-F8, it works. But when called from the event, It doesn't

Here is the code that I'm using as an example:

CurrentDocument.Tables.Add Range:=Selection.Range, NumRows:=3,
NumColumns:= 3
Selection.TypeText Text:="1234"
Selection.MoveRight Unit:=wdCell


Thanks is advance,
Denis Heliszkowski
Portugal
 
C

Cindy M.

Hi Denis,

Do I understand correctly: you're trying to insert a TABLE in a Content
Control, and this is failing? What kind of content control is this? If it's a
plain text content control, that would explain why it doesn't work. You can
create a table only in a Rich Text type of content control.
I am stuck in a problem since last Wednesday, and I run out of time trying
different things to do what I want.

What I want:

We are currently implementing CRM, and I want to create a quote document and
fill the properties with values from CRM.

What works:

It works like a charm and is the easy part.

I've created a "Document Information Panel" that queries the database, gets
the values for the fields that I want, and updates the properties of the
document. Then the content controls get updated... It's working very well.

What doesn't:

Now I want to bring the detail records from the quote. The problem is that I
don't have a way to update tabular data from the "Document Information
Panel". After some hours of experiences, I ended up with another approach
for this part...

My current solution is to use the ContentControlBeforeUpdate event, and
there re-query the database for the detail records and insert a table with
the correct columns and rows.

Before I started, I thought that it was a simple and inelegant solution but
a working one. Anyway, after all this days of frustration, I don't know what
to do next L

My current problem:

I don't know why, but when I am in this event, there are some things (wich I
don't know what) that I can't do! One of them is simply creating a table and
updating the cells.

My question:

Is there anybody that knows what is happening behind the scenes and can help
me to solve my problem? Or just tell me why can't I change the "#$%&/()= ?
Document ? Or even better point me to another more elegant solution!

I've tried the exactly same code in a macro, and when I call it from the
ALT-F8, it works. But when called from the event, It doesn't

Here is the code that I'm using as an example:

CurrentDocument.Tables.Add Range:=Selection.Range, NumRows:=3,
NumColumns:= 3
Selection.TypeText Text:="1234"
Selection.MoveRight Unit:=wdCell

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
D

Denis Heliszkowski

Hello Cindy,

Thanks for the answer! Perhaps I didn't explained myself right...

I am not trying to insert a table in the ContentControl, It's far more
simple than that... I am just trying to create a table and add text to it,
anywhere inside the doc. when the contentcontrol changes.

The point here is that whenever I am inside that event, I simply cannot call
those lines of code that I posted. I've tried several different ways, but
all of them fail with different strange error messages.

For example:

If I use the following command:

ThisDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=3

I get:
Run-time error '4605':
This method or property os not available because the current document has
not finished loading.

If I use:
ThisDocument.Tables.Add Range:=ThisDocument.Paragraphs(1).Range, NumRows:=3,
NumColumns:=3

I get:
Run-time error '5887'
Command not available because document is password-protected.

Can you help me ?

Thanks!!!
 
J

Jay Freedman

"ThisDocument" refers to a code module, not to the document itself. The term
you want is "ActiveDocument". Or, better, assign the activedocument to an
object variable of type Document, and use that variable for all further
references:

Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.Tables.Add etc....

None of this has anything to do with content controls, which may indeed have
some quirks as do most new features.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Denis Heliszkowski

Hello Jay,

Thanks for the answer, but I have tried both ways. Using ActiveDocument and
ThisDocument and there is no difference. I have the same problem using
either...

Best regards
 
C

Cindy M.

Hi Denis,

OK, I think I understand what's happening, but I'm not sure I can help...

When a ContentControl updates, it essentially recreates itself in the document.
The XML in the background is being re-written, so the document re-loads. That
explains the error message. And you can actually confirm (sort of) that this is
the case by looking at the method signature for

Private Sub Document_ContentControlBeforeDelete(ByVal OldContentControl As
ContentControl, ByVal InUndoRedo As Boolean)

the reason there's an InUndoRedo parameter in there is because the content
control is deleted and recreated when the user performs these actions *even
within the ContentControl*. (IOW the user isn't deleting the control; that's
not what's being undone or redone.)

If you're not triggering the change in ContentControl content through code,
then I think you're going to have to re-think this. I've tried all the related
events and none of them will let me manipulate the Word document outside the
Content Control while one of these special events is executing. I had thought I
could prehaps use the ContentControlOnExit or OnEnter events to trigger
creation of the table, but I can't force the selection to move during the
Update events...

Unfortunately, I don't understand enough about the your project, and the
circumstances under which the table should be created, to make any suggestions.
Thanks for the answer! Perhaps I didn't explained myself right...

I am not trying to insert a table in the ContentControl, It's far more
simple than that... I am just trying to create a table and add text to it,
anywhere inside the doc. when the contentcontrol changes.

The point here is that whenever I am inside that event, I simply cannot call
those lines of code that I posted. I've tried several different ways, but
all of them fail with different strange error messages.

For example:

If I use the following command:

ThisDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=3

I get:
Run-time error '4605':
This method or property os not available because the current document has
not finished loading.

If I use:
ThisDocument.Tables.Add Range:=ThisDocument.Paragraphs(1).Range, NumRows:=3,
NumColumns:=3

I get:
Run-time error '5887'
Command not available because document is password-protected.

Can you help me ?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
D

Denis Heliszkowski

Hi Cindy,

Thanks for the valuable info! I am giving up of doing it in a medium-elegant
manner :(

It would be so simple if I could access the word document from the Infopath
document information panel...

This was my first wish: Whenever the selection changed I would update the
associated word document from the managed code of the form in InfoPath.

Since it was not possible, my second try was in the document itself as a
event beeing fired as I said. It was not a very clever, but a working
approach. I also tried to update another contentcontrol from inside the
ContentControlBeforeUpdate, but even this didn't work!

Since it's also not possible. I'm thinking in using the BeforeEnter event of
the contentcontrol. Then I'll check the property
ContentControl.ShowingPlaceholderText, and if so I'll query the DB, bring
the records and create the table inside the richtext contentcontol.

It will force the user to enter in the contentcontrol after setting the
quoteid in the Document Information Panel. It's an ugly solution ! But I
think it will work...

Thanks again for your support. If you have any other tips on how to do it
better, I'm glad to know!

rgds,
 
C

Cindy M.

Hi Denis,
If you have any other tips on how to do it
better, I'm glad to know!
On other thought did occur to me, laying awake last
night...

Word VBA has an OnTimer method that you can use to delay
execution of a macro. The ContentControlBeforeUpdate event
method should be able to initialize that. Make the delay
long enough, so that the document has "finished loading",
and it should work.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
D

DH

Well Cindy,

Thanks again!

I swear that I tried :( but it again didn't worked.

If I place the following code in the Document_Open it works like a charm.

Application.OnTime Now + TimeValue("00:00:01"), "UpdateLines"

But whenever it's called from the !"#$%&/() ContentControlBeforeUpdate
event, it throws an error.

Any other try ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top