Page 1 of 1

[Solved] Switch window focus

PostPosted: Thu Mar 10, 2011 7:06 pm
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

Re: Switch window focus

PostPosted: Thu Mar 10, 2011 10:02 pm
by RPG
Hello

I think you can better use OpenForm.

Romke

Re: Switch window focus

PostPosted: Thu Mar 10, 2011 10:29 pm
by packrat386
For what I'm doing I really need to use Writer, Form won't be very helpful for me

Re: Switch window focus

PostPosted: Thu Mar 10, 2011 11:17 pm
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

Re: Switch window focus

PostPosted: Thu Mar 10, 2011 11:27 pm
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   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


Re: Switch window focus

PostPosted: Fri Mar 11, 2011 1:14 am
by packrat386
Thanks so much. that fixed my problem

Re: [Solved] Switch window focus

PostPosted: Wed Jan 31, 2018 1:23 am
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   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