Conflict between two macro's on a protected document

S

Steve

Hi,

I have designed a form with a seq number running with an autonew macro. The
code has been entered to unprotect>number change>protect. I now wish to add
an address field linked to my outlook address book. Again the code has been
entered in the correct place to protect and unprotect. There are both
succesful on a seperate document but fail together. Is it possible to have
them both work on the same document? The codes i am using are as follows:

Private Sub Document_New()

End Sub
Sub AutoNew()

Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.Txt", "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "100#")
ActiveDocument.SaveAs FileName:="D:\My Documents\Work\Quotes Past
Month\Quotation ID_" & Format(Order, "100#")
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

-------------------------------------------------------

Public Sub InsertAddressFromOutlook()
Dim strCode As String, strAddress As String
Dim iDoubleCR As Integer

'Set up the formatting codes in strCode
strCode = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr & _
"<PR_COMPANY_NAME>" & vbCr & _
"<PR_POSTAL_ADDRESS>" & vbCr

'Display the 'Select Name' dialog, which lets the user choose
'a name from their Outlook address book
strAddress = Application.GetAddress(AddressProperties:=strCode, _
UseAutoText:=False, DisplaySelectDialog:=1, _
RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
'If user cancelled out of 'Select Name' dialog, quit
If strAddress = "" Then Exit Sub

'Eliminate blank paragraphs by looking for two carriage returns in a row
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Do While iDoubleCR <> 0
strAddress = Left(strAddress, iDoubleCR - 1) & _
Mid(strAddress, iDoubleCR + 1)
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Loop

'Strip off final paragraph mark
strAddress = Left(strAddress, Len(strAddress) - 1)
'Insert the modified address at the current insertion point
ActiveDocument.Unprotect
Selection.Range.Text = strAddress
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True

End Sub

Many thanks in advanced.

Steve
 
J

Jean-Guy Marcil

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

I have designed a form with a seq number running with an autonew
macro. The code has been entered to unprotect>number change>protect.
I now wish to add an address field linked to my outlook address book.
Again the code has been entered in the correct place to protect and
unprotect. There are both succesful on a seperate document but fail
together. Is it possible to have them both work on the same document?

Together? Do you mean you want both of them to run together, one after the
other when creating a document?
How do they fail? This is too vague.

<snip>

AutoNew and Document_New are the same as far as automatically executing when
creating a new document from a template. Use only one of them, preferably
Document_New as the other is deprecated.

Try something like this (not tested, just quickly rearranging your macros in
a way that should work):
By the way, you have a line that states:
'Insert the modified address at the current insertion point
Since you are invoking these macros whent the document opens, the insertion
point will be at the begining of the document. Is that what you want?
'_______________________________________
Private Sub Document_New()

ActiveDocument.Unprotect
UpdateAutoNumber
InsertAddressFromOutlook
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

End Sub
'_______________________________________

'_______________________________________
Sub UpdateAutoNumber()

Order = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "Order")

If Order = "" Then
Order = 1
Else
Order = Order + 1
End If

System.PrivateProfileString("C:\Settings.Txt", "MacroSettings", _
"Order") = Order

ActiveDocument.Bookmarks("Order").Range.InsertBefore Format(Order, "100#")
ActiveDocument.SaveAs FileName:="D:\My Documents\Work\Quotes Past
Month\Quotation ID_" & Format(Order, "100#")

End Sub
'_______________________________________

'_______________________________________
Public Sub InsertAddressFromOutlook()
Dim strCode As String, strAddress As String
Dim iDoubleCR As Integer

'Set up the formatting codes in strCode
strCode = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr & _
"<PR_COMPANY_NAME>" & vbCr & _
"<PR_POSTAL_ADDRESS>" & vbCr

'Display the 'Select Name' dialog, which lets the user choose
'a name from their Outlook address book
strAddress = Application.GetAddress(AddressProperties:=strCode, _
UseAutoText:=False, DisplaySelectDialog:=1, _
RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
'If user cancelled out of 'Select Name' dialog, quit
If strAddress = "" Then Exit Sub

'Eliminate blank paragraphs by looking for two carriage returns in a row
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Do While iDoubleCR <> 0
strAddress = Left(strAddress, iDoubleCR - 1) & _
Mid(strAddress, iDoubleCR + 1)
iDoubleCR = InStr(strAddress, vbCr & vbCr)
Loop

'Strip off final paragraph mark
strAddress = Left(strAddress, Len(strAddress) - 1)
'Insert the modified address at the current insertion point
Selection.Range.Text = strAddress

End Sub
'_______________________________________

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

Steve

Hi Jean,

Thanks for your response.

Excuse my lack of knowledge it's something i'm only just getting my head into.

I've solved my problem before your post and have a document that opens with
seq number and i can access my address book with the document protected at
all times.

I have three macros running on this document, taborder, autonew and
document_new, are you saying put the autonew and document_new on the same
macro?

Also yes you are right, the insertion point should be at the end and have
been clicking save for now?

I'm guessing there are better ways to do what i'm doing?

Thanks

Steve
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?U3RldmU=?=,
I have three macros running on this document, taborder, autonew and
document_new, are you saying put the autonew and document_new on the same
macro?
No, they are not. document_new is an event that must be in the ThisDocument
module, while AutoNew is a "real macro". Both *fire* when a new document is
created, but at different times. I can't recall which fires first. And one
fires before Word has done certain things, while the other fires after. So
often it doesn't matter which you use, but under certain circumstances it
can make a difference. Generally, one wouldn't use both in the same project.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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