Deleting part of a string

S

Sammy

Hi,

I am creating a macro to put our document management system's document ID
number in the footer. The field comes in like this:

::Odma\Pcdocs\Name\2102921\1

I need it to look like this:

2102921.1

Not sure how to manipulate the entire line to eliminate what I don't want.
The rest of the macro works although I probably wouldn't win any awards for
the code. So far I have:

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count

With ActiveDocument.Sections(i)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Footers(wdHeaderFooterFirstPage).Range.Delete 'clears out existing footer
.Footers(wdHeaderFooterPrimary).Range.Delete

'puts ID in first page footer
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Caps \p ", PreserveFormatting:=True

'puts ID in primary footer
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Caps \p ", PreserveFormatting:=True
End With

Next i

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Can you help? I've tried a variety of things over the past few weeks and
tried to figure this out myself, now I'm up against deadline pressure.
Thanks!
 
J

Jean-Guy Marcil

Sammy was telling us:
Sammy nous racontait que :
Hi,

I am creating a macro to put our document management system's
document ID number in the footer. The field comes in like this:


I need it to look like this:

2102921.1

Not sure how to manipulate the entire line to eliminate what I don't
want. The rest of the macro works although I probably wouldn't win
any awards for the code. So far I have:

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count

With ActiveDocument.Sections(i)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Footers(wdHeaderFooterFirstPage).Range.Delete 'clears out
existing footer .Footers(wdHeaderFooterPrimary).Range.Delete

'puts ID in first page footer
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "FILENAME \* Caps \p ", PreserveFormatting:=True

'puts ID in primary footer
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _ "FILENAME \* Caps \p ", PreserveFormatting:=True
End With

Next i

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Can you help? I've tried a variety of things over the past few weeks
and tried to figure this out myself, now I'm up against deadline
pressure. Thanks!

Here is one way of doing it (with a revised version of your code that does
not use the unpredictable and less then efficient pane view to access the
footer).

The string manipulation may not be what you want as it depends on the
consistency of the data it will be handling.

'_______________________________________
Dim i As Integer
Dim rgeFooter As Range
Dim strTemp() As String
Dim strFinal As String

For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Footers(wdHeaderFooterFirstPage).Range.Delete 'clears out existing
footer
.Footers(wdHeaderFooterPrimary).Range.Delete

'puts ID in first page footer
Set rgeFooter = .Footers(wdHeaderFooterFirstPage).Range
With rgeFooter
.Collapse wdCollapseStart
.Fields.Add Range:=rgeFooter, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Caps \p ", PreserveFormatting:=True
'Get field into range
.MoveEnd wdParagraph, 1
'convert field into text
.Fields(1).Unlink
'remove ¶ from range
.MoveEnd wdCharacter, -1
strFinal = .Text
'modify text
strTemp = Split(strFinal, "\")
strFinal = strTemp(UBound(strTemp) - 1) & "." _
& strTemp(UBound(strTemp))
.Text = strFinal
End With

'puts ID in primary footer
Set rgeFooter = .Footers(wdHeaderFooterPrimary).Range
With rgeFooter
.Collapse wdCollapseStart
.Text = strFinal
End With
End With
Next i
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Sammy,

Here is a option for determining the string if your format is always
as you describe. Do you really need a field?
Sub Test()
Dim i As Integer
Dim pInputStr() As String
Dim pRevisedStr As String
pInputStr() = Split("Odma\Pcdocs\Name\2102921\1", "\")
pRevisedStr = pInputStr(3) & "." & pInputStr(4)
MsgBox pRevisedStr
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Footers(wdHeaderFooterFirstPage).Range.Text = pRevisedStr
.Footers(wdHeaderFooterPrimary).Range.Text = pRevisedStr
End With
Next i
End Sub
 
M

macropod

Hi Sammy,

To extract & convert your string, try something based on:

Sub Test()
Dim MyStr As String
MyStr = "::Odma\Pcdocs\Name\2102921\1"
MsgBox Split(MyStr, "\")(UBound(Split(MyStr, "\")) - 1) & "." & Split(MyStr, "\")(UBound(Split(MyStr, "\")))
End Sub

Cheers
 
S

Sammy

THANK YOU ALL FOR YOUR HELP. Since the doc id for each document will be
different I do need to use the field, it looks like the first response will
work best, Jean-Guy I will give it a try at work on Monday. Thanks again so
much.

macropod said:
Hi Sammy,

To extract & convert your string, try something based on:

Sub Test()
Dim MyStr As String
MyStr = "::Odma\Pcdocs\Name\2102921\1"
MsgBox Split(MyStr, "\")(UBound(Split(MyStr, "\")) - 1) & "." & Split(MyStr, "\")(UBound(Split(MyStr, "\")))
End Sub

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

Sammy said:
Hi,

I am creating a macro to put our document management system's document ID
number in the footer. The field comes in like this:

::Odma\Pcdocs\Name\2102921\1

I need it to look like this:

2102921.1

Not sure how to manipulate the entire line to eliminate what I don't want.
The rest of the macro works although I probably wouldn't win any awards for
the code. So far I have:

Dim i As Integer
For i = 1 To ActiveDocument.Sections.Count

With ActiveDocument.Sections(i)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Footers(wdHeaderFooterFirstPage).Range.Delete 'clears out existing footer
.Footers(wdHeaderFooterPrimary).Range.Delete

'puts ID in first page footer
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Caps \p ", PreserveFormatting:=True

'puts ID in primary footer
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Caps \p ", PreserveFormatting:=True
End With

Next i

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Can you help? I've tried a variety of things over the past few weeks and
tried to figure this out myself, now I'm up against deadline pressure.
Thanks!
 
S

Sammy

Hello,

Jean-Guy Marcil, I'm trying to get a hold of you regarding some paid VBA
consulting if you would be interested but I can't seem to get an email to
you. You can email me at (e-mail address removed). Thank you!
 
G

Gary O

Jean-Guy Marcil said:
'_______________________________________
Dim i As Integer
Dim rgeFooter As Range
Dim strTemp() As String
Dim strFinal As String

For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Footers(wdHeaderFooterFirstPage).Range.Delete 'clears out existing
footer
.Footers(wdHeaderFooterPrimary).Range.Delete

'puts ID in first page footer
Set rgeFooter = .Footers(wdHeaderFooterFirstPage).Range
With rgeFooter
.Collapse wdCollapseStart
.Fields.Add Range:=rgeFooter, Type:=wdFieldEmpty, Text:= _
"FILENAME \* Caps \p ", PreserveFormatting:=True
'Get field into range
.MoveEnd wdParagraph, 1
'convert field into text
.Fields(1).Unlink
'remove ¶ from range
.MoveEnd wdCharacter, -1
strFinal = .Text
'modify text
strTemp = Split(strFinal, "\")
strFinal = strTemp(UBound(strTemp) - 1) & "." _
& strTemp(UBound(strTemp))
.Text = strFinal
End With

'puts ID in primary footer
Set rgeFooter = .Footers(wdHeaderFooterPrimary).Range
With rgeFooter
.Collapse wdCollapseStart
.Text = strFinal
End With
End With
Next i
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org

Can a guru advise what additional code needs to be added so that the above
code can display a complete footer as follows in Arial font 8pt:

(Left) Current Date
(Centre) Page Number
(Right) Abbreviated Path (ie above code)

How would I auto execute this macro whenever a new document is being created
from our normal.dot?

I also understand that I will need to intercept the "save" and "print" word
commands so that the above custom details will be updated whenever printing
or saving the document.

Is it further possible to specifiy whether the code will be executed on the
"first page", only on "odd pages" or only on "even pages".

Thanks for your patience.

Regards


Gary O
 
D

Doug Robbins - Word MVP

Rather than trying to automate this process when a new document is being
created from your normal.dot template, you should create a new template that
contains the required information in its footer.

For the date use a { CreateDate } field for the page number use a { Page }
field. For the modified path\filename, insert a { DOCVARIABLE
varPathFilename } field and use the following code the set that variable and
display the result in the docvariable field

Dim fpath As String
Dim strTemp() As String
With ActiveDocument
fpath = .FullName
strTemp = Split(fpath, "\")
fpath = strTemp(UBound(strTemp) - 1) & "." _
& strTemp(UBound(strTemp))
.Variables("varPathFilename").Value = fpath
.PrintPreview
.ClosePrintPreview
End Sub

Have that code in macros created in that template with the names of

FileSave
FileSaveAs
FilePrint
FilePrintDefault

or have it in a separate routine which you call for each of the above named
macros.

The other things can all be done, but how is the "specifying" going to be
done.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Gary O

Doug Robbins - Word MVP said:
Rather than trying to automate this process when a new document is being
created from your normal.dot template, you should create a new template that
contains the required information in its footer.

For the date use a { CreateDate } field for the page number use a { Page }
field. For the modified path\filename, insert a { DOCVARIABLE
varPathFilename } field and use the following code the set that variable and
display the result in the docvariable field

Dim fpath As String
Dim strTemp() As String
With ActiveDocument
fpath = .FullName
strTemp = Split(fpath, "\")
fpath = strTemp(UBound(strTemp) - 1) & "." _
& strTemp(UBound(strTemp))
.Variables("varPathFilename").Value = fpath
.PrintPreview
.ClosePrintPreview
End Sub

Have that code in macros created in that template with the names of

FileSave
FileSaveAs
FilePrint
FilePrintDefault

or have it in a separate routine which you call for each of the above named
macros.

The other things can all be done, but how is the "specifying" going to be
done.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Doug,

I believe what you've suggested is exactly what I was after. When I go back
to work after the weekend I'll implement what you've suggested and advise how
it is working.

Regards


Gary O
 
G

Gary O

Hi Doug,

I'm a newbie with macros and I ahven't been able to get your macro to work
for me.

I created a new macro and copied and pasted your code into the macro and
saved it as "varPathFilename".

I then went to the document and tried to enter a new DOCVARIABLE field
assigning the name "varPathFilename" and nothing happened that I could see. I
know I'm doing something wrong, but don't know what, or, will it only work
using the four commands noted.

If so, how do I do that.

I am sorry for being such a dunce.

Regards


Gary O
 
D

Doug Robbins - Word MVP

You will need four macros, each containing the same code and named

FileSave
FileSaveAs
FilePrint
FilePrintDefault



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top