Link to home
Start Free TrialLog in
Avatar of Andreamary
Andreamary

asked on

Word macro that references an Excel file to update work order numbers

I would like a macro for Word 2016 that searches through the file for each work order number, then checks for the work order number in Column A of an Excel list, and if found, then replaces the work order number in the Word file based on the value listed in Column C of the Excel list. (Column C is a formula that concatenates Column A [the work order number] and Column B [initials]).

Additional notes:
  • Excel spreadsheet is in a table format
  • Column A and Column C are dynamic named ranges, as the number of work orders listed in the spreadsheet will change/fluctuate
  • I would like the original formatting of the work order number in Word to be retained when the work order number is updated in the file.
  • If the work order is not found in Column A of the Excel file, then no action is required in the Word file.

The format of the work order number is:
 ####-####### (4 numbers, followed by a dash, then 7 numbers)

I have attached the following files:
1. Cycle_Changes_Depot.docx
2. WO_Initials_List.xlsm
3. Cycle_Changes_Depot_After_Update.docx (this is an example file of the results I am looking for after the macro is run)

I hope I've included all the relevant information/files. Please let me know if you have any questions or need anything further.

Thanks!
Andrea
Cycle_Changes_Depot.docx
Cycle_Changes_Depot_After_Update.docx
WO_Initials_List.xlsm
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

It isn't clear which file is meant in the first sentence, so this Excel-hosted macro will simply step throught the sheet and modify the input document.
Sub AugmentWO()
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim strWO As String
    Dim strConcatenate As String
    Dim xlWks As Excel.Worksheet
    Dim r As Integer
    Dim strWordFile As String
    
    Set xlWks = Sheets("WO_Initial")
    r = 2
    
    'create a word application instance if necessary
    On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
    End If
    
    'open the document
    strWordFile = "J:\Allwork\ee\29136233\Cycle_Changes_Depot.docx"
    Set wdDoc = wdApp.Documents.Open(strWordFile)
    
    'do find and replace for each valid row of the sheet
    Do
        strWO = xlWks.Cells(r, 1)
        strConcatenate = xlWks.Cells(r, 3)
        r = r + 1
        With wdDoc.Range.Find
            .Text = strWO
            .Replacement.Text = strConcatenate
            .Execute Replace:=wdReplaceAll
        End With
    Loop Until xlWks.Cells(r, 1) = ""
End Sub

Open in new window

Avatar of Andreamary
Andreamary

ASKER

Hi Graham,

Thanks for your quick response. I've added this macro into my Excel file. When I try to run it, I get a compile error on the following line:

Dim wdApp As Word.Application

Open in new window


I have attached the screen capture of the error in hopes that it can be resolved...

Thanks,
Andrea
Compile_Error.PNG
Hi Graham,

Update: I figured out the issue above (Tools > References > checking MS Word) and then ran the macro, which worked perfectly, thanks! So please ignore my previous response.

One thing I realize now, and should have included in my question, is that we will be running it on the same Word files more than once as we add new work orders, so there will be a mix of work order numbers: some that have already been updated to match Column C of the Excel file, and some that are not updated.

Currently, when I rerun the macro, it adds additional instances of "(*)", as shown below:

2017-0004801 (TPG) (TPG)

Is it possible to have the macro revised so that:
If the work order number is found in Column A, and the work order number in the Word file already matches the value in Column C of the Excel file, then no action required.

Graham, given that this is an additional request, please don't hesitate to let me know if I should be posing this as a new question instead of adding it to this one.

Thanks again for your assistance,
Andrea
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Hi Graham,

I'm sorry for my tardiness in responding - I was assigned to a special project this week and so this has been my first chance to review the updated macro. It works perfectly - I'm very pleased. Thanks so much!

Cheers,
Andrea