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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vb Probs in Word app

Status
Not open for further replies.

phinala

Programmer
Oct 17, 2005
3
SE
Hi all!

I have a problem with a word document.

I have Built a invoice template for a customer. My tought is that when he press a button, after he has filled the invoice, a vb function is called. The fuction will go to a pree defined mapPath. It will lopp all the wordfiles and select the max filenameNumber. All the files is named with i number. Then it will add 1 to that number. Then it will save and name the new invoice with the new number.

Here is the code:

Private Sub cmdSaveDok_Click()
Dim newName As String
Dim maxVal As Integer
Dim dirVal As Integer
Dim pStr As String
Dim nVal As Integer
Dim MyPath As String
Dim MyName As String


'MyPath = "\\JOHAN\Takman Dokument\Fakturor\"
MyPath = "C:\Temp\"
maxVal = 0

MyName = Dir(MyPath, vbNormal)
'Kör loopen
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(MyPath & MyName) And vbNormal) = vbNormal Then
'Sorterar ut siffrorna i filnamnet och sparar undan dem
If IsNumeric(Left(MyName, Len(MyName) - 4)) Then
dirVal = CInt(Left(MyName, Len(MyName) - 4))
If dirVal > maxVal Then maxVal = dirVal
End If
End If
End If
MyName = Dir
Loop
'Plussar på ett
If maxVal = 0 Then
nVal = 10000
Else
nVal = maxVal + 1
End If
'Sparar dokumentet
newName = MyPath & CStr(nVal) & ".doc"
ThisDocument.TextBox1.Text = CStr(nVal)
ThisDocument.SaveAs newName

MsgBox ("Dokument " & newName & " är sparat.")

End Sub

My problem is:
The code works just fine excepts that the ThisDocument.TextBox1.Text dose not show the new number and when a quit the template. the new max number is not saved in the template. SO that next time my customer open the template he will se tha last used invioce number.

How do i do that?

Do u follow me? if not i will try to explane it better

/Tyronne
 
What you're attempting is dangerous in so many ways!

What if a file gets deleted, etc? MaxFiles+1 now overwrites/fails... This and host other dangers! I would *strongly* advise you use Access or some other database to go about this. This kind of information can only be reliably saved in some kind of database - and being that it's financial data, you cannot mess around with (what WILL become) hundreds of disparate documents.

Also, how would to Sort/Seek/Report on these files?
 
.saveas will happily overwrite an existing file with the same name, and won't give you any warning or notice that it is doing so. At the very least you need to check if a file with that name already exists before you attempt to save it.

 
HI!

Yes i know that but my customer who wants this is very stuborn. So i have to as he says.

Do you have any ideas about my problem

/Tyronne
 
Hi phinala,

It would be far better to have a small text file that stores an index number. You could then read that, add 1 to it, update the number in both the text file and use the updated number in your filename. For example:
Code:
Dim lCounter as Long
Open "C:\Invoices\InvoiceLog.txt" For Input As #1
Input #1, lCounter
lCounter = lCounter + 1
Close #1
DoEvents
Open "C:\Invoices\InvoiceLog.txt" For Output As #1
Write #1, lCounter
Close #1
That way, there's less risk of an existing file being overwritten.

Cheers
 
HI!

Thanks for taking time to help me!

MintJulep: If you refer to
Code:
newName = MyPath & CStr(nVal) & ".doc"
    ThisDocument.TextBox1.Text = CStr(nVal)
    ThisDocument.SaveAs newName

So when i debug the CStr(nVal) it do have a value and the ThisDocument.TextBox1.Text do also get a value.

Is there anyway that i can attach a file to this post, so that you guy´s can have a look att the document.

The Texfile solution i also think is better because i loop through all the invoice files. So i will do that when i got the document workung. by the way! i am using office 2003 pro.

/Phinala
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top