Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Word 2003 - one too many field updates

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick Fluck - 16 Jun 2007 22:59 GMT
I am a novice (eejit..) macro / VBA user.

I have a template document in which I use fillin fields to prompt for
references, name and address lines and header lines.

I want to use a simple (taken me about four hours cutting and pasting and
testing and restarting so far!!) VBA routine to prompt for and set custom
document properties.  (Code to date set out below) and I also want to be
able to check an existing document when I reopen it to see if I want to
update the custom document properties.

This creates a problem though as the updating of document properties when
either the original or the update code causes the fillin fields to run,
twice, once before the prompt for the custom doc properties and once after
them (and again at print time!)

Is there a way to force the custom document properties to update BEFORE and
without affecting fillin fields?

There is one more silly quirk with what I have done to date and this is
that, as the docproperty fields in my template don't get updated until after
the initial display of an "Error doc property doesn't exist"
in BOLD the docproperty items end up bold when they are entered as they
pick up the formatting of the displayed field error.

See I told you I was an eejit!

Code:

Sub AutoNew()

Dim ClientRef As String
Dim FeeRef As String
Dim SecRef As String
Dim oStory As Range

ClientRef = InputBox("Enter Client reference (Letter and 6 numbers)",
"ClientRef", "")
FeeRef = InputBox("Enter Fee-Earner initials", "FeeRef", "")
SecRef = InputBox("Enter Secretary's initials", "SecRef", "")
'ActiveDocument.Variables("ClientName").Value = ClientName
ActiveDocument.CustomDocumentProperties.Add Name:="ClientRef",
LinkToContent:=False, Value:=ClientRef, Type:=msoPropertyTypeString
ActiveDocument.CustomDocumentProperties.Add Name:="FeeRef",
LinkToContent:=False, Value:=FeeRef, Type:=msoPropertyTypeString
ActiveDocument.CustomDocumentProperties.Add Name:="SecRef",
LinkToContent:=False, Value:=SecRef, Type:=msoPropertyTypeString
'ActiveDocument.BuiltInDocumentProperties("Author").Value = Author
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType < wdMainTextStory Then
   While Not (oStory.NextStoryRange Is Nothing)
   Set oStory = oStory.NextStoryRange
   oStory.Fields.Update
   Wend
   End If
   Next oStory
   Set oStory = Nothing
   End Sub

Sub AutoOpen()

Dim ClientRef As String
Dim FeeRef As String
Dim SecRef As String
Dim oStory As Range

On Error GoTo ExitSub
Update = MsgBox("Update Document Information? NB has no effect unless custom
document properties have already been set", vbYesNo)
If Update = vbYes Then
   ClientRef = InputBox("Enter Client reference(Letter and 6 numbers)",
"ClientRef", "")
   FeeRef = InputBox("Enter Fee-Earner initials", "FeeRef", "")
   SecRef = InputBox("Enter Secretary's initials", "SecRef", "")
   ActiveDocument.CustomDocumentProperties("ClientRef").Value = ClientRef
   ActiveDocument.CustomDocumentProperties("FeeRef").Value = FeeRef
   ActiveDocument.CustomDocumentProperties("SecRef").Value = SecRef

   'ActiveDocument.Variables("PolicyNum").Value = PolicyNum
   'ActiveDocument.BuiltInDocumentProperties("Author").Value = Author

   For Each oStory In ActiveDocument.StoryRanges
   oStory.Fields.Update
   If oStory.StoryType < wdMainTextStory Then
     While Not (oStory.NextStoryRange Is Nothing)
       Set oStory = oStory.NextStoryRange
       oStory.Fields.Update
     Wend
   End If
Next oStory
Set oStory = Nothing
End If
ExitSub:
End Sub

Any help would be much appreciated and I will not be in the least offended
if everyone wants to tell me how stupidly I have set about this project!

thanks

Nick Fluck
Doug Robbins - Word MVP - 16 Jun 2007 23:24 GMT
I would suggest that instead of your InputBoxes and Fillin fields, you use a
userform.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

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 am a novice (eejit..) macro / VBA user.
>
[quoted text clipped - 98 lines]
>
> Nick Fluck
Nick Fluck - 17 Jun 2007 12:34 GMT
Thanks for this, Doug.

I have had a play with a simple userform (thank you for this - I can see a
whole new sea of finger problems opening before me now!) but this reveals
another lack of knowledge which may be crucial!

I wanted to have a single text box for "address" details but although I can
make this multiline it doesn't seem to be possible to start a new line using
only the CR key but only by holding shift and CR.  This would cause my users
endless grief so I swapped the address lines for individual one-line text
boxes and this in turn means I now need to know how to get rid of unused
bookmarks and closing up the used ones to be on sequential lines

Thus

addr1
addr2
addr4
addr5

even though addr3 is blank.

Any thoughts - and thanks very much once again for your blindingly swift
earlier response.

regards

Nick Fluck
>I would suggest that instead of your InputBoxes and Fillin fields, you use
>a userform.
[quoted text clipped - 106 lines]
>>
>> Nick Fluck
Doug Robbins - Word MVP - 17 Jun 2007 20:45 GMT
Dim address as String
address = ""
If addr1 <> "" then
   address = address & addr1.text
End if
If addr2 <> "" then
   address = address & vbCr & addr2.text
End if
If addr3 <> "" then
   address = address & vbCr & addr3.text
End if

etc

ActiveDocument.Bookmarks("Address").Range.InsertBefore address

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

> Thanks for this, Doug.
>
[quoted text clipped - 136 lines]
>>>
>>> Nick Fluck
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.