PDA

Click to See Complete Forum and Search --> : VB Macro help


Jnickrand
Aug 29th, 2005, 12:47 PM
I was wondering if someone could help me tweak this code a little. This a macro in word that should save the document as a txt file with current filename that it has. The code below is saving all of the files into one notepad file called wrkord. Basically the word document gets open through a program and it gets assigned a number like (R792384750943875.rtf) When the user saves the document it saves it to a folder on the network where another program picks it up. All files should be separate, not all lumped together like they are now. THANKS :wave:


Public Sub HMSSave(Optional informUser As Boolean = True)

'variables added for brit
Dim wpathdoc As String
Dim wpathtx As String
Dim docToClose As String
Dim docToClose As String
Dim foundstring As Long
Dim britpathtxt As String
Dim tempvar As String
'end of variables

'save a version for brit teleradiology
If ActiveDocument.Name Like "*_temp.doc" Then
System.Cursor = wdCursorWait
wpathdoc = ActiveDocument.Variables("wpathdocvar").Value
wpathtxt = ActiveDocument.Variables("wpathtxtvar").Value

foundstring = InStr(1, wpathtxt, "\")
tempvar = wpathtxt
While foundstring <> 0
tempvar = Mid(tempvar, foundstring + 1)
foundstring = InStr(foundstring, tempvar, "\")
Wend
'end of brit code

ActiveDocument.save 'saves the doc as "app_path + \temp.doc"
On Error Resume Next
Kill wpathdoc
Kill wpathtxt

' New for brit
Kill britpathtxt
' End

On Error GoTo 0
' store the name of the current document
docToOpen = ActiveDocument.FullName
' createtextfile (APP_PATH + "\temp.txt")
ActiveDocument.SaveAs FileName:=wpathtxt, fileformat:=wdFormatDOSTextLineBreaks
'Save a txt copy for brit teleradiology
britpathtxt = "I:\brit\" & ActiveDocument.Name
ActiveDocument.SaveAs fileName:=britpathtxt, fileformat:=wdFormatDOSTextLineBreaks
'end
'get the current name of the doc
docToClose = ActiveDocument.Name
're-open temp.doc for more editing
Documents.Open docToOpen
'Close the other doc
Document(docToClose).Close

If informUser = true then
MsgBox “Document saved.”, vbOKOnly + vbInformation, “HMS Trans”
End if
=============================================================This is the new code that they have in the Macro

Public Sub HMSSave(Optional informUser As Boolean = True)
shouldBeMoved = True
'Make sure to update all built in document properties.
ActiveDocument.ComputeStatistics wdStatisticCharacters
ActiveDocument.ComputeStatistics wdStatisticCharactersWithSpaces
ActiveDocument.ComputeStatistics wdStatisticLines
ActiveDocument.ComputeStatistics wdStatisticPages
ActiveDocument.ComputeStatistics wdStatisticWords

ActiveDocument.save

If informUser = True Then
If getType() = TYPE_DOCUMENT Then
VBA.MsgBox "Transcription document saved.", vbOKOnly + vbInformation, "HMS Transcription"
Else
VBA.MsgBox "Transcription template saved.", vbOKOnly + vbInformation, "HMS Transcription"
End If
End If

End Sub

Webtest
Aug 30th, 2005, 11:34 AM
At the very top of your module put in "Option Explicit" and I think you will identify some of your problems ... I think you have typos in variable names, and this is EXACTLY why it is so important to set this option!

While foundstring <> 0 ... I believe the return from InStr can be -1 (?) ... set this test for Greater than only. That's the only thing that makes sense.

For testing, put a MsgBox to display the generated filenames before each of the 2 "ActiveDocument.SaveAs" statements to be sure that you have the filenames correct. Also, if you do not have an extension specified, the parameters may coerce an extension different from the expected ".doc" extension???

Also, you need about 5 times as many comments as you have now.