Log in

View Full Version : Documentum counting?



BPrior
01-20-2017, 04:31 AM
Hello all!

So i need a little help for my work. I created a macro that copies my 'project.doc' fully into a new document, but also i would like to add something like that automatically adds a plus number in my 'Number' column whenever a new copy is created. Is there any possibility for this?:think:

gmaxey
01-20-2017, 09:16 AM
Probably, but without a better explanation or example of what you are trying to do then how is anyone's guess.

BPrior
01-21-2017, 01:59 PM
Hey!

Okay, so

Sub Macro()


Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
Selection.InsertFile FileName:="Project.docx", Range:="", ConfirmConversions _
:=False, Link:=False, Attachment:=False
WordBasic.PageSetupMargins Tab:=0, PaperSize:=0, TopMargin:="0.7", _
BottomMargin:="1", LeftMargin:="1.3", RightMargin:="0.7", Gutter:="0", _
PageWidth:="21", PageHeight:="29.7", Orientation:=0, FirstPage:=0, _
OtherPages:=0, VertAlign:=0, ApplyPropsTo:=4, FacingPages:=0, _
HeaderDistance:="1.25", FooterDistance:="1.25", SectionStart:=2, _
OddAndEvenPages:=0, DifferentFirstPage:=0, Endnotes:=0, LineNum:=0, _
CountBy:=0, TwoOnOne:=0, GutterPosition:=0, LayoutMode:=0, DocFontName:= _
"", FirstPageOnLeft:=0, SectionType:=1, FolioPrint:=0, ReverseFolio:=0, _
FolioPages:=1
End Sub

I use this macro for making my job easier, it's not so clean but this copies my 'project' doc into a new document. What would be nice for me that when everytime i create a new copy document this way, a plus number would be added automatically in my 'Nr: ***x' column, like a counter.

gmayor
01-21-2017, 10:40 PM
It would make more sense to save your project document as a macro enabled template containing the part of the following code that logs and applies the number, and create new documents from it, but you can use your existing document as a template.

We have no idea what your 'Nr: ***x' column is, but open your project document and bookmark the *** bit where the number is to go and name the bookmark bmCount. Save the document and change C:\Path\ in the macro to the location where the document is stored.

The macro stores the counter in the registry at HKEY_CURRENT_USER\SOFTWARE\VB and VBA Program Settings\MyProject\Counter and increments it each time a new project document is created.


Option Explicit
Sub NewProject()
Dim sCount As String
sCount = GetSetting("MyProject", "Counter", "Count")
If sCount = "" Then
sCount = "1"
Else
sCount = val(sCount) + 1
End If
Documents.Add Template:="C:\Path\Project.docx"
'do something with sCount e.g.
FillBM "bmCount", sCount
SaveSetting "MyProject", "Counter", "Count", sCount
lbl_Exit:
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

gmaxey
01-22-2017, 08:22 AM
As an alternative. You say number column which implies a table. Perhaps something along this line:


Sub NewProject()
On Error Resume Next
ThisDocument.Variables("Counter") = ThisDocument.Variables("Counter") + 1
If Err.Number <> 0 Then
ThisDocument.Variables("Counter") = 1
End If
On Error GoTo 0
Documents.Add Template:=ThisDocument.FullName
ActiveDocument.Tables(1).Cell(2, 4).Range.Text = ThisDocument.Variables("Counter") '2, 4 assumes row 2, column 4 of the table.
lbl_Exit:
Exit Sub
End Sub