[Solved] Switch window focus

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
packrat386
Posts: 3
Joined: Thu Mar 10, 2011 7:00 pm

[Solved] Switch window focus

Post by packrat386 »

Hi, Im trying to write a macro that would open a new document, assign that document to an object and then switch back to the old wind, using writer

I'm having trouble switching back to the old window my code is


sub switch
Dim Olddoc as Object
Dim Oldpath as string
Oldpath = thiscomponent.url
Olddoc = ThisComponent.CurrentController.Frame

rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:AddDirect", "", 0, Array())

Speech = ThisComponent.CurrentController.Frame

Olddoc.CurrentController.Frame.ContainerWindow.toFront()

end sub


the error code i get says "CurrentController" not recognized

If someone sees the problem, or has another solution I would be very grateful. Thank You
Last edited by packrat386 on Fri Mar 11, 2011 1:15 am, edited 1 time in total.
OpenOffice 3.1 on Windows 7
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Switch window focus

Post by RPG »

Hello

I think you can better use OpenForm.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
packrat386
Posts: 3
Joined: Thu Mar 10, 2011 7:00 pm

Re: Switch window focus

Post by packrat386 »

For what I'm doing I really need to use Writer, Form won't be very helpful for me
OpenOffice 3.1 on Windows 7
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Switch window focus

Post by RPG »

Hello

I was wrong to think you use a database document.
I do not understand your macro. So I do not understand what you want.

It seems to me you must better understand how you can use thiscomponent. Maybe you can also use Currentcomponent.

When you use a button in both documents then you can do it also with openform as where I point and use the method of Villeroy.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Switch window focus

Post by Charlie Young »

packrat386 wrote:Hi, Im trying to write a macro that would open a new document, assign that document to an object and then switch back to the old wind, using writer

I'm having trouble switching back to the old window my code is


sub switch
Dim Olddoc as Object
Dim Oldpath as string
Oldpath = thiscomponent.url
Olddoc = ThisComponent.CurrentController.Frame

rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:AddDirect", "", 0, Array())

Speech = ThisComponent.CurrentController.Frame

Olddoc.CurrentController.Frame.ContainerWindow.toFront()

end sub


the error code i get says "CurrentController" not recognized

If someone sees the problem, or has another solution I would be very grateful. Thank You
A problem with your code is, Olddoc is assigned by

Code: Select all

Olddoc = ThisComponent.CurrentController.Frame
So when you do

Code: Select all

Olddoc.CurrentController.Frame.ContainerWindow.toFront()
you are asking for the CurrentController.Frame of the CurrentController.Frame, which doesn't exist.

With your switch sub, Just do

Code: Select all

Olddoc.ContainerWindow.toFront()
I usually like to do these things without the dispatcher stuff. Here MakeArg is just a helper function, but LoadNewDoc loads a new Writer doc, then switches back and forth a couple of times just to show the possibilities.

Code: Select all

Function makeArg As com.sun.star.beans.PropertyValue
	makeArg = createUnoStruct("com.sun.star.beans.PropertyValue")
End Function

Sub LoadNewDoc
	Dim Doc As Object
	Dim NewDoc As Object
	Dim Args(1)
	Dim ArgCount As Integer
		
	Doc = ThisComponent
						
	ArgCount = 0
 	Args(ArgCount) = MakeArg
 	Args(ArgCount).Name = "MacroExecutionMode"
 	Args(ArgCount).Value = 4
 	ArgCount = 1
 	Args(ArgCount) = MakeArg
 	Args(ArgCount).Name = "Hidden"
 	Args(ArgCount).Value = False
 	
 	NewDoc = StarDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, Args)
 	Wait 5000
 	Doc.CurrentController.Frame.ContainerWindow.toFront()
 	Wait 5000
 	NewDoc.CurrentController.Frame.ContainerWindow.toFront()
 	Wait 5000
 	Doc.CurrentController.Frame.ContainerWindow.toFront()
					
End Sub

Apache OpenOffice 4.1.1
Windows XP
packrat386
Posts: 3
Joined: Thu Mar 10, 2011 7:00 pm

Re: Switch window focus

Post by packrat386 »

Thanks so much. that fixed my problem
OpenOffice 3.1 on Windows 7
det
Posts: 86
Joined: Fri Mar 31, 2017 8:24 pm

Re: [Solved] Switch window focus

Post by det »

My aim is to load and manipulate 2 Docs and and see the results. My Code is an extension of the previous one.
Det
Here it is:

Code: Select all

Load/Manipulate 2 Docs
 Sub SwitchDocs
   Dim Doc1, Doc2 As Object
   Dim Args(1)
   Dim ArgCount As Integer
    
   ArgCount = 0  
   Args(ArgCount) = MakeArg     'See Function at the End
   Args(ArgCount).Name = "MacroExecutionMode"
   Args(ArgCount).Value = 4
   ArgCount = 1
   Args(ArgCount) = MakeArg
   Args(ArgCount).Name = "Hidden"
   Args(ArgCount).Value = False
 
 '   XXXXXXX Load  Doc1 & Doc2   XXXXXXX
   
   Url = convertToURL( "/Users/imac/MyLib/1 Freq/Doc1.ods")
   Doc1 = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Args)
   Url = convertToURL( "/Users/imac/MyLib/1 Freq/Doc2.ods")
   Doc2 = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Args)
  
  '   XXXXXXX Activate Doc1   XXXXXXX 
  
   Doc1.CurrentController.Frame.ContainerWindow.toFront()
   Doc1.CurrentController.Frame.Activate()
   
  '   XXXXXXX Activate Doc2 and load a Cell   XXXXXXX
  
   Doc2.CurrentController.Frame.ContainerWindow.toFront()
   Doc2.CurrentController.Frame.Activate()
   SN=1
   CN=1
   RN=1
   Sheet = Doc2.Sheets(SN)
   Cell = Sheet.getCellByPosition(CN,RN)
   Cell.Value=111
   
     '   XXXXXXX Show the Cell   XXXXXXX
     
   Call ShowActive(SN,CN,RN)
   Print "111 in Doc2 S1,C1,R1?"
   
   '   XXXXXXX Activate Doc1 and load a Cell   XXXXXXX
  
   Doc1.CurrentController.Frame.ContainerWindow.toFront()
   Doc1.CurrentController.Frame.Activate() 
   SN=2
   CN=3
   RN=5
   Sheet = Doc1.Sheets(SN)
   Cell = Sheet.getCellByPosition(CN,RN)
   Cell.Value=511

   Call ShowActive(SN,CN,RN)
   Print "511 in Doc1 S2,C3,R5?"
   Print "Done"            
   End Sub

 '   XXXXXXX  The Function   XXXXXXX
  
  Function makeArg As com.sun.star.beans.PropertyValue
  makeArg = createUnoStruct("com.sun.star.beans.PropertyValue")
  End Function

 '   XXXXXXX Sub to show the Active Cell   XXXXXXX
  
  Sub ShowActive(SN,CN,RN)
  Doc = ThisComponent
  Sheet = Doc.Sheets(SN)
  ThisComponent.CurrentController.Select(Sheet)
  Cell = Sheet.getCellByPosition(CN,RN)
  ThisComponent.CurrentController.Select(Cell)
  End Sub
OpenOffice 4.1.5 on Mac Sierra 10.13.1
Post Reply