Help combining macros

chally3

Board Regular
Joined
Mar 22, 2006
Messages
160
Can Anyone help please, although this a word document I guess any VB expert can help.

I have a document with the following VB which simply copies & pastes the first document onto another page (different header).

Sub ADVICENOTE()
'
' ADVICENOTE Macro
' Macro recorded 22/03/2006 by XXXXXXXXX
'
Selection.WholeStory
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.InsertBreak Type:=wdPageBreak
Selection.PasteAndFormat (wdPasteDefault)
End Sub


The actual document I have changed so only certain fields can be filled in to make it idiot proof. As the document is protected when I use this function, the copy/paste macro will not run.

I have found the following code which unprotects a locked document, then allows changes to be made then relocks it.

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If


What I would like to know is can both be combined into 1 piece of VB code I can used as 1 macro, I have no idea. Please help!!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
The protection code you supplied was almost there. It checks if the document isn't protected, and if it isn't it protects it.
You need to check if the document is protected, unprotect it, do your stuff and protect it afterwards (whether it was or wasn't protected in the first place).

Code:
Sub ADVICENOTE()
'
' ADVICENOTE Macro
' Macro recorded 22/03/2006 by XXXXXXXXX
'

    ' Check if document protected.  If it is then remove protection.
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If
    
    'Your code
    Selection.WholeStory
    Selection.Copy
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.TypeParagraph
    Selection.InsertBreak Type:=wdPageBreak
    Selection.PasteAndFormat (wdPasteDefault)
    
    'Protect document again.
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
        NoReset:=True
    
End Sub

Hope it helps :)
 
Upvote 0
Hi

I know this is an old post but I am trying to do something very similar and thought it would be better to re-open this than start a new thread.

I have tried out Delmar D'Percy suggestion and it works great.

I was wondering though if when it pastes into a new document if you could have the protection OFF.

To explain:
My work has a Form that is protected. They want people to complete it, press the macro button, it copies and pastes it into a new document. This new document should not be protected that way it allows them to select all so that they can then copy and paste into another program that we are using. They then close the new document without saving.

So like I said basically I am wondering if we can turn the protection OFF when it pastes into the new document.

I hope I have explained myself clearly enough.

Thanks in advance for reading this post, further thanks if you can help.:)
 
Upvote 0
Sorry, just me again.

On thinking about this what I need to do is get the information to copy to the clipboard rather than copying and pasting it to a new document.

Can anyone help with a macro to copy to clipboard?

Sorry to be a nuisance.

And again thanks if you have read this and thanks if you can help.
 
Upvote 0
I found this

Public Sub copyToClipboard()
Dim MyData As DataObject
Dim cAddress As String

Set MyData = New DataObject
Let cAddress = compileAddress(True)
MyData.SetText cAddress
MyData.PutInClipboard
End Sub


but unfortunately I dont know how to change the code I have obtained from the previous post here:

Sub Copyandpaste()
'
' Copyandpaste Macro
' Macro recorded on 26/01/2009 by Roberta Key
'
' Check if document protected. If it is then remove protection.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

Selection.WholeStory
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.Paste
'Protect document again.
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True

End Sub

Can anyone help my combine these two macros together by removing copying to a new document and adding in the copy to clipboard?​

I am really struggling with this and your help would be much appreciated.​
 
Upvote 0
Re: Help combining macros ~ Fixed for 1Roberta1

I don't believe it.

All I need to do was REMOVE

Documents.Add DocumentType:=wdNewBlankDocument
Selection.Paste

from the code I had.

Sorry for all the posts, I did try to edit but I couldn't see the option to edit my previous posts.

Hopefully though someone else might find this useful.

Thanks
 
Upvote 0

Forum statistics

Threads
1,225,382
Messages
6,184,640
Members
453,248
Latest member
levi_15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top