Link to home
Start Free TrialLog in
Avatar of Sam S
Sam S

asked on

Run Macro from Embedded Word Object in Excel

I have a macro enabled word object inside an excel file and need to run macro from the embedded word file. I stumbled through and can get it to work, but it is not clean or reliable. The code below will run the word macro "PasteText" only if there is another word file open and the embedded file is selected. If the embed isn't selected the text will be pasted into the open word file. If the word program is not open I get error 91: Object variable not set and the debugger takes me to the "PasteText" Macro in word. Would love any help on fixing up my attempt at this!

Sorry if this is more than a little confusing.  End result, I would like to push a button on the excel file and run some excel and word macros. I think I can get that to work, I just need your help cleaning up this bit.  Thank you so much for all your help!

-Sam

Sub WordMarco()

    Dim wdApp As Word.Application
    Set wdApp = GetObject(, "Word.Application")
    wdApp.Visible = True
    wdApp.Run "PasteText"
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sam S
Sam S

ASKER

Hi Rgonzo,

Thank you so much for your help! I seem to still be missing something. When running the code without having word open I get runtime error 91 and the debugger takes me to the word macro "PasteText." If a word file open, it seems to work just fine. Does anyone have any ideas how I can skip having a word program open??

This file is going to many people and I need to eliminate as many steps as possible. Also, attached is my working file if anyone is interested.

Thanks!!
Sample.xlsm
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sam S

ASKER

Thanks for the quick response! Is there anyway to work it so word doesn't have to open?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sam S

ASKER

I had to edit the code to get it to work for me and ended up removing the "wDoc.Application.Visible = True" Line. Thank you so much for your help!!