Selection vs Range

C

Compass Rose

Working in Word 2000 and being inexperienced in VBA, I have recorded/written
a macro to perform some file tasks. I am not getting the results I am looking
for. I hope someone can help.

I have read about the suggestion of SETting a Document variable when opening
and working with several documents at the same time. I am not sure of the
syntax to define the 2 variables to make it work the way I want.

When the Unlink Fields is executed, it unlinks the fields in both open
documents, whereas I just want it to unlink the fields in the oDoc_New
document. The same is true of the Close. I want it to only close the oDoc_New
document.

I have been reading on this message board about the inadvisability of using
the Selection method over the Range method. My code is posted below and I
would appreciate some sample code suggestions of how to accomplish the tasks
that are currently using Selection, using Range instead.

The bookmark "NextBusiDay" contains fields that calculate a future date
using macropod's excellent work. The code is as follows:

Dim strNextDay As String, strTodayFileName As String, strNextDayFileName _
As String
Dim oDoc As Document, oDoc_New As Document
Application.ScreenUpdating = False
Set oDoc = ActiveDocument
With oDoc
.Save
strTodayFileName = oDoc.FullName
End With
Selection.WholeStory
Selection.Font.Hidden = False
Selection.GoTo What:=wdGoToBookmark, Name:="NextBusiDay"
With oDoc.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.Fields.Update
Selection.Copy
strNextDay = Selection.Text
Selection.Font.Hidden = True
strNextDayFileName = Left(strTodayFileName, Len(strTodayFileName) - 17) _
& strNextDay & ".doc"
oDoc.SaveAs FileName:=strNextDayFileName
Set oDoc_New = Documents.Open(strTodayFileName)
Selection.WholeStory
Selection.Fields.Unlink
Documents.Save
Documents.Close
Application.ScreenUpdating = True
Set oDoc_New = Nothing
Set oDoc = Nothing

I was also wondering what is the difference between hidden text and a hidden
bookmark? Could I use the hidden bookmark instead of the hidden text in this
case?

TIA
David
 
G

Graham Mayor

I think the following achieves your intention - the fields are fixed in the
current document and a new document is created containing the fields and
bookmark, which presumably will be used the next time the macro is run?
Obviously I don't know your path structure so I have left that pretty much
alone. Neither am I sure of the point of hiding and unhiding or sorting the
bookmarks, but I have left it available

Dim strNextDay As Range
Dim strTodayFileName As String
Dim strNextDayFileName As String
Dim oDoc As Document
Dim oDoc_New As Document
Application.ScreenUpdating = False
Set oDoc = ActiveDocument
With oDoc
.Save
strTodayFileName = .FullName
'.Range.Font.Hidden = False
'With .Bookmarks
' .DefaultSorting = wdSortByName
' .ShowHidden = True
'End With
Set strNextDay = .Bookmarks("NextBusiDay").Range
.Fields.Update
'.Bookmarks.ShowHidden = True
'strNextDayFileName = Left(strTodayFileName, Len(strTodayFileName) - 17)
_
& strNextDay & ".doc"
'.SaveAs FileName:=strNextDayFileName
.SaveAs FileName:=strNextDay.Text & ".doc"
.Close wdSaveChanges
End With
Set oDoc_New = Documents.Open(strTodayFileName)
oDoc_New.Fields.Unlink
oDoc_New.Close wdSaveChanges
Application.ScreenUpdating = True
Set oDoc_New = Nothing
Set oDoc = Nothing


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
C

Compass Rose

Thank you, Graham.

The statements that sort and unhide the bookmark are remnants of recording a
macro to go to the bookmark "NextBusiDay", which I then select, copy and then
assign Selection.Text to the variable strNextDay. (The bookmark "NextBusiDay"
contains fields that calculate a future date using macropod's excellent work,
but there is no need for it to be visible, so it's hidden text.) My method
worked, but I suspected there was a much easier way to accomplish it, hence
this post.

If the text of the bookmark NextBusiDay is hidden, will the statement

Set strNextDay = .Bookmarks("NextBusiDay").Range

work? From what I've read, it has to be unhidden first.

Thanks,
David
 
G

Graham Mayor

It only has to be unhidden if you need to select it. Try running the macro
;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
C

Compass Rose

Hi Graham
I've worked out the hidden issues, but I've run into a problem with the
macro. When the following line is executed,
With oDoc
'
'
.Close wdSaveChanges
the document closes, it disappears from the project explorer window, the
module coding window closes and none of the statements that follow are
executed. Hmmm.

How do I fix that?

David
 
G

Graham Mayor

If the macro is stored in the normal template (or the template from which
the initial document is created) then the macro runs to completion and
closes both documents. You should end up with two files - the old one in
which the field is unlinked and the new one in which it isn't.
I have not tested the code in Word 2000, but I don't think I have inserted
any code that is incompatible with that version. If you want one or both of
the documents to remain open, remove or disable the .close statements.

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