PDA

View Full Version : How to get an indexed object in COM



marcel
08-11-2007, 13:13
Hi,

I studied the examples but could not find an answer to the next question:

VBA example: ActiveWindow.Panes(2).Close

I have an indexed object in Word such as .....Panes(2)..... How can I both reference the index but also getting the object back with the OLE_Execute() statement.

Is that done with the %TB_DISPATCH_PROPERTYPUTREF constant?

RobertoBianchi
09-11-2007, 09:58
Hi Marcel,

sorry, I don't know MS Word automation, could you please post a simple script sample that I can use to study the problem?

Thanks,
Roberto

marcel
09-11-2007, 10:45
Hello Roberto,

What I'm doing is translating some Word VBA code into thinBasic and see what problems I encounter. I manage in addressing the objects and properties just like the examples but I run into a fence when I have to get an indexed object (collection).

marcel
09-11-2007, 14:38
Hi,

I did some more investigation and found that in this situation there is only one pane in Word. Therfore closing Pane(2) isn't possible and generates an error in VBA and so it will certainly do in thinBasic. In this example it not possible to check if Word is the problem or the thinBasic code.

I will create another example to test this adressing method.

marcel
09-11-2007, 15:12
This is a new version. It doesn't give a error but is also doesn't close the pane.

Michael Clease
09-11-2007, 16:08
' if %wdPaneNone <> vRetVal then 'Check if there is a second pane and close it.

COM_Execute(pWdActWnd, "ActivePane", %TB_DISPATCH_PROPERTYGET, 0, 0, vRetVal)
PWdActPane = vRetVal
msgbox 0, "Activepane value: " & str$(vRetVal)
COM_Execute(pWdActPane, "Close", %TB_DISPATCH_METHOD, 0, 0, 0)

' end if



this works but I dont know why the if doesnt enter, if enabled.

marcel
09-11-2007, 17:13
Hello Abraxas,

Thanks for the help. I have an explanation why the if isn't working. The split screen I created isn't after all a SplitSpecial type. The next types are the types that are gonna closed.


wdPaneComments
wdPaneCurrentPageFooter
wdPaneCurrentPageHeader
wdPaneEndnoteContinuationNotice
wdPaneEndnoteContinuationSeparator
wdPaneEndnotes
wdPaneEndnoteSeparator
wdPaneEvenPagesFooter
wdPaneEvenPagesHeader
wdPaneFirstPageFooter
wdPaneFirstPageHeader
wdPaneFootnoteContinuationNotice
wdPaneFootnoteContinuationSeparator
wdPaneFootnotes
wdPaneFootnoteSeparator
wdPanePrimaryFooter
wdPanePrimaryHeader
wdPaneRevisions


So only when Word is in one of the above states the if clause will be executed. To test the code inside the if construct Word has to be set in one of the above states.

If you change the "<>" in "=" and there is no SplitSpecial type active the Pane collection is empty and the close will generate an error if the pane(2).close or ActivePane.close command is used.

In the example I made there are 2 panes (splitted) and the active pane is closed by your solution. Running the same command again would generate an error. Therefore to test the code Word has to be first put into one of the SplitSpecial states and then the if code will be invoked.

That brings me to my original question. When I want to translate the next line:


ActiveWindow.Panes(2).Close

How do I address the .Panes(2) part. I think it has something to do with the %TB_DISPATCH_PROPERTYPUTREF constant. But I can't find information about it.

--- added --

Maybe I should explain that not the closing of the pane is the goal but the addressing of a member in a collection object.

RobertoBianchi
09-11-2007, 18:43
Hi Marcel,

to get an indexed property you should use %TB_DISPATCH_PROPERTYGET and pass the indexes in the first n elements of parameters (vParam(1)=2) and the number of indexes in number of parameters (4th parameter of COM_Execute()) but again it does't work.
I'm not sure that Panes existing because if you choose close from file menu WORD close the document, instead I can remove the slipt from window menu.
Sorry but i'm not a MS WORD expert! So I'll need more time to study it.

Ciao,
Roberto

marcel
09-11-2007, 19:09
Hi Roberto,

Thanks, I will test it. I do not understand your sentence: "I'm not sure that Panes existing because if you choose close from file menu WORD close the document, instead I can remove the slipt from window menu." What do you mean with "instead I can remove the slipt ..."? Added: Oh I get it, you mean Split.


Sorry but i'm not a MS WORD expert! So I'll need more time to study it.

Take your time Roberto, I tinker with the info you gave.

marcel
09-11-2007, 22:24
Hi,

I got it working but there is a macro recorded Word construction I do not understand quite right at the moment. I'm working for more than 10 years with Word and took the code Word produced always for granted. In this case I have my thoughts. ::)

Nevertheless I got the adressing working. It must be done with the item property, see the code. I also add an image of the panes().

Petr Schreiber
09-11-2007, 23:32
Hi Marcel,

just tested the script in Word 2007 ( yes, that with ... unusual interface :) ) and all seems to work as it should.
Good work !


Petr

RobertoBianchi
10-11-2007, 15:18
Hi Marcel,

I'm sorry for my typo (slipt instead of split)!
I tested your script and now it work but I don't understand why SplitSpecial returns 0.
If I made the call directly (I mean from the module) the results is the same, but strangely enough in both cases the function seems performed correctly.
You can test it with:



msgbox 0, COM_Succeeded(COM_Execute(pWdView, "SplitSpecial", %TB_DISPATCH_PROPERTYGET, 0, 0, vRetVal))



However, I'm working on setting an indexed property.

Bye,
Roberto

marcel
10-11-2007, 17:55
Hi Roberto,

Thanks for the testcode. I have the same problem as you with the SlitSpecial code. As I mentioned above the code I used was a recording done by Word. I created a macro to open the header. In the code the splitspecial property came along. But in all circumstances in Word it is always 0.

I made another test and now I got another value for the SplitSpecial property when I first put the Word screen in the primary footer state.


Sub header()
'VBA code below
'
ActiveDocument.ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter 'This command opens a special footer window
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="test"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

About the indexing problem, I got it wotking with the .Item property.

RobertoBianchi
11-11-2007, 10:09
Hi Marcel,

as far as I understand SplitSpecial property should gets or sets the active window pane, so may be in our case the active window pane is 0, so we should first try to set as active the window pane nr.1 but I must finish to accommodate the function.


About the indexing problem, I got it wotking with the .Item property
Did you mean set the .Item property or get or both?

Ciao,
Roberto

marcel
11-11-2007, 11:50
Hi Roberto,

About the SplitSpecial issue you are right. With all the information I gathered uptil now and tests I made, it was after all simple. The reason that the code wasn't invoked was due to fact that the SplitSpecial and wdPaneNone were still the same.


If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

When a user is working in Word the user can get the screen in a certain state, see the SplitSpecial types above in the answer for Abraxas. However, I could not get Word in this state only with VBA code, see next example vba code.


'VBA Code
ActiveDocument.ActiveWindow.View.SplitSpecial = wdPanePrimaryFooter 'This command opens a special footer window

So this is a simple property setting and it works when I assign one of the wd-constants. In fact the problem is solved now.

About the adddressing of the index. I tested a lot with different constructions and the next one works when I use the Item property in VBA. It is possible to use the short way of adressing:


ActiveWindow.Panes(2).Close

and the extended way with the Item property.


temp = ActiveWindow.Panes.Item(2)
temp.close

and resulted in the next thinbasic code:



'thinBasic code
'Now the addressing starts!
vParam(1) = 2
COM_Execute(pWdPanes, "Item", %TB_DISPATCH_METHOD, 1, vParam, vRetVal) 'This works

'vRetVal holds the indexed pane value.

pWdPane = vRetVal
COM_Execute(pWdPane, "Close", %TB_DISPATCH_METHOD, 0, 0, 0)


So in the end we can call just one object from a colection of objects. In this case it is a Pane but if there are more than one documents active we can get one particular document and make it active and work with it. The problem was which thinbasic syntax I had to use to 'connect' the object model. But you must tell me if what I made is correct, I have no knowledge about it, I just combining syntaxes and see what happens. ;D

Now it can. That means also that constructions with
for each obj in objects must be rewritten into loop constructions. :)

------ Added -------------

To answer your question: Did you mean set the .Item property or get or both? >>>> BOTH

marcel
12-11-2007, 14:34
Hi,

How can I prevent those &#38;#160;&#38;#160 characters in the code boxes when I edit the text after I've posted it? >:(

Petr Schreiber
12-11-2007, 15:09
Not sure what they are,

try to "modify message", delete them and place there spaces (?)


Bye,
Petr

RobertoBianchi
12-11-2007, 15:22
Marcel

maybe you used the small edit icon on the right, try to use the Modify message link on the top of post.

Ciao,
Roberto

ErosOlmi
12-11-2007, 16:03
Sometimes I've noted too that color codes in colored syntax.
Usually it is enough to re-edit the message and save again to see them disappear.
Seems a problem of the syntax color engine. Sometimes it get confused by some code

marcel
12-11-2007, 16:04
@Petr: Yes replacing them works fine. I have done this all the time but now I want to prevent this.&#160; ;)

@Roberto: Yes, I used the little edit Icon. I'll try the modify message link.&#160; :)

--- Ok, that works better.