+ Reply to Thread
Results 1 to 14 of 14

Excel VBA not executing in Word

  1. #1
    Registered User
    Join Date
    10-19-2017
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    5

    Excel VBA not executing in Word

    I have a VBA program in Excel that I would like to use to perform a Find and Replace with some values gathered from my Excel doc into a template Word document. The Word doc correctly opens, but does not execute the Find and Replace. Here's my code:

    Please Login or Register  to view this content.
    Any idea why this isn't working? I've added in the Word Object Library in Excel for early binding and I grabbed the code directly from a Word macro and then edited it to perform from Excel. Thanks for any help!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA not executing in Word

    Have you checked what Selection is when the Word document is opened?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Excel VBA not executing in Word

    I used to find that I had to open the word document using Excel VBA and get that to run a Word VBA macro.

    I have not done that for year though.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  4. #4
    Registered User
    Join Date
    10-19-2017
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel VBA not executing in Word

    How do I make sure it's "selecting" the entire document to do the Find and Replace?

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA not executing in Word

    Any chance you could upload a sample document?

    Click on GO ADVANCED, scroll down and click Manage Attachments.

  6. #6
    Registered User
    Join Date
    10-19-2017
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel VBA not executing in Word

    How can I send it to you personally?

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA not executing in Word

    All I was looking for really was something to work with rather than having to create something myself, just being lazy I suppose.

    Can't you upload something with dummy data here?

  8. #8
    Registered User
    Join Date
    10-19-2017
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel VBA not executing in Word

    Not easily, unfortunately. My company's branding is plastered all over the place on both documents. I'd rather keep that under wraps from the public if I could. Sorry!

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA not executing in Word

    Couldn't you just open a blank document, stick some example data in it, save it and upload it here?

    I would do it myself but don't have time right now I'm afraid.

  10. #10
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Excel VBA not executing in Word

    Quote Originally Posted by smit3446 View Post
    I've added in the Word Object Library in Excel for early binding and I grabbed the code directly from a Word macro and then edited it to perform from Excel
    Properly coded, there is no need for early binding. Using wdFindAsk will also stop the code's execution while it waits for you to answer a prompt you're probably not seeing. Working with Selections is also incredibly inefficient. Try:
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  11. #11
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Excel VBA not executing in Word

    Cross-posted at: https://www.mrexcel.com/forum/genera...ting-word.html
    Please read Excel Forum's Cross-Posting policy in rule 8: http://www.excelforum.com/forum-rule...rum-rules.html

  12. #12
    Registered User
    Join Date
    10-19-2017
    Location
    Minnesota
    MS-Off Ver
    2013
    Posts
    5

    Re: Excel VBA not executing in Word

    I appreciate the code and it seems to make sense to me, but it's still not performing correctly. Now the header/footers, where [Company] and [Date] are located, are getting deleted altogether. The text box which contains all three [Company], [Date], and [Event] variables is unchanged.

    Not sure what's going on there.

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Excel VBA not executing in Word

    Quote Originally Posted by smit3446 View Post
    it's still not performing correctly. Now the header/footers, where [Company] and [Date] are located, are getting deleted altogether.
    Since the code isn't touching any headers/footers, it isn't responsible whatever issues you're having there.
    Quote Originally Posted by smit3446 View Post
    The text box which contains all three [Company], [Date], and [Event] variables is unchanged.
    You previously mentioned noting about any textbox; it makes a world of difference that that's where the content is. Change:
    With .ActiveDocument.Range.Find
    to:
    With .ActiveDocument.Shapes(1).TextFrame.TextRange.Range.Find
    where 1 is the number of whichever Shape object in the document your textbox is.

  14. #14
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Excel VBA not executing in Word

    FWIW, If you either bookmarked your [Company], [Date], and [Event] strings (or simply replaced them with bookmarks) named Company, Date, and Event, respectively, you could reduced the code to:
    Please Login or Register  to view this content.

+ 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. Excel for Mac is not executing macro
    By kevivu in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-31-2015, 01:55 PM
  2. Executing a Mail Merge with An Embedded Word File
    By GazB in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2013, 07:06 AM
  3. Excel 2007 : Difficulties executing excel commands
    By Thembinkosi in forum Excel General
    Replies: 2
    Last Post: 01-18-2011, 03:49 AM
  4. Finding a specific word in a cell and executing an array
    By jgray152 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-10-2010, 07:49 PM
  5. Executing PL/SQL in Excel
    By scos00 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-09-2005, 07:20 AM
  6. Executing of excel Macros.
    By KarthikK in forum Excel General
    Replies: 4
    Last Post: 06-28-2005, 07:00 AM
  7. Executing a procedure from a excel buttom
    By Ken Scanlon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-05-2005, 10:26 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