MS-Word from PB with COM Interface

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Nice work guys !
BlairH
New User
New User
Posts: 9
Joined: Sun Jul 27, 2003 11:47 pm

Can not seem to get the Excel example to work.

Post by BlairH »

Can someone please provide a detailed list on getting the Excel Example to work.

I keep getting errors, saying that I have a error in the Pb include file, saying the the structure or interface is already declared double.

Help.

Blair
aXend
Enthusiast
Enthusiast
Posts: 103
Joined: Tue Oct 07, 2003 1:21 pm
Location: Netherlands

Post by aXend »

Could you tell which Interface is declared double? Do you use the Excel.res file or not? I suggest you disable (delete?) the Excel.res file if you're using it.
Generate a new Interface description with the Interface generator. You can include this with your program. If you get remarks over double Interfaces again, you can rename your Interface in the include file. You have to rename references to this Interface too then!
The (simple) example in this post works as it is, without any "Excel.res" file. I suggest you try that first.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Post by LuckyLuke »

Managed to do a find in a word document. However, I'm still having problems with the HomeKey and get_Text functions.
(Trying to make a report-engine using MS-Word.)

Code: Select all

        If oWord\get_Selection(@oSel)  = #S_OK
;          oSel\HomeKey(#wdStory,#wdMove,None)    => does not work
          If oSel\get_Find(@oFind.wdFind) = #S_OK
            oFind\ClearFormatting()
            
            Tag.VARIANT\vt = #VT_BSTR 
            Tag\bstrVal    = SysAllocString_(Ansi2Uni("\<S\>*\</S\>"))                         
            Order.VARIANT\vt = #VT_BSTR 
            Order\bstrVal    = SysAllocString_(Ansi2Uni("Z")) 
            
            ;Try finding <S>*</S> in the document            
            If oFind\ExecuteOld(Tag, vFALSE, vFALSE, vTRUE, vFALSE, vFALSE, vTRUE, vFALSE, vFALSE, Order ,vFALSE, vFalse) = #S_OK
              ;Now we want to obtain the text between the tags ... => does not work
              value.VARIANT\vt = #VT_BSTR   
              If oSel\get_Text(Tekst) = #S_OK
                 Debug Uni2Ansi(value\bstrVal)
              EndIf
            EndIf
          EndIf
        EndIf      
aXend
Enthusiast
Enthusiast
Posts: 103
Joined: Tue Oct 07, 2003 1:21 pm
Location: Netherlands

Post by aXend »

If you look at the description of the HomeKey method

Code: Select all

HRESULT HomeKey(
                [in, optional] VARIANT* Unit, 
                [in, optional] VARIANT* Extend, 
                [out, retval] long* prop);
The first two parameters are VARIANTS. You have to pass VARIANTs. The last parameter is an output parameter. The parameter points to the number of positions that the selection has moved. This means that you should code

Code: Select all

v1.VARIANT\vt = #VT_UI4
v1.value = #wdStory
v2.VARIANT\vt = #VT_UI4
v1.value = #wdMove
oSel\HomeKey(v1,v2,@pos.l)
The same for the get_Text method. The description is

Code: Select all

HRESULT Text([out, retval] BSTR* prop)
The parameter is an output variable. You should code

Code: Select all

If oSel\get_Text(@Tekst.l) = #S_OK
  Debug Uni2Ansi(Tekst)
EndIf
I hope this helps, I wasn't able to test this snippet.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Post by LuckyLuke »

Thanks aXend !

The get_Text is working fine. :D
But the HomeKey still fails. :?
aXend
Enthusiast
Enthusiast
Posts: 103
Joined: Tue Oct 07, 2003 1:21 pm
Location: Netherlands

Post by aXend »

I found that I made some mistakes in the coding. With me this works now:

Code: Select all

  v1.VARIANT\vt = #VT_UI4 
  v1\value = #wdStory 
  v2.VARIANT\vt = #VT_UI4 
  v2\value = #wdMove 
  oSel\HomeKey(v1,v2,@pos.l) 
  Debug pos
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Post by LuckyLuke »

Following code does work :

Code: Select all

v1.VARIANT\vt = #VT_I2
v1\value = #wdStory 
oSel\HomeKey(v1,None,@pos.l)  
but

Code: Select all

v1.VARIANT\vt = #VT_UI4
v1\value = #wdStory 
oSel\HomeKey(v1,None,@pos.l)  
does not work ... :?

I'm using Word 2002,WinXP and PB3.90

BTW Where can I find the description like HRESULT HomeKey( [in, optional] ....
I only found this http://msdn.microsoft.com/library/defau ... omekey.asp

Many thanks.
aXend
Enthusiast
Enthusiast
Posts: 103
Joined: Tue Oct 07, 2003 1:21 pm
Location: Netherlands

Post by aXend »

I have found that it is sometimes try and error to get the right VARIANT type. For me the #VT_UI4 worked. :?

To get the parameter description I built the Interface Generator. This shows interface descriptions, structures and enumerations. You can download it from http://home.planet.nl/~aXend/purebasic/ ... erator.zip.
If you type word.application and click on create interface then you can find the interface description of Selection and if you click on the method HomeKey then you find the description of the parameters.

You can find more info at the forum. See viewtopic.php?t=11031
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 233
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Re: MS-Word from PB with COM Interface

Post by Ajm »

Hi,

Does anybody still have a copy of Interface Generator v1.0 and the Word and Excel .res files stashed away somewhere. I know this is a very old post, I've tried searching but only come back to the same dead links.
Regards

Andy

Image
Registered PB & PureVision User
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: MS-Word from PB with COM Interface

Post by srod »

I have a copy of interface generator. If you pm me an email address then I am happy to email it your way.
I may look like a mule, but I'm not a complete ass.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 233
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Re: MS-Word from PB with COM Interface

Post by Ajm »

Sorry I should have posted back to say I've now got a copy curtesy of mk-soft.

Thanks anyway.
Regards

Andy

Image
Registered PB & PureVision User
Jens-Arne
New User
New User
Posts: 3
Joined: Sun Feb 04, 2024 11:09 am

Re: MS-Word from PB with COM Interface

Post by Jens-Arne »

Hello,

same for me: Is there anyone out there who could email me the zip file(s) since they are no longer available using the original links? They haven't been archived by archive.org either because they weren't publicy linked from http://home.planet.nl/~aXend at the time this homepage was alive.

Thank you very much in advance, Jens-Arne
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: MS-Word from PB with COM Interface

Post by Justin »

Sometime ago i converted the typelib with a tool a wrote, here is the pb include (MSWORD.pbi):
https://github.com/omegakode/PBMSWord

i just tested this basic code and it works:

Code: Select all

EnableExplicit

XIncludeFile "MSWORD.pbi"

Define._Application wdApp

OleInitialize_(0)

If CoCreateInstance_(?CLSID_Application, 0, #CLSCTX_LOCAL_SERVER, ?IID__Application, @wdApp) <> #S_OK
  MessageRequester("Warning:","Couldn't init oWord",0)
  End
EndIf

wdApp\Activate()
wdApp\put_WindowState(#wdWindowStateNormal)
wdApp\put_Width(400)
wdApp\put_Height(300)
wdApp\put_Left(10)
wdApp\put_Top(10)
wdApp\put_Caption("Word from PureBasic")
wdApp\put_Visible(#True)
wdApp\Release()

OleUninitialize_()
Jens-Arne
New User
New User
Posts: 3
Joined: Sun Feb 04, 2024 11:09 am

Re: MS-Word from PB with COM Interface

Post by Jens-Arne »

Thank you very much! The example program opens WinWord without a new document for me (just the main program with the new caption).

Now the big question is: How do I use this interface? A help file of some kind would be great. Probably it's a bit too huge an effort to cover all the functions, but a basic concept would help enormously.

Best regards, Jens-Arne
Post Reply