PDA

View Full Version : [SOLVED:] Insert A Sequential DocID - Into Each Word Doc - Set The Sequential Starting Number



dj44
03-15-2016, 04:17 PM
Folks,

good day and evening to all.:)

I am trying to Insert a DocId number into a folder full of documents.

Now that I found all my good files. I need to catalogue them all. So in the future there is no chance of losing them.

What I mean by DocId - is each document will have a unique number inserted into it.

I need this DocID number to be inside the document, not as part of the file name.

I will give it a starting number eg 1200
The file name is not important - just to make sure that each file gets a sequential number inserted into it.

Example:

File1 : DocId 1201
File2 : DocId 1202
File3 : DocId 1203


My not so great version number 30+. I have been trying to write this script for the past week.



Sub InsertIDS()

'This is to insert a unique number into each document so that it can be catalogued

Dim SeqNumber, SeqStartNumber As Integer
Dim i as long

Dim file
Dim path As String

path = "C:\Users\DJ\Desktop\Files\"


SeqStartNumber = 2034


'Another loop of some kind? There is one already


file = Dir(path & "*.docx")


Do While file <> ""
Documents.Open FileName:=path & file

With ActiveDocument.Paragraphs(1).Range
.InsertBefore "DocID - " & SeqStartNumber++ ' This is wrong



file = Dir()



Loop

'Now move on to the next file and insert id


End With
End Sub





Now i have been testing all sorts for the past week and well - it turned up dud all my efforts.

Some help from the kind pros is needed.
A pro master must know how i have gone wrong, please advise.

I hope I am not running up the wrong tree here

thank you and cheers for your valuable advice:)

DJ

gmaxey
03-15-2016, 07:12 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document
Dim lngIndex As Long
Dim strFile As String, strPath As String

strPath = "C:\Users\DJ\Desktop\Files\"
lngIndex = 2034
strFile = Dir(strPath & "*.doc*")
Do While strFile <> ""
Set oDoc = Documents.Open(FileName:=strPath & strFile)
With oDoc
.Paragraphs(1).Range.InsertBefore "DocID - " & lngIndex '++ ' This is wrong
lngIndex = lngIndex + 1
.Close wdSaveChanges
End With
strFile = Dir()
Loop
lbl_Exit:
Exit Sub
End Sub

dj44
03-15-2016, 09:32 PM
Greg,

thank you for your generous help.

I really am stoked you made it work.

i got all these files and opening them 1 by one to enter the number, especially when its a 4 digit number had my memory playing trips on me.


I think i may have forgotten to actually put a loop when i tested this code :grinhalo:

SeqStartNumber++

Well I started one loop, then I did not know how to make another one.


I am humbled to benefit from your expertise, top man as always for sharing your time

thank you very much - I appreciate it

any way alls well that ends well thanks to you

Have a great week now:beerchug:

cheers

DJ