[Solved] Switch window focus

Creating a macro - Writing a Script - Using the API

[Solved] Switch window focus

Postby packrat386 » Thu Mar 10, 2011 7:06 pm

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
packrat386
 
Posts: 3
Joined: Thu Mar 10, 2011 7:00 pm

Re: Switch window focus

Postby RPG » Thu Mar 10, 2011 10:02 pm

Hello

I think you can better use OpenForm.

Romke
LibreOffice 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2159
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Switch window focus

Postby packrat386 » Thu Mar 10, 2011 10:29 pm

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
packrat386
 
Posts: 3
Joined: Thu Mar 10, 2011 7:00 pm

Re: Switch window focus

Postby RPG » Thu Mar 10, 2011 11:17 pm

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 6.1.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2159
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Switch window focus

Postby Charlie Young » Thu Mar 10, 2011 11:27 pm

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   Expand viewCollapse view
Olddoc = ThisComponent.CurrentController.Frame


So when you do

Code: Select all   Expand viewCollapse view
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   Expand viewCollapse view
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   Expand viewCollapse view
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
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Switch window focus

Postby packrat386 » Fri Mar 11, 2011 1:14 am

Thanks so much. that fixed my problem
OpenOffice 3.1 on Windows 7
packrat386
 
Posts: 3
Joined: Thu Mar 10, 2011 7:00 pm

Re: [Solved] Switch window focus

Postby det » Wed Jan 31, 2018 1:23 am

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   Expand viewCollapse view
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
det
 
Posts: 86
Joined: Fri Mar 31, 2017 8:24 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests