Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word to read file and to decide save location

Status
Not open for further replies.

ConChoona

IS-IT--Management
May 19, 2005
22
GB
Hi all,

Not really done any office programming before so please bear with me!

Ok, basically what I want is for Word to read a document, locate data and determine where the file should be saved depending on the that data - for example, if I had a letter with a certain address in it, I would like it to automatically save this document in the correct location.

I'm not even sure if this is possible, but any help would be great.

Thanks a lot,

CC
 
This is going to LARGELY depend on the criteria used to categorize your documents. Can you provide example data, criteria, and save location? We need to see what logic you want to use to do all of this.
 


Hi.
I want is for Word to read a document
MS Word, the application, can OPEN a Word document. Or is the document something other than a Word document?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hey thanks for the replies.

Ok, the document will be a letter in Word doc format (2003).

There is a section within the Word doc that contains the address of the sender. The majority of these docs can be saved in a certain location e.g.

\\FS01\DOCS\SENDER1

I have one particular sending address that requires the document to be stored in a separate location e.g.

\\FS01\DOCS\SENDER2

I would like something that will read the sender zip\postcode, and decide from that where the document should be saved.

So, SENDER1 would have postcode of: 123456

and SENDER2 would have postcode of: abcdef

The location of the code in the document will not change, regardless of the senders address.

Thanks again - let me know if you need more information.

CC
 
You have told us you have a letter containing the sender's address - you have even gone so far as to tell us it will always be in the same place. You have not told us _where_ in the document it will be, or given us any clues about how to find it, nor what country you are in, which might give some indication of the format the address might be in.

That said, we can, perhaps, safely assume that the address is the first thing in the letter. We can, also, perhaps, assume that the postcode is the last line of the address. If we could further assume that it is in a section by itself (you only say it is in a section, you don't say that section doesn't contain other text) then we could try and extract the postcode like this:

Code:
If ActiveDocument.Sections(1).Range.Words.Last.Previous.Previous.Words.Last = "abcdef" Then
    SaveLocation = "SENDER2"
Else
    SaveLocation = "SENDER1"
End If

This steps backwards into the section, steps over the end of section mark and (presumed) paragraph mark and takes the word there. It is using a lot of assumptions so, at best, it is giving you a starting point. It also assumes that you are only looking for a single particular postcode - if that is, indeed, the case, a search for it might be a better way to proceed.

If you are a completely new to all of this, then you need to realise that VBA sees the body of a document, loosely, as a long stream of characters (there's more to it, and some of the characters have special meanings, but that's for another time). The stream of characters can be broken down in various ways, but VBA does not see document structure the way you do. To identify, in this case, the address, VBA needs to know how to pull it out of the stream - it may be in some particular style, perhaps right aligned, it may always be the fourth paragraph, it may be big, or bold, or it may always be followed by an empty paragraph, it may always be followed by a particular salutation at the start of the letter proper, or it may be any combination of these or other attributes. Whatever it is, there has to be something unique about it that VBA can identify - you can perhaps tell us what that is, we don't know until you do.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Hey Tony,

Thanks for your reply.

It's hard as I don't currently have a sample of the doc available atm. However, the postcode will always start L-

It will be on the right hand side, in a block of justified text, 6 or 7 lines down.

I will endeavour to get a sample asap, but does the above help?
 
You could try something like this, but without knowing for sure how the document is organised, it's difficult:

Code:
    Dim p As Paragraph
    Dim r As Range
    
    Set p = ActiveDocument.Paragraphs(1)
    
    Do While p.Alignment = wdAlignParagraphRight
        Set p = p.Next
    Loop
    
    Set r = p.Previous.Range
    r.End = r.End - 1

    Postcode = r.Text



Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Hi. The key point is: "locate data "

We need (or rather YOU need) to state where that is, i.e locate the data. Like I stated in the Office forum with your original post, this should not be difficult, what drives it is the logic.

And what will drive the logic is getting the data.

If the address is in a bookmark, this is real simple.
If the address is in a field, this is real simple.
If the address is in a defined cell in a table, this is real simple.

So..........

"There is a section within the Word doc that contains the address of the sender. "

How can we find that "section"? Is it a real Word section, or is it a "section" that YOU think of as a section? To Word, a Section is a defined range of the document, between two section breaks. NO other definition. A Section is between section breaks.

I suspect your document is not structured this way, and that is OK, but we need to know. WHERE is that address? If it is text (like other text), then we need to be to find it. Perhaps search for it. However, if the address text is ALWAYS - and I mean always - in the same location, then this is can be helpful.

1. you can get the address information by looking at that location.

2. you can bookmark that location, and then get the information from the bookmark (this is EASY).

Once you get the address, saving to different location is straightforward. Something like:
Code:
Dim strAddress As String
strAddress = [i]address information[/i]

[COLOR=red]'  something like
'  strAddress = ActiveDocument.Bookmarks("Address"). _
         Range.Text[/color red]
If InStr(strAddress, "abcdef") > 0 Then
    ActiveDocument.SaveAs _
      FileName:="\\FS01\DOCS\SENDER2" & _
            [i]YourFileName[/i]
Else
    ActiveDocument.SaveAs _
      FileName:="\\FS01\DOCS\SENDER1" & _
            [i]YourFileName[/i]
End if
In any case, once you GET the address information, it is easy to check if it contains a specific string (say abcdef), and if it does, save the file at X; if it does NOT, save the file at Y.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top