We help IT Professionals succeed at work.

Run Macro from Embedded Word Object in Excel

425 Views
Last Modified: 2017-03-30
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for the quick response! Is there anyway to work it so word doesn't have to open?
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.