+ Reply to Thread
Results 1 to 13 of 13

Toggle between Excel and PDF file

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Toggle between Excel and PDF file

    Hello,
    I have a text selected in a PDF file; at the same time I have an open Excel file.
    I want a macro which would Copy the selected text from the PDF and Paste it in my active cell in Excel.
    Is this possible with an excel macro?
    Thanks

  2. #2
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    Before trying this - see note below the code
    Code assumes that you have
    1) selected relevant text and hit ctrl + C in your pdf program, and
    2) re-activated your workbook

    Please Login or Register  to view this content.

    NOTE
    (from http://excelhelphq.com/)
    You can access and retrieve the data in the clipboard into a variable in Excel VBA, using the Microsoft Forms library. In addition to using the above code , you must follow the these steps and activate Microsoft Forms 2.0 Object Library for the code to work. You only need to do it once as long as this macro stays in the same file.

    On the Macro editor menu bar on the top, click on “Tools”
    In the menu, click on “References”
    Click the “Microsoft Forms 2.0 Object Library”
    Press OK
    If you don’t follow these steps, you will get a “Compile error: User-defined type not defined” error… Lots of people have been stumped on this part.

    (you are my first post on this site!!)
    Last edited by Kevin#; 01-03-2016 at 02:48 PM. Reason: First Post

  3. #3
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Re: Toggle between Excel and PDF file

    Kevin#,
    Thanks for the response; it works however, if I have to manually reactivate my workbook, than I just could do Ctrl + Paste, instead of running the macro.
    I am wondering if, after doing the Ctrl + Copy on the selected PDF text, VBA could automatically reactivate the workbook and than do the "paste".
    The way I picture this is:
    - While I am in the excel sheet, I run he macro from there.
    - VBA would jump into the PDF window where my text is already selected.
    - VBA would do the Ctrl + C
    - VBA jump back into the Excel and paste value.
    I know there are automation programs which could do this but I am wondering if this is achievable without that.
    Thanks

  4. #4
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    Simplest solution would appear to be:
    - select and copy PDF text to clipboard
    - open workbook
    - save clipboard text to a a special variable in VBA
    - run any other other sub-routines
    - paste variable value (ie PDF clipboard text) to relevant cell within relevant sub-routine
    - continue ...

    How does that sound?

  5. #5
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Re: Toggle between Excel and PDF file

    Meantime, I have tried an other approach..
    using from your web site the SendKey idea;
    this could do the work, but interestingly, if I do the Alt+Tab manually, than the approach seems to work but within the VBA code, the SendKeys "%{TAB}" does nothing.
    Here is my entire code, I can't figure out why is not doing the job:
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    Looks like it could also work provided you can ensure you tab to the correct place.
    Will test your code tomorrow.
    How about activating the workbook window instead of tabbing back?
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Re: Toggle between Excel and PDF file

    The "workbook activate" works fine; the only problem is the
    Please Login or Register  to view this content.
    Thanks!

  8. #8
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    Have made good progress.
    Everything works on amended code EXCEPT the copying to the clipboard. (I will investigate next)
    Before running this, close all windows except PDF and Excel windows (and only have the one Excel file open)

    The code would be more robust (and allow other windows to remain open) if we knew the name of the PDF window - could then replace Application.WindowState = xlMinimized with Windows("YourPDFFile.xlsm").Activate
    Will the PDF name be the same each time?

    Please Login or Register  to view this content.
    Last edited by Kevin#; 01-04-2016 at 05:51 AM.

  9. #9
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    Will you be running several procedures or just a single one (in Excel) after selecting your PDF text?
    If it is only a single procedure, then this should do the trick.
    (Otherwise must declare strPDF as a public variable to enable VBA to remember its value as the other procedures are run)

    Test this without any amendments first and then insert the rest of your code and test again
    1st: open pdf window, select text AND copy to clipboard and
    2nd: toggle to excel window and execute the macro


    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    I have (briefly) tested another approach which involves opening a pdf file from within the macro. And this has borne fruit - doing it this way has enabled sendkeys ^a, ^c & ^v - the contents of the whole pdf was successfully copied and pasted automatically into the worksheet.

    I now await your comments on posts #08 and #09 before I do anything else.
    Also are you using Adobe Reader to open the pdf file?
    And will the pdf FILENAME and/or FILEPATH vary?

    thanks
    Last edited by Kevin#; 01-04-2016 at 02:32 PM.

  11. #11
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    PDF file is opened and ALL text copied to a temporary worksheet (sub ONE)
    User can select multiple ranges of text for pasting to chosen cell (sub TWO)
    VBA runs so fast that it gets ahead of itself – hence line Application.Wait (Now + TimeValue("0:00:01"))

    Text selection is made in Excel rather than pdf. (amend to use Word if easier).
    Amend code for your pdf application and pdf file name. (sub ONE)
    Amend chosen cell/sheet (sub ONE)


    Please Login or Register  to view this content.
    Last edited by Kevin#; 01-06-2016 at 10:47 AM.

  12. #12
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: Toggle between Excel and PDF file

    Have just noticed this solution on MrExcel's site.
    This appears to do exactly what you want, in the way you want to do it. But it requires Adobe Acrobat Professional rather than Acrobat Reader.
    http://www.mrexcel.com/forum/excel-q...ions-code.html

  13. #13
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    593

    Re: Toggle between Excel and PDF file

    Kevin#,
    Thank for your help.
    I was able to make it work; it was helpful mainly the ("^c"); my mistake was that I had the parenthesis within the " sign;
    Here is my code:
    [CODE][CODE]Sub PasteFromPDF()
    Dim CurrentWS As String
    Dim keys As String
    Dim objData As New MSForms.DataObject
    Dim strText As String

    'you must add the reference: Tools/References/ “Microsoft Forms 2.0 Object Library”

    AppActivate "Microsoft Excel"
    CurrentWS = ActiveSheet.name
    AppActivate "Adobe Reader"
    For i = 1 To 100000000
    'delay
    Next i
    keys = ("^c")
    Set wsh = CreateObject("WScript.Shell")
    wsh.SendKeys keys, True
    Worksheets(CurrentWS).Activate
    For i = 1 To 100000000
    'delay
    Next i
    objData.GetFromClipboard
    strText = objData.GetText()
    ActiveCell.value = objData.GetText()
    ActiveCell.Offset(1, 0).Select
    ActiveCell.EntireRow.Insert
    End Sub[CODE][CODE]


    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Toggle Native Excel settings
    By freeofcost in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-14-2014, 11:08 AM
  2. Toggle Button Does not Toggle OFF when Clicked
    By robbfamily in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-19-2014, 05:12 PM
  3. Toggle Button in Ribbon Excel UI - After Reopening file it is not pressed
    By marzoumm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-02-2014, 09:33 AM
  4. Replies: 1
    Last Post: 02-13-2014, 10:55 PM
  5. Toggle Button Open/Close PDF File
    By Nu2Java in forum Excel General
    Replies: 0
    Last Post: 10-09-2012, 02:12 PM
  6. [SOLVED] program a toggle between worksheets in the same file
    By K! in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2006, 11:38 AM
  7. [SOLVED] How do I toggle between worksheets in the same Excel file ?
    By Sameer in forum Excel General
    Replies: 1
    Last Post: 11-03-2005, 01:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1