Log in

View Full Version : Replacing Text In A Bookmark in Footer on Save



bstephens
06-10-2011, 07:33 PM
Hi, I am trying to create a macro that works as follows:

Assume the user is working on a document called "S:\WP\access\70125.01\abc700.doc"
Upon the user saving the document, the following complex string is inserted at a bookmark in the footer ("bkmName") as follows:


2011-70125.01-abc700
Where "2011" is the current year inserted as a text string, "70125.01" is a string that is backed out from the active document file path (the folder or subfolder closest to the filename), "abc700" is the active document file name (backed out from cases where the document has either a 3 character (.doc) or 4 character (.docx) extension, and the dashes are inserted as strings.

I've been researching and discovered the below code will insert (and replace) simple text strings at my bookmark ("bkmName") as pre-defined in the footer of my template, however, the problem is that I can only get it to insert simple text strings.

Anyone have guidance as to how to get the code below to insert the more complex string as quoted above at bkmName?


Sub WriteTextToBookmarkSub(doc As Word.Document, bkmName As String, newText As String)
Dim rng As Word.Range
If doc.Bookmarks.Exists(bkmName) Then
Set rng = doc.Bookmarks(bkmName).Range
rng.Text = newText
doc.Bookmarks.Add bkmName, rng
End If
End Sub

Sub WriteTextToBookmark()
Dim doc As Document
Set doc = ActiveDocument
WriteTextToBookmarkSub doc, "bkmName", "this is only a simple string!"
End Sub
Thanks in advance for any help!

gmaxey
06-10-2011, 10:14 PM
Try:


Sub ExtractSring()
Dim doc As Document, pStr As String, pPath As String, i As Long
Set doc = ActiveDocument
pStr = Format(Date, "yyyy")
pStr = pStr & "-"
pPath = doc.Path
i = Len(pPath) - InStrRev(pPath, "\")
pStr = pStr & Right(pPath, i) & "-" & Left(doc.Name, InStr(doc.Name, ".") - 1)
WriteTextToBookmarkSub doc, "bkmName", pStr
End Sub
Sub WriteTextToBookmarkSub(doc As Word.Document, bkmName As String, newText As String)
Dim rng As Word.Range
If doc.Bookmarks.Exists(bkmName) Then
Set rng = doc.Bookmarks(bkmName).Range
rng.Text = newText
doc.Bookmarks.Add bkmName, rng
End If
End Sub





Hi, I am trying to create a macro that works as follows:

Assume the user is working on a document called "S:\WP\access\70125.01\abc700.doc"
Upon the user saving the document, the following complex string is inserted at a bookmark in the footer ("bkmName") as follows:

Where "2011" is the current year inserted as a text string, "70125.01" is a string that is backed out from the active document file path (the folder or subfolder closest to the filename), "abc700" is the active document file name (backed out from cases where the document has either a 3 character (.doc) or 4 character (.docx) extension, and the dashes are inserted as strings.

I've been researching and discovered the below code will insert (and replace) simple text strings at my bookmark ("bkmName") as pre-defined in the footer of my template, however, the problem is that I can only get it to insert simple text strings.

Anyone have guidance as to how to get the code below to insert the more complex string as quoted above at bkmName?


Sub WriteTextToBookmarkSub(doc As Word.Document, bkmName As String, newText As String)
Dim rng As Word.Range

If doc.Bookmarks.Exists(bkmName) Then
Set rng = doc.Bookmarks(bkmName).Range
rng.Text = newText
doc.Bookmarks.Add bkmName, rng
End If
End Sub

Sub WriteTextToBookmark()
Dim doc As Document
Set doc = ActiveDocument
WriteTextToBookmarkSub doc, "bkmName", "this is only a simple string!"
End Sub
Thanks in advance for any help!

bstephens
06-10-2011, 10:19 PM
Came up with this:


Sub WriteTextToBookmarkSub(doc As Word.Document, bkmName As String, newText As Variant)
Dim rng As Word.Range

If doc.Bookmarks.Exists(bkmName) Then
Set rng = doc.Bookmarks(bkmName).Range
rng.Text = newText
doc.Bookmarks.Add bkmName, rng
End If
End Sub

Sub WriteTextToBookmark()
Dim doc As Document
Set doc = ActiveDocument
Var1 = InStr(ActiveDocument.Name, ".") - 1
Var2 = Right(ActiveDocument.Path, Len(ActiveDocument.Path) - InStrRev(ActiveDocument.Path, "\"))
String1 = Format((Date), "yyyy") & "-" & Var2 & "-" & Left(ActiveDocument.Name, Len(ActiveDocument.Name) - (Len(ActiveDocument.Name) - Var1))
WriteTextToBookmarkSub doc, "bkmName", String1
End Sub


Anyone see a way to make smarter/improve?

Also, want to build a new concept in, where I do a conditional test, where if the last digit of the adjacent folder or subfolder doesn't contain an integerm then the function will look for "" until it finds the first subfolder that starts with an integer (OK, horrible explanation, so I give example).

Ex:

If the directory is: S:\WP\access\70125.01\abc700.doc, the function inserts:

2011-70125.01-abc700

at the bookmark.

However, if the directory is: S:\WP\access\70125.01\agreements\abc700.doc

the function would insert

2011-70125.01\agreements-abc700

at the bookmark.

Thanks for any input!

-Brian

bstephens
06-10-2011, 10:21 PM
Greg, thanks, you posted before I could submit my post!! Any thoughts on a smart way to add the conditional?

BTW: thanks for the code!

Best,
Brian

gmaxey
06-10-2011, 10:48 PM
Not very elegant or smart perhaps but try:


Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim pPathAndName As String, pStr As String
Dim arrStr() As String
Dim i As Long, j As Long
pPathAndName = "S:\WP\access\70125.01\agreements\abc700.doc"
arrStr = Split(pPathAndName, "\")
For i = UBound(arrStr) To 0 Step -1
If Left(arrStr(i), 1) Like "[0-9]" Then
pStr = Format(Date, "yyyy") & "-" & arrStr(i) & "\"
j = i + 1 'edited this line.
Exit For
End If
Next
For i = j To UBound(arrStr) - 1
pStr = pStr & arrStr(i) & "-"
Next i
pStr = pStr & Left(arrStr(UBound(arrStr)), InStr(arrStr(UBound(arrStr)), ".") - 1)
End Sub

bstephens
06-11-2011, 01:56 PM
Thanks Greg.

Darn, I didn't think this through... if I create a non-linked section break, and apply my autotext footer entry with the bookmark, the bookmark obviously is only applied once in the document. I need the "document id number" (what i referred to as the "complex string") to update in all sections of the document.

Going to re-think this.