PDA

View Full Version : Solved: ACCESSING FILES IN A FOLDER



Saintcools
07-08-2006, 12:34 AM
Hi All,
I have a small problem in running my VBA program.

This sub is used to select the folder which has Doc file
Private Sub ProcessFolder(fol As folder)
Dim subfolders As Folders
Dim folder As folder
Dim filecol As Files
Dim fil As File
Set subfolders = fol.subfolders
Set filecol = fol.Files
For Each fil In filecol
If UCase(Right(fil.Name, 3)) = "DOC" Then
Label1.Caption = " Adding Tags : " & fil.Path
'DoEvents
Call Savetext(fil.Path)
End If
Next
End Sub

This function is called inside the Processfolder sub
Function Savetext(FileName As String) As Boolean
Dim docfilename As String
Dim wrddoc As Object
On Error GoTo savetexterror
docfilename = FileName
'OPEN WORD DOCUMENT:
Set wrddoc = wrdApp.Documents.Open(docfilename)
'Call Functions
Call itags

'CLOSE WORD DOCUMENT
ActiveDocument.Close (wdSaveChanges)
wrddoc.Quit
Savetext = True
Savetextend:
Exit Function
savetexterror:
Savetext = False
Resume Savetextend
End Function

This sub is called inside the savetext function
private Sub itags()
Dim p As String
p = "<p align=""justify"">"
pc = "</p>"
For Each Paragraph In ActiveDocument.Paragraphs
Paragraph.Range.Select

If Paragraph.Style = "Normal" Then
Selection.Range.InsertBefore (p)
Selection.Range.MoveEnd unit:=wdCharacter, Count:=-1
Selection.Range.InsertAfter (pc)
End If
Next
End Sub

the problem is the itags sub keeps running infinitly inside the savetext function.all i need is that the sub ITAGS to one once and not infinite times.thanks in advance for your help.

fumei
07-08-2006, 02:22 PM
1. Use Option Explicit. I just hate that you do not declare your variables. pc is not declared. Paragraph (and that is a BAD BAD word to use as a variable!!!!!) is not declared.

This is poor programming.

2. have you actually tried stepping through this...oh, I don't know...to see what is actually happening? It just may give you a clue. That is what stepping through is for.

Saintcools
07-09-2006, 09:31 PM
Hi Fumei,
I have send the main part of my program and i am using option explicit in my program.Can u point which stepping u are asking.If u are asking about the step in processfolder sub it is for the files in the folder.awaiting your reply.

fumei
07-10-2006, 10:51 PM
Uh...Step Into. It is a process in the VBE where you can step through your code.....one line.....at.....a.....time.

You can switch back and forth between the code editor and your document and SEE what each step (line) is doing.

It is a basic tool of debugging code.

Try it. Step into your code, and actually look at what each line does. It helps some times.

MOS MASTER
07-11-2006, 08:54 AM
To help you on youre way debugging your code you can step through your code with F8 key. :yes

Saintcools
07-18-2006, 09:16 AM
Hi
thanks guys i solved the problem by doing some modification but now the problem is for example if the document name is "TEST" the first file that is open for process is "~$TEST".why the document is opening like this.when is process is completed i have two documents.one "TEST" and the other "~$TEST".any solution and why is this behavior.

fumei
07-19-2006, 09:49 PM
Sheeesh.

1. Please use separate post for separate questions.
2. If you work out a solution to a poated question, post your answer.

What happens when you open a document in Word? It makes a temp file ~documentname.doc. If you have a folder operation going into that folder...it is going to find that file, now isn't it?

Do a test for it. If UCase(Right(fil.Name, 3)) = "DOC" Thenwill still find that temp file. The filename is still ends with .doc.