VBA code to search for FileName in Word

J

jerem

I want to write a macro in Word that will find the filename (generated by a
document management system - Imanage) that resides on every page in the
Footer and then delete them. I can write a macro that deletes all footers,
but I do not want the page numbering in the footers to be disturbed and for
the life of me I cannot find sample code which represents FileName only. And
copying the field code which represents the file name in Word does not work
because the find will not look in the Footers.

Also, any way of doing a search in VB that will locate the end of every
sentence in a Word document. Want to write a macro that will find a period
and one space and replace it with a period and two spaces (just doing a
regular find and replace in Word pulls up abbreviations in midsentencea which
is not the desired result).
 
H

Helmut Weber

Hi,

for a full coverage, see:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

However, this might be sufficient:

Sub Test564()
' wdEvenPagesFooterStory = 8
' wdPrimaryFooterStory = 9
' wdFirstPageFooterStory = 11
Dim oFld As Field
Dim rTmp As Range
For Each rTmp In ActiveDocument.StoryRanges
Select Case rTmp.StoryType
Case 8, 9, 11
For Each oFld In rTmp.Fields
If oFld.Type = wdFieldFileName Then
Stop
' oFld.Delete ' if you like
End If
Next
End Select
Next
End Sub
Also, any way of doing a search in VB that will locate the end of every
sentence in a Word document. Want to write a macro that will find a period
and one space and replace it with a period and two spaces (just doing a
regular find and replace in Word pulls up abbreviations in midsentencea which
is not the desired result).

Impossible, IMHO.

'Word' and 'sentence' and 'abbreviation' and others
are names of concepts of natural, fuzzy(!) language.

There are two approaches to language,
list-based and rule-based.
But the lists are endless
and the rules are always changing,
and influence the lists...

--
Greetings from Bavaria, Germany

Helmut Weber, M.A. (linguistics), MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

Assuming that the field that contains the filename is the first field in the
footer, you should be able to do it with:

Dim i As Long
With ActiveDocument
For i = 1 To .Sections.Count
.Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields(1).Delete
.Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields(1).Delete
Next i
End With

For the second item, you should be able to do it using a Wildcard
Edit>Replace with

.. ([A-Z]{1,})

in the Find what control and

.. \1

in the replace with control

--
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
 
R

Russ

With VBA code you can use a find method to search within each header or
footer range. But you would need something unique about the filenames to
search for. Location or field or recurring pattern or amount of characters
or backslashes or the only nonfield.

I want to write a macro in Word that will find the filename (generated by a
document management system - Imanage) that resides on every page in the
Footer and then delete them. I can write a macro that deletes all footers,
but I do not want the page numbering in the footers to be disturbed and for
the life of me I cannot find sample code which represents FileName only. And
copying the field code which represents the file name in Word does not work
because the find will not look in the Footers.

Also, any way of doing a search in VB that will locate the end of every
sentence in a Word document. Want to write a macro that will find a period
and one space and replace it with a period and two spaces (just doing a
regular find and replace in Word pulls up abbreviations in midsentencea which
is not the desired result).

http://www.shaunakelly.com/word/concepts/rules_onespace/index.html
http://wordtips.vitalnews.com/Pages/T1497_An_Automatic_Two_Spaces_After_a_Pe
riod.html
 
R

Russ

Don't miss the links below, I sent about two spaces either. In the other
reply they were the same color as your post, so you may have missed them.
 
J

jerem

I tried the code you gave me. Does not seem to work, but I shall reference
the link you gave me since that seems to address the Find Anything Anywere.
Thanks for your response.
 
J

jerem

The problem is that there are varying operators who create the documents I
need to manipulate and it is a matter of individual preference as to whether
the page numbers will appear before the document identifier (doc #) or after.
But thanks for your response.

The second issue of replacing a period and one space with a period and two
spaces: I tried the ([A-Z]{1,}) in the Find with wildcards and the \1 in the
Replace -- very wierd, don't know what this is finding and replacing but the
first time I ran it it did 125 replaces, but of what I don't know. Could not
recognize any change in the document at all. Ran the find and replace again
in the same document and second time around made 65 replaces but same issue.
Don't know what it is changing. There is no visible difference from the
original document to the replacement document. What I did find works is if I
run a Macro I created which is called sticky spaces (replaces all titles
[i.e., Mr. and a space, Dr. and a space, Mrs. and a space , etc.] with Mr.
and a hard space, Dr. and a hard space, etc.). Once that macro completes and
it eliminates all titles with a period and a space with a period and a hard
space, I look for (.^32) a period and one space (^32 is the character code
for a space only) and I replace it with a period and 2 spaces. This, so far,
is the only method that I've come up with to globally replace a period and
one space at the end of sentences with a period and two spaces, however, I am
then left to look for abbreviations in midsentence to revert them back to a
period and one space.

But again, thanks for trying to help.
Assuming that the field that contains the filename is the first field in the
footer, you should be able to do it with:

Dim i As Long
With ActiveDocument
For i = 1 To .Sections.Count
.Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields(1).Delete
.Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields(1).Delete
Next i
End With

For the second item, you should be able to do it using a Wildcard
Edit>Replace with

.. ([A-Z]{1,})

in the Find what control and

.. \1

in the replace with control

--
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

jerem said:
I want to write a macro in Word that will find the filename (generated by a
document management system - Imanage) that resides on every page in the
Footer and then delete them. I can write a macro that deletes all
footers,
but I do not want the page numbering in the footers to be disturbed and
for
the life of me I cannot find sample code which represents FileName only.
And
copying the field code which represents the file name in Word does not
work
because the find will not look in the Footers.

Also, any way of doing a search in VB that will locate the end of every
sentence in a Word document. Want to write a macro that will find a
period
and one space and replace it with a period and two spaces (just doing a
regular find and replace in Word pulls up abbreviations in midsentencea
which
is not the desired result).
 
J

jerem

I went to your link regarding two spaces after sentences. Personally,
doesn't matter to me whether one uses one space or two spaces after
sentences, however, in the legal arena they are quite particular about their
documents. They require two spaces after a sentence and when you run scans
on documents, they come over as sentences with one space after the period.
Every one of these sentences has to be searched out and two spaces placed
after them. Thanks for the feedback.
 
D

Doug Robbins - Word MVP

re the second item, the seach string was

[period][space]([A-Z]{1,})

--
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

jerem said:
The problem is that there are varying operators who create the documents I
need to manipulate and it is a matter of individual preference as to
whether
the page numbers will appear before the document identifier (doc #) or
after.
But thanks for your response.

The second issue of replacing a period and one space with a period and two
spaces: I tried the ([A-Z]{1,}) in the Find with wildcards and the \1 in
the
Replace -- very wierd, don't know what this is finding and replacing but
the
first time I ran it it did 125 replaces, but of what I don't know. Could
not
recognize any change in the document at all. Ran the find and replace
again
in the same document and second time around made 65 replaces but same
issue.
Don't know what it is changing. There is no visible difference from the
original document to the replacement document. What I did find works is
if I
run a Macro I created which is called sticky spaces (replaces all titles
[i.e., Mr. and a space, Dr. and a space, Mrs. and a space , etc.] with Mr.
and a hard space, Dr. and a hard space, etc.). Once that macro completes
and
it eliminates all titles with a period and a space with a period and a
hard
space, I look for (.^32) a period and one space (^32 is the character code
for a space only) and I replace it with a period and 2 spaces. This, so
far,
is the only method that I've come up with to globally replace a period and
one space at the end of sentences with a period and two spaces, however, I
am
then left to look for abbreviations in midsentence to revert them back to
a
period and one space.

But again, thanks for trying to help.
Assuming that the field that contains the filename is the first field in
the
footer, you should be able to do it with:

Dim i As Long
With ActiveDocument
For i = 1 To .Sections.Count

.Sections(i).Footers(wdHeaderFooterFirstPage).Range.Fields(1).Delete

.Sections(i).Footers(wdHeaderFooterPrimary).Range.Fields(1).Delete
Next i
End With

For the second item, you should be able to do it using a Wildcard
Edit>Replace with

.. ([A-Z]{1,})

in the Find what control and

.. \1

in the replace with control

--
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

jerem said:
I want to write a macro in Word that will find the filename (generated
by a
document management system - Imanage) that resides on every page in the
Footer and then delete them. I can write a macro that deletes all
footers,
but I do not want the page numbering in the footers to be disturbed and
for
the life of me I cannot find sample code which represents FileName
only.
And
copying the field code which represents the file name in Word does not
work
because the find will not look in the Footers.

Also, any way of doing a search in VB that will locate the end of every
sentence in a Word document. Want to write a macro that will find a
period
and one space and replace it with a period and two spaces (just doing a
regular find and replace in Word pulls up abbreviations in midsentencea
which
is not the desired result).
 
R

Russ

And Jerem said:
"And copying the field code which represents the file name in Word does not
work because the find will not look in the Footers."

Helmut picked up on that quote and gave you a macro that will look for file
name fields in footers. What about it doesn't work? Aren't you talking about
Word fields that can look like the next line when you toggle them with the
keys ALT/F9?
{ FILENAME }

Maybe you need to replace his Stop line with MsgBox "I found a file name
field" so that you can see that it is finding all those kinds of fields.
 
J

jerem

Russ said:
And Jerem said:
"And copying the field code which represents the file name in Word does not
work because the find will not look in the Footers."

Helmut picked up on that quote and gave you a macro that will look for file
name fields in footers. What about it doesn't work? Aren't you talking about
Word fields that can look like the next line when you toggle them with the
keys ALT/F9?
{ FILENAME }

Maybe you need to replace his Stop line with MsgBox "I found a file name
field" so that you can see that it is finding all those kinds of fields.


--
Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Sorry took so long to respond - No, I replaced the Stop with the
 
J

jerem

Yes, you're right. I didn't comment on the second link because I didn't
notice it at first, however, it is a great website. I copied the code for
the two spaces issue, however, again - it's hard getting around the
abbreviations in midsentence so what I've done is tacked on some code to look
out for some abbreviations (a.m., p.m., Washington D.C., etc.) to bypass.

The other remedy was to select it in the Spellcheck feature, however, that's
interspersed with the spell check which makes it very distracting to look out
for both, not to mention the tediousness of it.

So yes, I love that website. Thanks.
 

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