PDA

View Full Version : Word VBA - couple of questions



edyau
11-06-2007, 05:50 AM
Hi,

I have some code that converts a folder of .xml files into .doc files. It runs from within Word, when the user clicks on a toolbar button. This works well, but there are some extra features I need

Sub convertFolder()

Dim wdApp As Word.Application
Dim doc As Word.Document
Dim strCompleteFileName As String
Dim strFolderPath As String

'carry out for all files in folder
strFolderPath = ActiveDocument.Path

For Each strFilename In GetAllFilesInDir(strFolderPath)
'open word doc
' MsgBox strFilename

strCompleteFileName = strFolderPath & "\" & strFilename
' MsgBox strCompleteFileName

If Not strFilename = ActiveDocument.Name And Right(strFilename, 3) = "xml" Then
Set wdApp = New Word.Application
Set doc = wdApp.Documents.Open(FileName:=strCompleteFileName, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False, Revert:=False)


'save
doc.SaveAs FileName:=strCompleteFileName & ".doc", FileFormat:=wdFormatDocument

'clean up
doc.Close SaveChanges:=False
Set doc = Nothing

wdApp.Quit SaveChanges:=wdDoNotSaveChanges
Set wdApp = Nothing
End If
Next strFilename


End Sub


a) I need it to delete all the .xmls as it goes, but if it did so, then it would crash when it tries to delete the document it's running from.

b) I'm need to remove all the xml tags from it when it is converted. The routine below does that on 1 document. I've tried adding doc.Select to the code above and running it, but it still works on the document it is being run from. Why is that?

Sub deleteTags()
'delete xml tags
Selection.WholeStory
Selection.Cut
Selection.PasteSpecial DataType:=wdPasteText
End Sub

Any thoughts on any of these? Your help would be thankfully received!

OTWarrior
11-07-2007, 02:59 AM
For your first question, couldn't you make a copy of the document that you want to convert, then delete the other copy when done?

TonyJollans
11-07-2007, 04:47 AM
One thing at a time ..

After you've done a SaveAs you are no longer editing the original document and should be able to delete it. This greatly simplified code works ..

Documents.Open(docname).SaveAs newdocname
Kill docname

TonyJollans
11-07-2007, 04:50 AM
For the second question.

You open the document Visible:=False, so it cannot be selected. You will have to work with its Range directly - which is, of course, better anyway.

fumei
11-07-2007, 03:28 PM
"It runs from within Word"

If that is so, why are you making instances of Word?

edyau
11-08-2007, 11:53 AM
Thanks all for the kind help. I feel I'm getting even more confused though, if that is possible!

Forgive me if my understanding isn't quite right. Here's my explanation of what is going on, but feel free to correct me if I have the wrong idea.

The code is deployed as a plugin. It runs from a word document that is in the folder that is being processed. That is the container for running the code - it needs to run somewhere! It opens an new instance of Word for each document it's trying to rename, does it then cleans up after. If I didn't do that, how would the for loop function?!

Below is my revised code - I kept the code for open separate from the saveas for clarity:


For Each strFilename In GetAllFilesInDir(strFolderPath)
'open word doc

strCompleteFileName = strFolderPath & "\" & strFilename

If Right(strFilename, 3) = "xml" Then
Set wdApp = New Word.Application
Set doc = wdApp.Documents.Open(FileName:=strCompleteFileName, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False, Revert:=False)

'save
doc.SaveAs FileName:=strCompleteFileName & ".doc", FileFormat:=wdFormatDocument

'clean up
doc.Close SaveChanges:=False
Set doc = Nothing
Kill strCompleteFileName

wdApp.Quit SaveChanges:=wdDoNotSaveChanges
Set wdApp = Nothing
End If
Next strFilename

This does mostly what I want, but it can't delete the final document as this is open (that is where the code is running). That's the only thing I need help on....

I know this is not the cleanest way to do the conversion, but I'm a Java programmer, and I have only limited experience of VBA. These XML documents have been automatically created by a Java system, and this is for now, just a shortfix until I have time to do a longterm solution with our production platform.

TonyJollans
11-08-2007, 01:34 PM
You can do it all from the instance of Word you are running in - just lose the extra instance ...


If Right(strFilename, 3) = "xml" Then
Set doc = Application.Documents.Open(FileName:=strCompleteFileName, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False, Revert:=False)

'save
doc.SaveAs FileName:=strCompleteFileName & ".doc", FileFormat:=wdFormatDocument

'clean up
doc.Close SaveChanges:=False
Set doc = Nothing
Kill strCompleteFileName

End If


It's hard to say exactly what's best in this particular scenario (will this run unattended, for example, or always be run by the same person, or on the same machine) but running the code from one of the documents you are working on is definitely the wrong way to go. You must have it saved somewhere else anyway so why not put it in its own document and run it from there?

fumei
11-09-2007, 02:44 PM
"It runs from a word document that is in the folder that is being processed. That is the container for running the code - it needs to run somewhere! "

Indeed, and you have the There, ie. the Word document you are running it from. You can run all the loops you want from that instance, as Tony pointed out. Ther eis no need to have those instances of Word. In fact, it is a bad idea. Word is pretty tidy...but not enough that I would trust say 100 separate instances of it being created, even if you DO explicitly set each of them to Nothing.

I agree with Tony regarding running it from a document you are working on. Consider putting the code in a global template, a container for code.

TonyJollans
11-09-2007, 04:14 PM
Gerry, I'm sorry, I didn't realise my post was going to go so wide - I just cut and pasted from edyau's post (but he hadn't used VBA tags) - even I can't see it all! As it isn't offering me an Edit button, I can't change it, although I thought I ought to be able to edit my own posts (in fact it says at the bottom of the page that I can - it lies!)

fumei
11-10-2007, 10:23 PM
Sigh, I think I am giving up on complaining and/or requesting changes regarding the wide posts. I am getting to the point where I simply no longer read them. If a thread has a wide post, I back out. Unless of course it has a strong interest for me, in which case, I struggle through.

You did one - can't remember which right now - that was WAAAAAY wide. I wanted to look at the code in it, but....did not.

Hmmm, I did not think it was the one here, but maybe. It certainly is, ummm, wide.

YOU can't see it all????? Hahahahahaha, I hope you do not take offence, but...chortle, I'm glad.

You can not edit? Bummer.

:rotlaugh:


:hi: