PDA

View Full Version : Solved: help with word macro...



lazyuser
02-24-2009, 12:09 PM
i am working on a batch application where i need to open word documents in a folder(specified folder), check for word documents, open a copy of the document, iinsert some links into it and then save this copy with a prefix "NEW_". my tool keeps doing this for all the word documents until there are not more documents left in the folder.

all this works fine, however, i need to skip a document that already has a "NEW_" prefix. just skip that document and pick up the next document without the prefix.

i am able to trap this. but how do i continue from there?

this is my code if you need it...



If (objFSO.FolderExists(txtSourceFolder.Text)) Then

Set objfolder = objFSO.GetFolder(txtSourceFolder.Text)

If objfolder.Files.Count = 0 Then 'check to see if any files exist in the folder
MsgBox "There are no documents to be processed in this folder!", vbOKOnly, appName
Exit Sub
Else
MsgBox "There are " & objfolder.Files.Count & " files in this folder.", vbOKOnly, appName
For Each objFile In objfolder.Files

If Word.Documents.Count > 0 Then

If Right(UCase(objFile.Name), 4) = ".DOC" Then 'check for files ending with .doc extension


If Left(UCase(objFile.Name), 4) = "NEW_" Then
MsgBox "IGNORE THIS FILE"
Exit Sub
Else

Word.Documents.Open (objFile.Path)
FindCitations

Set objDoc = Documents(Documents.Count - 1)
If objDoc Is Nothing Then 'counter keeps track of how the number of documents being processed
docCount = 0
Else
docCount = docCount + 1
End If

If Not objDoc Is Nothing Then 'saving the processed document with the prefix "NEW_"
objDoc.SaveAs (txtDestinationFolder.Text & "NEW_" & objFile.Name)

objDoc.Close
End If

If Documents.Count > 0 Then
Set objDoc = Documents(Documents.Count - 1)
If Not objDoc Is Nothing Then
objDoc.Close
End If
End If
End If
Else
MsgBox "No Word Documents exist in selected folder.", vbOKOnly, appName
Exit Sub
End If 'end check for documents ending with .doc extension
Else
MsgBox "No Documents exist in selected folder.", vbOKOnly, appName
End If
Next
End If 'end check to see if any files exist in the folder.

Else
MsgBox "Specified folder does not exist. Please choose a valid folder.", vbOKOnly, appName
txtDestinationFolder.SetFocus
Exit Sub
End If

MsgBox docCount & " Documents have been processed successfully!", vbOKOnly, appName



obviously, i cannot say end or exit sub here as this would take me out of the application.

help will be greatly appreciated. its quite urgent. thankssss

lazyuser
02-24-2009, 12:51 PM
never mind. i figured it out. i am first checking the documents for the name. i work on the documents that do not have this prefix.

thanksss anyways.

lucas
02-24-2009, 01:28 PM
mark it solved......

lazyuser
02-24-2009, 01:32 PM
mark it solved......

i looked for it. where do i do it?

lucas
02-24-2009, 01:38 PM
Top of the page....use the thread tools drop down....

lazyuser
02-24-2009, 01:46 PM
Top of the page....use the thread tools drop down....

thanks. done!

fumei
02-24-2009, 02:21 PM
1. Could you please use the underscore character more? It took me ten minutes just to grab to code out of here and indent it enough in my VBE so I could actually read it. Thanks.

2. Could you explain the relationship of these?

For Each objFile In objfolder.Files
If Word.Documents.Count > 0 Then

The first line is processing with a FileSystemObject, the second is the application (Word) active documents count. What is the connection?

3. I am not following the logic of:
Word.Documents.Open (objFile.Path)
FindCitations

Set objDoc = Documents(Documents.Count - 1)
' counter keeps track of
' how the number of documents being processed
If objDoc Is Nothing Then
How can objDoc be Nothing, if you just Set it? Further, as you have just opened the file, why are you using = Documents(Documents.Count - 1)?

You could of course be doing something in FindCitations (whatever that does) that changes the Document index number, but in that case, simply Set objDoc BEFORE the call to FindCitations.
Set objDoc = Word.Documents.Open (objFile.Path)

FindCitations


4. What - really - is the point of:
MsgBox "IGNORE THIS FILE"
when you are just ignoring it? Do they really need to know? Do they really need to have to click the darn OK button?

5. Since you ARE actually opening a file, why bother with an IF regarding docCount? Just count it.

6. Try this:

Dim docCount As Long

If (objFSO.FolderExists(txtSourceFolder.Text)) Then
Set objfolder = objFSO.GetFolder(txtSourceFolder.Text)
'check to see if any files exist in the folder
If objfolder.Files.Count = 0 Then
MsgBox "There are no documents to be processed " & _
"in this folder!", vbOKOnly, appName
Exit Sub
Else
' if there are no .DOC files
' may as well get out now
' so count them
For Each objFile In objfolder.Files
If Right(UCase(objFile.Name), 4) = ".DOC" Then
docCount = docCount + 1
End If
' if count = 0 then bye bye
If docCount = 0 Then
MsgBox "No Word Documents exist in selected folder.", _
vbOKOnly, appName
Exit Sub
End If
Next

' anything after means there ARE files
' and there ARE .DOC files
' but RESET docCount to collect the number
' of files actually processed
docCount = 0

For Each objFile In objfolder.Files
' check for files ending with .DOC extension
' and NOT "NEW_"
' only those files will be actioned
If Right(UCase(objFile.Name), 4) = ".DOC" And _
Left(UCase(objFile.Name), 4) <> "NEW_" Then
' file IS .doc and NOT "New_"
' so do the action
Set objDoc = Word.Documents.Open(objFile.Path)
FindCitations
With objDoc
.SaveAs (txtDestinationFolder.Text & _
"NEW_" & objFile.Name)
.Close
End With
docCount = docCount + 1
End If
Next ' to the next file
Else
MsgBox "Specified folder does not exist. " & _
"Please choose a valid folder.", vbOKOnly, appName
txtDestinationFolder.SetFocus
Exit Sub
End If

MsgBox docCount & _
" Documents have been processed successfully!", _
vbOKOnly, appName

End Sub

lazyuser
02-24-2009, 03:33 PM
thanks fumei! but this code does not save the first file that it opens. i have to again manually close each of the document that it opens.

it sees 4 documents in the folder.
it opens document 1. works on it. saves the copy (save as...NEW_document1.doc). closes the NEW document. leaves document 1 open.
its opens document 2. works on it. saves the copy (save as...NEW_document2.doc). closes the NEW document. leaves document 2 open.
and so on.

eventually, i have to manually close each of the open documents. they should be closed too, when the NEW document gets saved.

fumei
02-25-2009, 02:06 PM
Sorry, I am not following.

"it opens document 1. works on it. saves the copy (save as...NEW_document1.doc). closes the NEW document. leaves document 1 open."

No, how can that be?

It leaves the current document - the one you are running the code from! - open, yes.

With objDoc
.SaveAs (txtDestinationFolder.Text & _
"NEW_" & objFile.Name)
.Close
End With
objDoc is the file opened, then SavedAs.

Ooops.....I know possibly why.

Try adding a objDoc = Nothing


With objDoc
.SaveAs (txtDestinationFolder.Text & _
"NEW_" & objFile.Name)
.Close
End With
Set objDoc = Nothing
Although it should (if VBA is behaving as some say it does...ahem.....) be explicitly Set as a new object in the next iteration of the loop.

Question: what is the document you are executing the code from?

lazyuser
02-25-2009, 02:40 PM
Question: what is the document you are executing the code from?

my code is in a dot file. i open word ( blank doc) then load this dot file on top of it and only then am i able to view the code.

obviously, we cannot give the code away to end users. so i am trying to run this app on a blank document without loading the dot file (it is built in ofcourse, as a macro).

however, i need to keep a blank document open in order to execute this code. if close the document and still keep word running (just the gray screen from where we can open a doc), i get error - error 5479.

hope this helps. i am researching on why i am getting this error. thanks for your reply fumei. that was a big help.

fumei
02-25-2009, 02:45 PM
OK, let me see if I have this correct.

"i open word ( blank doc) then load this dot file on top of it and only then am i able to view the code. "

So you are opening the .dot file itself? Not a good idea. Load it as a add-in, a global template. You can then execute its code without actually opening it.

How does that first statement work with:

"so i am trying to run this app on a blank document without loading the dot file "

lazyuser
02-25-2009, 03:07 PM
Load it as a add-in, a global template. You can then execute its code without actually opening it.

i am doing this on another computer to run my app. to debug it, i need to load the dot file, though.



How does that first statement work with:
"so i am trying to run this app on a blank document without loading the dot file "

I am now able to work around the runtime error 5479. but i still need to get my app to work when no word document is open.

i would want the app to even run when theres no blank doc open. what i mean to say is: start button-->all programs-->microsoft office-->word
you'll see a word instance on a gray screen with the menu bar visible so you can open an existing or new document. (I wish i could paste a screen shot here). my app does not detect any documents in this scenario. it says no documents found.

whereas in the second scenario - start button-->all programs-->microsoft office-->word-->blank document (the white screen) the app runs fine and does all its suppsed to do.