# Visual Basic > Visual Basic 6 and Earlier >  [RESOLVED] menuItemExecute

## vijy

Am trying to execute a menu item " Documents " in the Adobe Profeesional 7.0



```
Dim Ok as boolean
ok = AcrobatApp.MenuItemExecute("DigSig:ToolsDiff")
```

This not working..
This ok return false only at all time.. 
Can anybody say how to execute this menu??

----------


## Fazi

Would you like to use SendKeys?

----------


## vijy

Without using sendKeys is it  possible to fire this menu?

----------


## Fazi

only other way *i know* is SendMessage / PostMessae  :Smilie:

----------


## vijy

Sorry i don know Send.../post messag..
If any solution in .net too?? U can share..
Could u say why  menuItemExecute not working..??

----------


## vijy

U Pls share how to use post/send message in this application???

----------


## Fazi

vijy,
try this example shows how to execute notepad's Help>About Notepad menu item. Before run this code,keep open your note pad.



```
Option Explicit

Private Const WM_COMMAND = &H111
Private Const MIIM_TYPE = &H10
Private Const MIIM_ID = 2

Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetMenu Lib "user32" (ByVal Hwnd As Long) As Long

Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" _
(ByVal hMenu As Long, ByVal un As Long, ByVal b As Long, lpMenuItemInfo As MENUITEMINFO) As Long

Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
```



```
Sub MenuClick(Hwnd As Long, Menu As Long, Item As Long)
Dim hMenu As Long, hSubMenu As Long, L As Long
Dim M As MENUITEMINFO
If Hwnd Then
   hMenu = GetMenu(Hwnd)
   If hMenu Then
      hSubMenu = GetSubMenu(hMenu, Menu)
      If hSubMenu Then
         M.fMask = MIIM_TYPE Or MIIM_ID
         M.dwTypeData = Space$(128)
         M.cbSize = Len(M)
         M.cch = 128
         L = GetMenuItemInfo(hSubMenu, Item, True, M)
         L = SendMessage(Hwnd, WM_COMMAND, M.wID, ByVal 0)
      End If
   End If
End If
End Sub
```



```
' Example - Open Notepad's About Dialog.
Private Sub Command1_Click()
Dim Lng As Long
Lng = FindWindow(vbNullString, "Untitled - Notepad")
MenuClick Lng, 4, 2 ' 0 based, menu seperators count as an item.
'4 mean, the 5th menu in note pad. 2 mean the 3rd item in the 5th menu. as sad, starts at 0 based.
End Sub
```

How this will give you the idea.

----------


## vijy

Thanks Fazi.. 
I studied this code..Any reference we have to include????

Let me try this in Adobe also...

----------


## Fazi

no referrences.
only you need the handle of the window where that menus are hanging.

----------


## vijy

Thank u...
Let me try and tell u..

----------


## vijy

Hi Fazi.. 
 Am getting problem in send message..

If u have Adobe Profession 7.0 means pls try this... 
1.Open a Pdf File..



```
OnCommand_Click
Dim Lng As Long
Lng = FindWindow(vbNullString, "Adobe Acrobat Professional - [test1.pdf]")
MenuClick Lng, 3, 10
```



```
Sub MenuClick(hwnd As Long, Menu As Long, Item As Long)
Dim hMenu As Long, hSubMenu As Long, L As Long
Dim M As MENUITEMINFO
If hwnd Then
   hMenu = GetMenu(hwnd)
   If hMenu Then
      hSubMenu = GetSubMenu(hMenu, Menu)
      If hSubMenu Then
         M.fMask = MIIM_TYPE Or MIIM_ID
         M.dwTypeData = Space$(128)
         M.cbSize = Len(M)
         M.cch = 128
         L = GetMenuItemInfo(hSubMenu, Item, True, M)
         L = SendMessage(hwnd, WM_COMMAND, M.wID, ByVal 0)
      End If
   End If
End If
End Sub
```

This code u only gave me yesterday..
This not workling...

Could u say watz the prob...

----------


## vijy

If i didnt open any file in Adobe means...
it is...


```
Lng = FindWindow(vbNullString, "Adobe Acrobat Professional")
MenuClick Lng, 3, 10
```

this working Fine..

----------


## Fazi

ok, i can give a temprary solution. i dont have adobe 7. using 8.
just find the window handle using a program like winspector spy, Jocim Anderson Window Finder (in VBF code bank) or the program in my signature.
So if you bring the cursor over tht window, which you want to send the message, you will shown the handle.

with out using find window, you just put the handle directly in SendMessage.
check weather your program works as expected. then you can realize what is the trouble. i mean weather you passing the correct window handle or not.

----------


## RobDog888

You might want to try the Adobe SDK and also register as a member (free) as the info they have really helps with this kind of programming as is not really something that allot of developers do outsite their site. 

I have done some development with it under 5.0 and their pdf documentation manuals are really helpful but you need to register in order to download them and accept the disclosure too.

----------


## vijy

@ RobDogg

Ok Rob, I will register and see..

----------


## vijy

@ Fazi
ya fine, i will try through winspector spy,
Window handle id is a constant or it varies.. i watched the value its differing,

----------


## vijy

@ Fazi..
FindWindow returning a long value and the sendmessage also get executing but the menu only not executing,,
i dont know how to check its correct value or not..

----------


## Fazi

> @ Fazi
> ya fine, i will try through winspector spy,
> Window handle id is a constant or it varies.. i watched the value its differing,


Yes, the handles value is not constant. Changes each time...

----------


## Fazi

> @ Fazi..
> FindWindow returning a long value and the sendmessage also get executing but the menu only not executing,,
> i dont know how to check its correct value or not..


Then there wan't be a problem with the handle. somthing elase in your code.
check the return values of Getmenu api's

----------


## vijy

I checked window handle through spy++, its correct only.. 
Getmenu too returning values.. But the menu only not executing if any files opened in the Adobe... :-(

----------


## Fazi

GetMenu()
If the function succeeds, the return value is the handle of the menu.

GetMenuItemInfo()
If the function succeeds, the return value is nonzero. 
If the function fails, the return value is zero.

GetSubMenu()
If the function succeeds, the return value is the handle of the drop-down menu or submenu activated by the menu item. If the menu item does not activate a drop-down menu or submenu, the return value is NULL.

----------


## RobDog888

I think without using the Adobe SDK you will continue having issues as its not really designed to be automated this way. You need to use some JavaScript and other scripts inside Adobe and/or the document itself.

----------


## vijy

@ RobDogg.
I googled in adobe for the script of this "Documents", they clearly stated that "Documents menu is not automated".... 
I already did some applications using Adobe scripts..


@ Fazi.
Is it possible to execute the menu using process id.... If so means i can execute the Adobe.exe using shell.. and get the process id..

----------


## Fazi

I am not sure about that vijy.
do your code works with notepad if you change the handle to notepad?
if its working with notepad and not working with adobe, you have to check what RobDog advised.  :Smilie:

----------


## RobDog888

With the sdk they show how you can execute and even add your own menu items to Adobes menus. 

With using vb you can create the AcroExch.App object and automate the application. Why doesnt MenuItemExecute work for you? Are you sure you have the correct menuitem id for the desired menu item? This is the most proper and stable way to automate Adobe.

----------


## vijy

Yes rob, am giving the correct value for menuItemExecute.. Its working fine..



In pdf, if i didnt opened any file  in Adobe means,the menu executing.. 

Am getting problem in this case alone " If any pdf file opened in the Adobe,MenuClick not executing "

----------


## vijy

@ Fazi..
This working fine in notepad.. Only in Adobe this not working..


```
Lng = FindWindow(vbNullString, "test1.txt - Notepad")
MenuClick Lng, 0, 1
```

----------


## RobDog888

I dont have Adobe anymore but if it works fine when a document is opened in it and it doesnt work if there is no document loaded then perhaps the menu item is not available unless there is at least one open document?

----------


## vijy

Rob MenuClick works  fine  if the Adobe doesnt open any pdf files.. 

In case 1 MenuClick  Working Fine..
In Case 2, menuClick not working...

*Case 1:*

```
Lng = FindWindow(vbNullString, "Adobe Acrobat Professional")
MenuClick Lng, 3, 10
```

*Case 2:*

```
Lng = FindWindow(vbNullString, "Adobe Acrobat Professional - [test1.pdf]")
MenuClick Lng, 3, 10
```

----------


## RobDog888

Have you used Spy++ to determine the window structure as once documents are opened in it the structure changes. The document opened is a child window and the menu handles may change due to merging of the menus etc. But is the exact window caption in Spy++ as you have it exactly in your code in case #2?

----------


## vijy

Rob,
I googled and got a javascript from Adobe developers for executing menu item..
I think it will work sound to Here 

Let me try this..

----------


## RobDog888

But that is the MenuItemExecute function we discussed earlier in this thread.

----------


## vijy

Lng = FindWindow(vbNullString, "Adobe Acrobat Professional - [test1.pdf]") 
this will return the window handle,,

I checked the winows handle  in spy++  along with the value returned from the find window both are different.. ...

----------


## Fazi

Deleted.

----------


## vijy

:Thumb:  I Got the result.... 
Thanks Fazi... 

When i opened a file in the Adobe,before the "File" menu,an image(Adobe Logo) is there... I didnt counted the image, When passing menu value in the MenuClick hwnd,menu,....

If no file opened in pdf means...there is no Logo before the "File" Menu.. This is the problem..

I attached two images.. jus see it.
Thanks a lot Rob and Fazi...

----------


## vijy

Now i want to click "ok"  in the dialog Box,.. Could u tell which Api to use pls..

----------


## Fazi

SendMessage Button_handle,BM_CLICK,0,0  :Smilie:

----------


## vijy

How to get the Button_Handle of the Particular dialogue box through code???

FindWindow("Button",vbNullString)
 :Confused:

----------


## Fazi

This code shows how to obtain the Notepad About box button handle.



```
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Private Sub Command1_Click()
Dim lngresult As Long
lngresult = FindWindow(vbNullString, "About Notepad")
lngresult = FindWindowEx(lngresult, 0&, vbNullString, "OK") 'OK IS THE Button text
MsgBox lngresult
End Sub
```

Hope this will solve the issue. 
Good Luck

----------


## RobDog888

A modal dialog window has a special class name of #32770. Use Spy++ to verify it and your level of nesting of the OK button. It may not be the first child window  :Wink:

----------


## howdykeith

Great code here ^^

This code in some cases will open a Modal menu and Excel (for example) will keep waiting for the Adobe Menu to be closed before continuing.  Changing to PostMessage opens the menu and continues the code.

You could add:


```
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
```

 and change to:


```
L = PostMessage(Hwnd, WM_COMMAND, M.wID, ByVal 0)
```

(thanks)

----------


## howdykeith

> vijy,
> try this example shows how to execute notepad's Help>About Notepad menu item. Before run this code,keep open your note pad.
> 
> 
> 
> ```
> Option Explicit
> 
> Private Const WM_COMMAND = &H111
> ...


Great code here ^^

This code in some cases will open a Modal menu and Excel (for example) will keep waiting for the Adobe Menu to be closed before continuing.  Changing to PostMessage opens the menu and continues the code.

You could add:


```
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
```

 and change to:


```
L = PostMessage(Hwnd, WM_COMMAND, M.wID, ByVal 0)
```

(thanks, 15 years later)

----------


## howdykeith

> Am trying to execute a menu item " Documents " in the Adobe Profeesional 7.0
> 
> 
> 
> ```
> Dim Ok as boolean
> ok = AcrobatApp.MenuItemExecute("DigSig:ToolsDiff")
> ```
> 
> ...


Another possible solution:




```
Sub MenuItemExecuteLateBind()
   Dim AcroApp As Object
   Dim OpenThisDoc As Object
  
   Set OpenThisDoc = CreateObject("AcroExch.PDDoc")

   Set AcroApp = CreateObject("AcroExch.App")
   'AcroApp.Show

   OpenThisDoc.Open "C:\Temp\MyAdobeFile.pdf"
   'AcroApp.MenuItemExecute ("Open")

   AcroApp.MenuItemExecute ("DigSig:ToolsDiff")
End Sub
```

----------


## howdykeith

> Great code here ^^
> 
> This code in some cases will open a Modal menu and Excel (for example) will keep waiting for the Adobe Menu to be closed before continuing.  Changing to PostMessage opens the menu and continues the code.
> 
> You could add:
> 
> 
> ```
> Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
> ...


^^  Please delete, thanks!

----------

