Using Excel Extract Content from Word Doc

ChrisOK

Well-known Member
Joined
Mar 26, 2003
Messages
601
Had some code (a sub) that performed this long ago; can't find-- searched every advanced search hit on this site and haven't found anything yet - hoping a guru can help! Used to be able to attach sample of what the source content looks like and how I'm needing it laid into the excel file to try to make this easier; appears this option is gone? - Cross posting so I can attach a helpful file attachment example and needing help rather soon so the more eyes the better:
Using Excel VBA Extract Content from Word Doc
I know there's a ton of brilliant friends of many years here on MrExcel that can probably figure something out without a visual - but the link is available if needed!

*Do not need the code to open any files
(User will already have open 1 Excel file (ExampleExtractor.xlsm) and 1 Word file (LogRuleSourceXX.doc) open before running the SUB.
They will open each Word file one at a time -- and there's a large folder full - so I'm needing a sub like this again to expedite ongoing extractions).

THE 4 BASIC RULES THAT THE SUB SHOULD PERFORM:
1==If excel finds one of these rule words in All Caps [IF,AND,OR,PERFORM,THRU] in the Word doc, then copy the content that sits to the immediate RIGHT of that rule word & paste into Excel column C. (copy until a space occurs I think will work)

2==Then, pick up (copy/paste) the content that sits on the immediate RIGHT of the equals sign into Excel column D
(sometimes that content is in quotes - sometimes it's not - so not using quotes in the definition is probably more accurate (to just say in the code to pick up everything to the right of the equals sign on that same line) - and I'll parse off any extra garbage that I don't need)

3==Need the code to look at the Filename of the Word doc and extract the 12th+13th char position, paste that into Col E of Excel

4==Last, the code should locate the "Output" content which always follows the rule word: [TO], copy/paste into Col F of Excel

That's it!

(I threw those RULE WORDS into Col A as an idea that the code could look to that column when performing it's LOOKUP/INDEX -
but it's probably much better to just hard code them into a line of code and manage them there -- if so, disregard column A)

Forever in debt to you if you can figure out a decent sub to expedite this painful process...
Thanks, Chris
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
*Do not need the code to open any files
(User will already have open 1 Excel file (ExampleExtractor.xlsm) and 1 Word file (LogRuleSourceXX.doc) open before running the SUB.
They will open each Word file one at a time -- and there's a large folder full - so I'm needing a sub like this again to expedite ongoing extractions).
So why wouldn't you want the code to automate the file opening & closing as well?
1==If excel finds one of these rule words in All Caps [IF,AND,OR,PERFORM,THRU] in the Word doc, then copy the content that sits to the immediate RIGHT of that rule word & paste into Excel column C. (copy until a space occurs I think will work)
Are these words to be found anywhere in the document, or in a particular location (e.g. the first table, a bookmarked range or content control, a specified Section)? Can only one of these words appear in a given document, or can they all appear in the same document? Can the words occur more than once? If so, then what do you want to do about that? How is the macro to determine where the required content ends (so far you've only described (apparently) where it starts)?
2==Then, pick up (copy/paste) the content that sits on the immediate RIGHT of the equals sign into Excel column D
(sometimes that content is in quotes - sometimes it's not - so not using quotes in the definition is probably more accurate (to just say in the code to pick up everything to the right of the equals sign on that same line) - and I'll parse off any extra garbage that I don't need)
That's all very well, but a macro needs to know what to expect. Other that the quotes (single?/double?) that might appear after your keywords, what else always occurs and what else might occur be between them and the '=' (spaces, tabs, letters, numbers, etc.)?
4==Last, the code should locate the "Output" content which always follows the rule word: [TO], copy/paste into Col F of Excel
Clear as mud. Now you're introducing [TO] but you give no explanation of how this relates to your other rules.
(I threw those RULE WORDS into Col A as an idea that the code could look to that column when performing it's LOOKUP/INDEX -
but it's probably much better to just hard code them into a line of code and manage them there -- if so, disregard column A)
It's not clear whether your LOOKUP/INDEX reference is supposed to be for Excel's benefit or for Word's; Word has no such functions.
 
Last edited:
Upvote 0
Excited you're taking a look Macropod - I've seen you do some pretty awesome things in the past! ThxInAdvc!
(1)So why wouldn't you want the code to automate the file opening & closing as well?
.A: You totally could (and it wouldn't be a bad thing) - Was just trying to keep it less complex since time is of the essence - less code to write and less to have to change file names for each time (unless of course, there were some sort of prompt that prompted the User to navigate to the file each time and open it wherever it may be) - this would be fabulous!.. but not at the top of criticalities..
(2a) Are these words to be found anywhere in the document, or in a particular location (e.g. the first table, a bookmarked range or content control, a specified Section)?
.A: Yes (anywhere), the word docs we receive from the programmers have the content spread throughout -- it is not sitting in tables and sits in a staggered format such as this:
---------------------------------------
Code:
IF ACJ-TRANS-LR-CODE = '05'                               
                                                                   
           IF (WS-DOC-ID = 'F7A') AND                              
              (WS-RECORD-CD = '4')                                 
             MOVE SPACES TO ACN-KEY-SUF                            
                                                                   
           ELSE                                                    
             IF WS-DOC-NR1 = 'M'   AND                             
                WS-RECORD-CD = '4' AND                             
                WS-BGCD = '8'                                      
                                                                   
                IF (WS-DOC-ID = 'D7A' AND WS-TT = 'N') OR          
                   (WS-DOC-ID = 'D6R' AND WS-TT = 'T') OR          
                   ((WS-DOC-ID = 'D6A')  AND                       
                   (WS-TT = 'N' OR SPACES))                        
                                                                   
                  MOVE 'M' TO ACN-KEY-SUF                          
                ELSE                                              
                  MOVE WS-TT TO ACN-KEY-SUF                       
             ELSE                                                 
               MOVE WS-TT TO ACN-KEY-SUF
(2b) Can only one of these words appear in a given document, or can they all appear in the same document?
.A: All could potentially appear in a single Word doc
(2c) Can the words occur more than once?
.A: Yes, see example above, e.g., "IF" "AND" "OR" appear multiple times (and so could the others)...
(2d) If so, then what do you want to do about that?
.A: just list it on a new row of the spreadsheet
(for example WS-TT appears more than once (3xs below), so it gets listed each time it appears, along w/ whatever Code is listed (but here's where it gets tricky).......,
Code:
                IF (WS-DOC-ID = 'D7A' AND WS-TT = 'N') OR          
                   (WS-DOC-ID = 'D6R' AND WS-TT = 'T') OR          
                   ((WS-DOC-ID = 'D6A')  AND                       
                   (WS-TT = 'N' OR SPACES))                        
                                                                   
                  MOVE 'M' TO ACN-KEY-SUF  <<< everything abv relates to this Output name
The 3rd time WS-TT is listed, there's a code in single quotes 'N', (and) there's an "OR" present
which means we'll need to list WS-TT a 4th time in order to record the other code "SPACES" (or)
if there's an easy way to list it on a single line like this: (it would be just as good) - whichever is easier!

Column C..................Col D..........Col E............Col F...........
Field Name................Condition....Rule Name....Output.........
ACJ-TRANS-LR-CODE..05..............AA..............ACN-KEY-SUF
WS-DOC-ID...............F7A............AA..............ACN-KEY-SUF
WS-RECORD-CD........4................AA..............ACN-KEY-SUF
WS-DOC-NR1............M...............AA..............ACN-KEY-SUF
WS-RECORD-CD........4................AA..............ACN-KEY-SUF
WS-BGCD.................8................AA..............ACN-KEY-SUF
WS-DOC-ID..............D7A............AA..............ACN-KEY-SUF
WS-TT.....................N................AA..............ACN-KEY-SUF
WS-DOC-ID..............D6R............AA..............ACN-KEY-SUF
WS-TT.....................T.................AA..............ACN-KEY-SUF
WS-DOC-ID..............D6A.............AA.............ACN-KEY-SUF
WS-TT.....................N.................AA.............ACN-KEY-SUF
WS-TT.....................SPACES........AA.............ACN-KEY-SUF
or can do it like this:
WS-TT.....................N,SPACES.....AA.............ACN-KEY-SUF
----------------------------------------------------------------------------------
the 'Rule Name' is simply the Word doc file name repeated all the way down,
and the 'Output' is re-used as it is relational (meaning all the line items listed in that little chunk all tie to/relate to the 'Output' name listed at the end of that chunk signified by preceding the word "TO":
Code:
IF (WS-DOC-ID = 'D7A' AND WS-TT = 'N') OR          
                   (WS-DOC-ID = 'D6R' AND WS-TT = 'T') OR          
                   ((WS-DOC-ID = 'D6A')  AND                       
                   (WS-TT = 'N' OR SPACES))                        
                                                                   
                  MOVE 'M' TO ACN-KEY-SUF  <<< everything abv relates to this Output name
(2e) How is the macro to determine where the required content ends (so far you've only described (apparently) where it starts)?
.A: I saw that it would be labor intensive to try to build in all the starts and stops into the code (considering the staggered format this content arrives to us in AND the inconsistency of CODES being/not being all housed between single quote marks)
- so with time being short, I mentioned just pick up the rest of that line
with the intention to parse off the extra garbage later - either manually or using some RIGHT,MID,LEFT functions.. even that, given the format it arrives in - will be difficult - so this may have to be done manually. (hate that word) =-)
(3) That's all very well, but a macro needs to know what to expect. Other that the quotes (single?/double?) that might appear after your keywords, what else always occurs and what else might occur be between them and the '=' (spaces, tabs, letters, numbers, etc.)?
.A: Well, that's where it gets really crappy because those rules are exactly what I looked for -- taking a handful of the huge stack of Word docs to try to identify patterns and inconsistencies..

It's safe to say these are the ALWAYS DO rules:
=1= After the word "TO", always pick up whatever content is sitting to the immediate RIGHT of "TO" (up until a space occurs/stop at space or end of line whatever comes first) to capture the Output Name
Code:
                  MOVE 'M' TO ACN-KEY-SUF
=2= Whenever an equals sign is encountered, always pick up content LEFT of it (up until a space or parens occurs/stop) to capture the Field Name
Code:
                   (WS-DOC-ID = 'D6R' AND WS-TT = 'T') OR
=3= Whenever the word "OR" appears followed by single quotes (like the below example), record both codes either in a single cell or separately on diff lines as demonstrated below:
Code:
IF HOLD-REIMB = ‘A’ OR ‘Y’<<<<<<<<<<<<< A & Y can be pasted into Col C either together or individually
IF PUBLIC-SALES-CODE AND PUBLIC-TIV-12
MOVE ‘12’ TO ACH-KEY-IND-VAL
Column C..................Col D..........Col E............Col F...........
Field Name................Condition....Rule Name....Output.........
HOLD-REIMB.............A.................AA..............ACN-KEY-SUF
HOLD-REIMB.............Y.................AA.............ACN-KEY-SUF
..or can do it like this (whichever is easier):
HOLD-REIMB.............A,Y..............AA.............ACN-KEY-SUF
----------------------------------------------------------------------------------

=4= Whenever "PUBLIC-SALES-CODE" (or) "PUBLIC-TIV-12" (or) NOT-PUBLIC-SALES-CODE are encountered like the below,
Code:
IF HOLD-REIMB = ‘A’ OR ‘Y’<<<<<<<<<<<<< A & Y can be pasted into Col C either together or individually
IF PUBLIC-SALES-CODE AND PUBLIC-TIV-12
MOVE ‘12’ TO ACH-KEY-IND-VAL
list them as individual line items as follows:
---------------------------------
Column C..................Col D..........Col E............Col F...........
Field Name................Condition....Rule Name....Output.........
PUBLIC-SALES-CODE..........YES.......AA.............ACN-KEY-SUF
PUBLIC-TIV-12...................YES.......AA.............ACN-KEY-SUF
NOT PUBLIC-SALES-CODE....NO........AA.............ACN-KEY-SUF<<< "NOT" INDICATES "NO" Condition
(4) Clear as mud. Now you're introducing [TO] but you give no explanation of how this relates to your other rules.
.A: Sorry, will try to be more clear: IF the word "TO" is encountered, copy the content that sits immediately to the RIGHT of "TO" and paste it into Col F Output field.
(4a) How much content gets copied?
.A: See below: (copy until the first space or end of line occurs after the content) "ACH-KEY-IND-VAL" then stop.
Code:
IF HOLD-REIMB = ‘A’ OR ‘Y’<<<<<<<<<<<<< A & Y can be pasted into Col C either together or individually
IF PUBLIC-SALES-CODE AND PUBLIC-TIV-12
MOVE ‘12’ TO ACH-KEY-IND-VAL
(5) It's not clear whether your LOOKUP/INDEX reference is supposed to be for Excel's benefit or for Word's; Word has no such functions.
.A: disregard that terminology, the goal is to use EXCEL to extract content from WORD docs; achieved in the fastest, most efficient way possible... if it's a little messy with picking up a little extra garb at the end of a line, so-be-it- as long as I can collect all the content needed from the Word doc and pass it on to programmers in a table such as is shown below. Hope that makes sense and THANKS AGAIN!

Column C.........................Col D..........Col E..............Col F...........
Field Name...................Condition......Rule Name........Output.........
ACJ-TRANS-LR-CODE...........05..............AA..............ACN-KEY-SUF
WS-DOC-ID........................F7A............AA..............ACN-KEY-SUF
WS-RECORD-CD.................4................AA..............ACN-KEY-SUF
WS-DOC-NR1.....................M...............AA..............ACN-KEY-SUF
WS-RECORD-CD.................4................AA..............ACN-KEY-SUF
WS-BGCD..........................8................AA..............ACN-KEY-SUF
WS-DOC-ID.......................D7A............AA..............ACN-KEY-SUF
WS-TT..............................N................AA..............ACN-KEY-SUF
WS-DOC-ID.......................D6R............AA..............ACN-KEY-SUF
WS-TT..............................T.................AA..............ACN-KEY-SUF
WS-DOC-ID.......................D6A.............AA.............ACN-KEY-SUF
WS-TT...............................N.................AA.............ACN-KEY-SUF
PUBLIC-SALES-CODE..........YES..............AA.............ACN-KEY-SUF
PUBLIC-TIV-12...................YES..............AA.............ACN-KEY-SUF
NOT PUBLIC-SALES-CODE....NO...............AA.............ACN-KEY-SUF<<< "NOT" INDICATES "NO" Condition
----------------------------------------------------------------------------------
eek! sorry all the ANSWERS to your questions are forcibly in italics because it's included within the quote area.. sorry!
(also note, there's an orange cross-post link to an actual example xlsm file if that helps - in the orig post above)
 
Last edited by a moderator:
Upvote 0
There seems to be some significant scope creep going on...

Try the following macro in a Word document. As coded, it just displays its output in a message box (I don't want to invest too much effort getting the wrong output). Excel implementation can be done when the Word basics are correct.

Unless the code lines are separated by line breaks (i.e. ↵) and not paragraph breaks (i.e. ¶), what isn't apparent from anything you've posted is how a macro would recognise which content belongs in column F.
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim wdApp As Word.Application, wdDoc As Word.Document, StrCode As String
Dim i As Long, StrIn As String, StrTmp As String, StrOut As String
Set wdDoc = ActiveDocument
With wdDoc
  StrCode = Mid(.Name, 12, 2)
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = " [=TOAND]{1,3} "
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      StrIn = Split(.Paragraphs(1).Range.Text, vbCr)(0)
      If StrIn = UCase(StrIn) Then
        StrIn = Replace(Replace(Replace(StrIn, vbTab, " "), ")", " "), "(", " ")
        StrIn = Trim(StrIn)
        Do While InStr(StrIn, "  ")
          StrIn = Replace(StrIn, "  ", " ")
        Loop
        For i = 1 To UBound(Split(StrIn, " = "))
          StrTmp = Split(Split(StrIn, " = ")(i - 1), " ")(UBound(Split(Split(StrIn, " = ")(i - 1), " ")))
          StrOut = StrOut & StrTmp & vbTab & Split(Split(StrIn, " = ")(i), " ")(0) & vbTab & StrCode & vbCr
        Next
        If UBound(Split(StrIn, " OR '")) = 1 Then
          StrOut = StrOut & StrTmp & vbTab & "'" & Split(StrIn, " OR '")(1) & vbTab & StrCode & vbCr
        End If
        For i = 1 To UBound(Split(StrIn, " TO "))
          StrTmp = Split(Split(StrIn, " TO ")(i - 1), " ")(UBound(Split(Split(StrIn, " TO ")(i - 1), " ")))
          StrOut = StrOut & StrTmp & vbTab & Split(Split(StrIn, " TO ")(i), " ")(0) & vbTab & StrCode & vbCr
        Next
        If InStr(StrIn, "NOT-PUBLIC-SALES-CODE") > 0 Then
          StrOut = StrOut & "PUBLIC-SALES-CODE" & vbTab & "NO" & vbTab & StrCode & vbCr
        ElseIf InStr(StrIn, "PUBLIC-SALES-CODE") > 0 Then
          StrOut = StrOut & "PUBLIC-SALES-CODE" & vbTab & "YES" & vbTab & StrCode & vbCr
        End If
        If InStr(StrIn, "NOT-PUBLIC-TIV-12") > 0 Then
          StrOut = StrOut & "PUBLIC-TIV-12" & vbTab & "NO" & vbTab & StrCode & vbCr
        ElseIf InStr(StrIn, "PUBLIC-TIV-12") > 0 Then
          StrOut = StrOut & "PUBLIC-TIV-12" & vbTab & "YES" & vbTab & StrCode & vbCr
        End If
      End If
      .End = .Paragraphs(1).Range.End
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
MsgBox StrOut
Application.ScreenUpdating = True
End Sub
PS: I've 'repaired' your reply...
 
Upvote 0
Macropod -- you're friggin' amazing -- it's actually very close!! I'm so excited!
It took me a while - but I cross-referenced what was generated against what should have generated to highlight areas where things went wrong..
Did it that way to TRY to make it easier for you to pinpoint things --

That said, trying to figure out a way to upload the results/or get them to you..
I've taken a screen-shot (gif)... and see that the "" code is turned on - but not sure how to get the gif into this post?
Any ideas?

I also could provide it via Excel file - but doesn't look like I can upload any files..
I guess as a last resort, I could upload it to another cross-post so it's viewable there? (feel kind of odd - doing that - but I guess it would be helpful as long as I post your code too - so folks see where we're heading with this..)
This one allows file attachments and images to be uploaded: [url=http://www.ozgrid.com/forum/showthread.php?t=202211]Using Excel VBA Extract Content from Word Doc[/url]
I'll wait and see what you think?

Let me know best way to get 1 to you and I'll get right on it!

PS - when I first opened the post I was like: WHOAAA! How did he fix that!? (all the quoting looks excellent now) - Thx!
 
Upvote 0
No-one can access you ozgrid attachment without opening an account there, which I'm loathe to do just for that. In any event, having the workbook wouldn't be much help without also having the document from which it was produced.

Rather, if you can post some sample data here that isn't being processed correctly, showing how it should be output, more progress might be made. In the meantime, try the following Excel implementation, which includes a folder browser and will process all files in the selected folder:
Code:
Sub GetWordDocumentData()
'Note: this code requires a reference to the Word object model to be set via Tools|References in the VBA editor.
Application.ScreenUpdating = False
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim strFolder As String, strFile As String
Dim StrCode As String, StrIn As String, StrTmp As String, StrOut As String
Dim WkSht As Worksheet, i As Long, j As Long, r As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
r = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    StrCode = Mid(.Name, 12, 2)
    With .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = " [=TOAND]{1,3} "
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        StrIn = Split(.Paragraphs(1).Range.Text, vbCr)(0)
        If StrIn = UCase(StrIn) Then
          StrIn = Replace(Replace(Replace(StrIn, vbTab, " "), ")", " "), "(", " ")
          StrIn = Trim(StrIn)
          Do While InStr(StrIn, "  ")
            StrIn = Replace(StrIn, "  ", " ")
          Loop
          For i = 1 To UBound(Split(StrIn, " = "))
            StrTmp = Split(Split(StrIn, " = ")(i - 1), " ")(UBound(Split(Split(StrIn, " = ")(i - 1), " ")))
            StrOut = StrOut & vbCr & StrTmp & vbTab & Split(Split(StrIn, " = ")(i), " ")(0) & vbTab & StrCode
          Next
          If UBound(Split(StrIn, " OR '")) = 1 Then
            StrOut = StrOut & vbCr & StrTmp & vbTab & "'" & Split(StrIn, " OR '")(1) & vbTab & StrCode
          End If
          For i = 1 To UBound(Split(StrIn, " TO "))
            StrTmp = Split(Split(StrIn, " TO ")(i - 1), " ")(UBound(Split(Split(StrIn, " TO ")(i - 1), " ")))
            StrOut = StrOut & vbCr & StrTmp & vbTab & Split(Split(StrIn, " TO ")(i), " ")(0) & vbTab & StrCode
          Next
          If InStr(StrIn, "NOT-PUBLIC-SALES-CODE") > 0 Then
            StrOut = StrOut & "PUBLIC-SALES-CODE" & vbTab & "NO" & vbTab & StrCode
          ElseIf InStr(StrIn, "PUBLIC-SALES-CODE") > 0 Then
            StrOut = StrOut & vbCr & "PUBLIC-SALES-CODE" & vbTab & "YES" & vbTab & StrCode
          End If
          If InStr(StrIn, "NOT-PUBLIC-TIV-12") > 0 Then
            StrOut = StrOut & vbCr & "PUBLIC-TIV-12" & vbTab & "NO" & vbTab & StrCode
          ElseIf InStr(StrIn, "PUBLIC-TIV-12") > 0 Then
            StrOut = StrOut & vbCr & "PUBLIC-TIV-12" & vbTab & "YES" & vbTab & StrCode
          End If
        End If
        .End = .Paragraphs(1).Range.End
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    .Close SaveChanges:=False
  End With
  For i = 1 To UBound(Split(StrOut, vbCr))
    r = r + 1
    StrTmp = Split(StrOut, vbCr)(i)
    For j = 0 To UBound(Split(StrTmp, vbTab))
      WkSht.Cells(r, j + 3).Value = Split(StrTmp, vbTab)(j)
    Next
  Next
  strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
 
Upvote 0
Ok wonderful!
I'll work this afternoon to get sample results uploaded for you to proceed. (totally understand the ozgrid situation - no prob, I'll get you want you need another way)

Meanwhile, need to make sure I understand how to operate this new code / not having much luck - here's what I did/and the result:
1-opened an excel file w/ the column headers in place("Sheet2"), named the file "Tester.xlsm"
2-Alt+F11, pasted the new code into that sheet (sheet2)
3-created a folder w/ 2 Word content test files
4-placed curser on 1st cell of active xlsm sheet where the content should paste to - (making focus on that active sheet)-(only file open)..
5-ran code from xlsm side this time, which prompted me to navigate to that folder - but once I clicked it and said "OK" - it seemed nothing happens..

So, I removed the code from that Sheet2 and created a new module within the "Personal.xlsb" area and tried running it from there.. got the same result.. (navigated to folder holding word docs, clicked OK, then nothing happened).

Let me know what I'm doing wrong =-/
 
Upvote 0
What format are to Word documents - doc, docx, docm? The code assumes docx.

There is no need to select anything in the Excel workbook. The output automatically starts below the last used row on the active sheet - you may as well add the code to the 'ThisWorkbook' module.
 
Upvote 0
OK, got the folder updated with a docx file (it had 1 doc and 1 docx the first time around)..
Put code into the "ThisWorkbook" but got some errors - then noticed the comment in the top of the code - indicating References needed verified --
Found that "Word" was not checkmarked - got it checked, ran again with same error --
Went back in and checkmarked about 4 line items within Refs and then it worked!

I've manually tried to get the content into columns for you to see both the RESULT and WHAT IT SHOULD LOOK LIKE:
Hope this helps! Looks really close -- getting really excited!
Also, heard about what was going on in Australia, hope you stay safe and have a wonderful, pleasant Christmas weekend, your help getting this working is greatly appreciated! What a *gift* you are! I hope to help you out someday - someway --

Here's how the results of the doc should look:
Should be 4 columns
Field Name..........Condition..RuleName...Output.........
ACJ-TRANS-LR-CODE..........05......AA.......ACN-KEY-SUF
WS-DOC-ID......................F7A.....AA.......ACN-KEY-SUF
WS-RECORD-CD.................4.......AA.......ACN-KEY-SUF
WS-DOC-NR1....................M.......AA.......ACN-KEY-SUF
WS-RECORD-CD.................4.......AA.......ACN-KEY-SUF
WS-BGCD..........................8.......AA.......ACN-KEY-SUF
WS-DOC-ID.....................D7A.....AA.......ACN-KEY-SUF
WS-TT..............................N.......AA.......ACN-KEY-SUF
WS-DOC-ID.....................D6R.....AA.......ACN-KEY-SUF
WS-TT..............................T.......AA.......ACN-KEY-SUF
WS-DOC-ID....................D6A.....AA.......ACN-KEY-SUF
WS-TT.............................N.......AA.......ACN-KEY-SUF
WS-TT.......................SPACES....AA.......ACN-KEY-SUF
WS-ICC...........................8.......AA.......ACH-KEY-VAL-IND
WS-ERRC........................N.......AA.......ACH-KEY-VAL-IND
WS-ERRC.........................P.......AA.......ACH-KEY-VAL-IND
DODAAC-FOUND...............N.......AA.......ACH-KEY-VAL-IND
VALID-FUND-CODE............N.......AA.......ACH-KEY-VAL-IND
HOLD-REIMBM..................A.......AA.......ACH-KEY-VAL
HOLD-REIMBM...................Y.......AA.......ACH-KEY-VAL
PUBLIC-SALES-CODE.........YES.....AA.......ACH-KEY-VAL
PUBLIC-TIV-12..................YES.....AA.......ACH-KEY-VAL
DODAAC-FOUND.................N.......AA.......ACH-KEY-VAL-IND
VALID-FUND-CODE..............N.......AA.......ACH-KEY-VAL-IND
HOLD-REIMB......................A.......AA.......ACH-KEY-VAL-IND
HOLD-REIMB.......................Y.......AA.......ACH-KEY-IND-VAL
PUBLIC-SALES-CODE..........YES.....AA.......ACH-KEY-IND-VAL
PUBLIC-SALES-CODE..........YES.....AA.......ACH-KEY-IND-VAL
WS-ICC..............................9.......AA.......ACH-KEY-IND-VAL-IND
DODAAC-FOUND..................N.......AA.......ACH-KEY-IND-VAL-IND
VALID-FUND-CODE...............N.......AA.......ACH-KEY-IND-VAL-IND
HOLD-REIMB........................A.......AA.......ACH-KEY-IND-VAL-IND
HOLD-REIMB........................Y.......AA.......ACH-KEY-IND-VAL-IND
PERFORM GSD-SURCHARGE..YES.....AA.......ACH-KEY-IND-VAL-IND
GSD-SURCHARGE-EXIT.........YES.....AA.......ACH-KEY-IND-VAL-IND
PUBLIC-SALES-CODE............YES.....AA.......ACH-KEY-IND-VAL

====================================================
This is what returned with the 2nd batch of code run from Excel
(only 3 Columns)
= Field Name mixed w/ Condition Codes in 1st Column
= Condition Code mixed w/ some Field Names in 2nd Col
= 3rd Column looks great
= 4th Column missing
=====================================================
ACJ-TRANS-LR-CODE....05'.......................AA
WS-DOC-ID................F7A'......................AA
WS-RECORD-CD...........4'.........................AA
SPACES.....................ACN-KEY-SUF.........AA
WS-DOC-NR1..............M'........................AA
WS-RECORD-CD..........4'.........................AA
WS-BGCD...................8'........................AA
WS-DOC-ID...............D7A'......................AA
WS-TT......................N'...........................AA
WS-DOC-ID...............D6R'.......................AA
WS-TT......................T'...........................AA
WS-DOC-ID...............D6A'.......................AA
WS-TT.......................N'..........................AA
M'.............................ACN-KEY-SUF..........AA
WS-TT.......................ACN-KEY-SUF..........AA
WS-TT.......................ACN-KEY-SUF..........AA
WS-ICC....................‘8’...........................AA
WS-ERRC..................‘N’...........................AA
DODAAC-FOUND........‘N’...........................AA
VALID-FUND-CODE.....‘N’...........................AA
‘01’...........................ACH-KEY-VAL-IND.....AA
HOLD-REIMB..............‘A’...........................AA
PUBLIC-SALES-CODE...YES.........................AA
PUBLIC-TIV-12...........YES..........................AA
‘12’...........................ACH-KEY-IND-VAL.....AA
‘02’...........................ACH-KEY-IND-VAL......AA
‘03’...........................ACH-KEY-IND-VAL......AA
DODAAC-FOUND........‘N’.............................AA
VALID-FUND-CODE.....‘N’............................AA
‘04’...........................ACH-KEY-VAL-IND......AA
HOLD-REIMB..............‘A’............................AA
‘15’...........................ACH-KEY-IND-VAL......AA
‘05’...........................ACH-KEY-IND-VAL......AA
‘16’...........................ACH-KEY-IND-VAL......AA
‘06’...........................ACH-KEY-IND-VAL......AA
WS-ICC.....................‘9’.............................AA
DODAAC-FOUND........‘N’.............................AA
VALID-FUND-CODE.....‘N’.............................AA
‘07’..........................ACH-KEY-VAL-IND........AA
HOLD-REIMB..............A'..............................AA
‘18’..........................ACH-KEY-IND-VAL........AA
‘08’..........................ACH-KEY-IND-VAL........AA
‘09’..........................ACH-KEY-IND-VAL........AA
‘99’..........................ACH-KEY-IND-VAL........AA
 
Upvote 0
Tried to edit last post to add this for you - but it time frame expired - so having to post down here..
=====================================
Here's what the content in the Word document looks like.. (there's a bunch a little tables of data here and there - but don't want anything that's sitting in those tables and appears you've already got the code ignoring all that... so we're good there... just wanted you to see the raw data:

Word Content:

Version 53: Multiple changes to billing GLT pairs. Debit ‘004’ and Credit ‘007’ changed to Debit ‘003’ and Credit ‘006’. This in turn will generate FA2 - Billing for issue from stock (Credit).
Version 52: Change all occurrences of General Ledger Account (GLA) 4210.10.01 to new GLA 4132.20, GLA 4210.10.02 to new GLA 4132.20 if the contra account is 4221.21, 4221.22, 4251.21, or 4251.22, and GLA 4210.10.02 to new account 4135.20 if the contra account is 4252.21 or 4252.22. New account 4139 will be established as the Year End roll up account for the new 4132 and 4135 accounts.

1. Document Identifiers and Suffixes:

Code:
2500-DET-SUF.
         IF ACJ-TRANS-LR-CODE = '05'                               
                                                                   
           IF (WS-DOC-ID = 'F7A') AND                              
              (WS-RECORD-CD = '4')                                 
             MOVE SPACES TO ACN-KEY-SUF                            
                                                                   
           ELSE                                                    
             IF WS-DOC-NR1 = 'M'   AND                             
                WS-RECORD-CD = '4' AND                             
                WS-BGCD = '8'                                      
                                                                   
                IF (WS-DOC-ID = 'D7A' AND WS-TT = 'N') OR          
                   (WS-DOC-ID = 'D6R' AND WS-TT = 'T') OR          
                   ((WS-DOC-ID = 'D6A')  AND                       
                   (WS-TT = 'N' OR SPACES))                        
                                                                   
                  MOVE 'M' TO ACN-KEY-SUF                          
                ELSE                                              
                  MOVE WS-TT TO ACN-KEY-SUF                       
             ELSE                                                 
               MOVE WS-TT TO ACN-KEY-SUF


5. Add TRANS-IND-CODE ‘AA’ to ZJOT0312:

Code:
	IF WS-ICC = ‘8’
		IF WS-ERRC = ‘N’ OR ‘P’
			IF DODAAC-FOUND = ‘N’ OR VALID-FUND-CODE = ‘N’
				MOVE ‘01’ TO ACH-KEY-VAL-IND
			ELSE
				IF HOLD-REIMB = ‘A’ OR ‘Y’
					IF PUBLIC-SALES-CODE AND PUBLIC-TIV-12
						MOVE ‘12’ TO ACH-KEY-IND-VAL
					ELSE  
						MOVE ‘02’ TO ACH-KEY-IND-VAL
				ELSE
					MOVE ‘03’ TO ACH-KEY-IND-VAL
		ELSE
			IF DODAAC-FOUND = ‘N’ OR VALID-FUND-CODE = ‘N’
				MOVE ‘04’ TO ACH-KEY-VAL-IND
			ELSE
				IF HOLD-REIMB = ‘A’ OR ‘Y’
					IF PUBLIC-SALES-CODE
						MOVE ‘15’ TO ACH-KEY-IND-VAL
					ELSE 
						MOVE ‘05’ TO ACH-KEY-IND-VAL
				ELSE
					IF PUBLIC-SALES-CODE
						MOVE ‘16’ TO ACH-KEY-IND-VAL
					ELSE  
						MOVE ‘06’ TO ACH-KEY-IND-VAL
	ELSE
		IF WS-ICC = ‘9’
			IF DODAAC-FOUND = ‘N’ OR VALID-FUND-CODE = ‘N’
				MOVE ‘07’ TO ACH-KEY-VAL-IND
			ELSE
				IF HOLD-REIMB = 'A' OR ‘Y’
					PERFORM GSD-SURCHARGE THRU GSD-SURCHARGE-EXIT
					IF PUBLIC-SALES-CODE
						MOVE ‘18’ TO ACH-KEY-IND-VAL
					ELSE  
						MOVE ‘08’ TO ACH-KEY-IND-VAL
				ELSE
					MOVE ‘09’ TO ACH-KEY-IND-VAL
		ELSE
			MOVE ‘99’ TO ACH-KEY-IND-VAL
============================================
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,803
Messages
6,174,693
Members
452,577
Latest member
Filipzgela

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top