MS Office Forum / Word / Programming / July 2007
VBA code to search for FileName in Word
|
|
Thread rating:  |
jerem - 14 Jul 2007 04:44 GMT 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).
Helmut Weber - 14 Jul 2007 09:00 GMT 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...
 Signature Greetings from Bavaria, Germany
Helmut Weber, M.A. (linguistics), MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
jerem - 17 Jul 2007 02:34 GMT 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.
> Hi, > [quoted text clipped - 38 lines] > and the rules are always changing, > and influence the lists... Russ - 17 Jul 2007 06:19 GMT 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.
> 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. [quoted text clipped - 43 lines] >> and the rules are always changing, >> and influence the lists...
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
jerem - 20 Jul 2007 04:30 GMT > And Jerem said: > "And copying the field code which represents the file name in Word does not [quoted text clipped - 58 lines] > > >> oFld.Delete because that's really what I wanted it to do, but I got some kind of compilation or syntax error. Not to savvy with VB code, most of the code I use I let Word generate for me through "Record a Macro", that's why I could not get the search going through Word. There doesn't seem to be any way to search a document ID through the Find using wildcards, ASCII codes, etc. I haven't had time to look at this again, but will keep plugging along until I do get it to work. Thanks. Russ - 20 Jul 2007 09:12 GMT Jerem, See down below.
>> 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 [quoted text clipped - 4 lines] >> 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.
>>>> Sorry took so long to respond - No, I replaced the Stop with the >>>> oFld.Delete because that's really what I wanted it to do, but I Try: oFld.Range.Delete
>>>> got some kind of compilation or syntax error. Not to savvy with VB code, >>>> most of the code I use I let Word generate for me through "Record a Macro", >>>> that's why I could not get the search going through Word. There doesn't >>>> seem to be any way to search a document ID through the Find using >>>> wildcards, ASCII codes, etc. I haven't had time to look at this again, but >>>> will keep plugging along until I do get it to work. Thanks.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Doug Robbins - Word MVP - 14 Jul 2007 09:11 GMT 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
 Signature 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
>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 [quoted text clipped - 15 lines] > which > is not the desired result). jerem - 17 Jul 2007 02:52 GMT 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: [quoted text clipped - 37 lines] > > which > > is not the desired result). Doug Robbins - Word MVP - 17 Jul 2007 05:57 GMT re the second item, the seach string was
[period][space]([A-Z]{1,})
 Signature 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
> 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 [quoted text clipped - 81 lines] >> > which >> > is not the desired result). Russ - 14 Jul 2007 09:18 GMT 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 [quoted text clipped - 9 lines] > 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
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 14 Jul 2007 09:26 GMT 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.
> 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 [quoted text clipped - 18 lines] > http://wordtips.vitalnews.com/Pages/T1497_An_Automatic_Two_Spaces_After_a_Pe > riod.html
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
jerem - 17 Jul 2007 02:56 GMT 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.
> 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. [quoted text clipped - 21 lines] > > http://wordtips.vitalnews.com/Pages/T1497_An_Automatic_Two_Spaces_After_a_Pe > > riod.html Russ - 17 Jul 2007 06:00 GMT Jerem, You didn't comment on the second link, which I repeat here. Didn't you find that useful after reading it all? <http://wordtips.vitalnews.com/Pages/T1497_An_Automatic_Two_Spaces_After_a_P eriod.html>
> 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 [quoted text clipped - 31 lines] >>> http://wordtips.vitalnews.com/Pages/T1497_An_Automatic_Two_Spaces_After_a_Pe >>> riod.html
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
jerem - 20 Jul 2007 04:36 GMT 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.
> Jerem, > You didn't comment on the second link, which I repeat here. Didn't you find [quoted text clipped - 37 lines] > >>> http://wordtips.vitalnews.com/Pages/T1497_An_Automatic_Two_Spaces_After_a_Pe > >>> riod.html
|
|
|