+ Reply to Thread
Results 1 to 9 of 9

Using Excel to run a Word Macro

  1. #1
    Registered User
    Join Date
    03-08-2016
    Location
    Newport
    MS-Off Ver
    2010
    Posts
    17

    Angry Using Excel to run a Word Macro

    I have an excel document that lists all of the documents in a given directory/folder. The file path is saved into column B. I have a macro that opens the document who's file path is in column B2.

    I have a WORD document that copies the header and footer from a template document (open in the background)into a letter that is open (macro run from the letter which is the active document).

    Both macros work independently - BUT when I run the macro from Excel and open the word document (a letter) the Word Macro will not run automatically - when I do run the word macro manually it then doesnt find the template file (still open in the background).

    The EXCEL macro: Sub automateword() Dim strFolder As String

    strFolder = Range("B2").Value
    strDoc = Range("C2").Value
    Set WordApp = CreateObject("word.Application")

    WordApp.Documents.Open strFolder
    WordApp.Visible = True
    WordApp.Activate

    DoCmd.RunMacro "Update"
    Application.Run (Update)

    WordApp.Documents.Save
    'WordApp.Quit
    End Sub
    The WORD macro: Sub Update()

    Application.Templates.LoadBuildingBlocks

    Letter = ActiveDocument & " [Compatibility Mode]"
    Template = "Rebranded.doc" & " [Compatibility Mode]"

    'Activate Letter Template and copy header
    Windows(Template).Activate
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.WholeStory
    Selection.Copy
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    'Activate Letter to amend and Paste Header
    Windows(Letter).Activate
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.WholeStory
    Selection.Paste
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    'Activate Letter Template and copy footer
    Windows(Template).Activate
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.WholeStory
    Selection.Copy
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    'Active Letter to amend and Paste Footer
    Windows(Letter).Activate
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.WholeStory
    Selection.Paste
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    End Sub
    Any comments or help would be greatly appreciated - I have over 1000 documents that need amending.

    Thanks

  2. #2
    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: Using Excel to run a Word Macro

    It's not apparent to me why you even need to involve Excel, as there's no need to list the documents before you can process them. The whole lot could be done from Word. There is also no need to select anything or to use inefficient code like SeekView and .Copy/.Paste to update the various headers & footers. For example, the following Word macro, run from the document containing the new headers & footers will update all the documents in whatever folder you select.
    Please Login or Register  to view this content.
    You should also note that Word documents have three header and footer ranges for every Section. With the above code, updates are restricted to the first Section and Target documents that don't already have the designated header(s)/footer(s) in the first Section don't get updated. This avoids adding headers to documents that don't already have them. Conversely, existing header(s)/footer(s) won't be wiped out if the Source document lacks them.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    03-08-2016
    Location
    Newport
    MS-Off Ver
    2010
    Posts
    17

    Re: Using Excel to run a Word Macro

    That works perfectly! Thank you so much - much better and quicker than what I was trying to do!

    One last question - I want the macro to search for text in the documents and replace it;
    Eg - if Tom is in the letter change it to Mike.

    I have the code:
    Set wdDocTgt = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDocTgt
    'HERE
    'Search letter for text and replace
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "Tom" 'Text to search for
    .Replacement.Text = "Mike" 'Text to replace with
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    'HERE
    .Close SaveChanges:=True
    End With

    Just recorded it so sorry for the beginners attempt.

    Where would I enter this code into your macro to make it work?

    Thanks again.

    Tom

  4. #4
    Registered User
    Join Date
    03-08-2016
    Location
    Newport
    MS-Off Ver
    2010
    Posts
    17

    Re: Using Excel to run a Word Macro

    Thank you - are you able to help with my additional question please?

    Thanks.

  5. #5
    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: Using Excel to run a Word Macro

    After:
    With wdDocTgt
    insert:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-08-2016
    Location
    Newport
    MS-Off Ver
    2010
    Posts
    17

    Unhappy Re: Using Excel to run a Word Macro

    I have found a final issue with how the code runs - everything is perfect apart from:
    Some of the footers have a simple page x of y. The footer is not copied over to these pages (which is correct) however for odd number pages the page x of y is removed. Is there anyway to add this?

    Thanks

  7. #7
    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: Using Excel to run a Word Macro

    The way the macro is coded, both your source document (the one containing the macro) and the target document would require the first Section to contain the page x of y footer for odd pages for the odd pages footer to be updated. Footers won't be deleted unless your source document has an empty odd pages footer and your target document also has an odd pages footer.

  8. #8
    Registered User
    Join Date
    03-08-2016
    Location
    Newport
    MS-Off Ver
    2010
    Posts
    17

    Re: Using Excel to run a Word Macro

    Hi,

    I am trying to add the the above macro the ability to turn on 'Track Changes' and then before the document is saved change the view to 'Final' - this is to track the changes that the macro makes but the tracking is only visible IF the user changes the view.

    My code is:
    wdDocTgt.TrackRevisions = True
    wdDocTgt.TrackMoves = True
    wdDocTgt.TrackFormatting = True

    I have entered this after the With wdDocTgt after it west the value of wdDocTgt near the top of the code.

    The problem part I am having is changing the view of the code:
    I have added:
    With wdDocTgt.View
    .ShowRevisionsAndComments = False
    .RevisionsView = wdRevisionsViewFinal
    End With

    wdDocTgt.TrackRevisions = False
    wdDocTgt.TrackMoves = False
    wdDocTgt.TrackFormatting = False

    Just before the End If's before the .close line.
    The problem appears to be the .view object with the wdDocTgt variable.

    Any suggestions on how to correct this please?

    Thanks.

  9. #9
    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: Using Excel to run a Word Macro

    The reason you're having trouble with .View.ShowRevisionsAndComments, etc. is that they're properties of the ActiveWindow, not of the document. Furthermore, as properties of the ActiveWindow, you can't save those settings with the document. Accordingly, your macro cannot exert any control over whether a user sees the change tracking. Since the code also isn't doing any Moves or Formatting revisions, you can use just:
    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 to Word macro doesn't work on Word 2013 if sheet is hidden (works fine on 2010)
    By dreddster in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-29-2015, 04:37 AM
  2. error when running MS Word macro commands in Excel macro
    By bsapaka in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-01-2014, 01:30 PM
  3. [SOLVED] Excel macro- Find replace an itallic font word with a regular word?
    By thisisaboutwork in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2013, 01:48 PM
  4. Need help with Excel-to-Word find and replace macro based on word filename
    By EnterTheSerpent in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-16-2013, 12:48 PM
  5. Replies: 3
    Last Post: 07-06-2006, 02:45 PM
  6. WORD-DELIMITED string vba macro for excel/word
    By jackal2k6 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-23-2005, 12:35 PM
  7. Replies: 1
    Last Post: 07-07-2005, 11:05 AM

Tags for this Thread

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