excelvbadde

How to send DDE (Dynamic Data Exchange) information using VBA 64-bit?


SumatraPDF is a light and fast PDF viewer.
One can control SumatraPDF with DDE commands.
https://www.sumatrapdfreader.org/docs/DDE-Commands.

I would like to use VBA to control SumatraPDF.

And here is a demo from Microsoft for DDE application.
https://learn.microsoft.com/en-us/office/vba/api/excel.application.ddeinitiate
I tested it in Excel VBA (64bit).
The error message always says Type mismatch for the line Application.DDEExecute channelNumber, "[FILEPRINT]".
I think I need to convert longPtr type channelNumber to long type.
Could you help to point it out how to do this? Thanks.

** Demo script for DDE**

Case1

Dim channelNumber As LongPtr, the line Application.DDEExecute will have type mismatch error.

Sub testdde()
Dim channelNumber As LongPtr
channelNumber = Application.DDEInitiate( _
 app:="WinWord", _
 topic:="C:\WINWORD\FORMLETR.DOC")
Application.DDEExecute channelNumber, "[FILEPRINT]"
Application.DDETerminate channelNumber
End Sub

Update 20240210

Using K J's code, it works for 64-bit SumatraPDF.

Upadate 20240211

How to select the target window is another challenge. As the below image shows, one has to choose the correct channel to pass the message. topic is one of these parameters to narrow down the channel.
In the post here, it says that topic is used to Describe **something** in the application to which you are opening a channel, usually a document of that application.. But what does something stands for, it's not very clearly documented.
https://learn.microsoft.com/en-us/office/vba/api/excel.application.ddeinitiate
Select data source


Solution

  • Note I am using 32 bit VBA with 32 bit SumatraPDF but you should never try to cross the 32/64bit security boundary. So for 64 bit VBA only use 64 bit SumatraPDF. It should work for most users, either portable or installed.

    To open a file you will need SumatraPDF active and thus actively listening. However, you can command line launch with the first file, then DDE control the viewing.

    You need to watch out for imbedded quotes in VBA.

    Sometimes it pays to open a secondary file before close the previous one otherwise there may be a time period without either.

    Sub Sumatrapdf()
    '
    ' Sumatrapdf Macro
    ' testdde()
    '
    
    Dim channelNumber As Long
    channelNumber = Application.DDEInitiate( _
     app:="SUMATRA", _
     topic:="control")
    Application.DDEExecute channelNumber, "[Open(" & """C:\temp\Burj Khalifa (8).pdf""" & ", 1, 1, 0)]"
    Application.DDETerminate channelNumber
    End Sub
    

    enter image description here

    Many commands without imbedded quotes are much easier, such as

    Application.DDEExecute channelNumber, "[CmdClose]" 
    

    will close the current tab. For efficiency avoid closing the app fully, as it can not respond until fully active in memory.