none
How to create a macro in word using vb.net RRS feed

  • Question

  • Hello all

    I want to avoid copy paste content and printing from my word document, I have tried with word protection with 'readonly' locking but, still I can able to copy and paste my word file content.
    So I go for word macro.

    Now I know how to write a macro manually but I want to create it using vb.net and call it.

     How can I write macro using VB.NET for a given word file ?

    Basically I need to develop program in vb.net where user can browse his word file and I can disable its copy paste facility by using macro.

    Wednesday, May 7, 2014 7:31 AM

Answers

  • Thanks for reply Paul

        I have found other way to accomplish my task, I have protect my document for 'Edit restriction for filling in forms' and it will disable copy/paste option of my document.

    WOW.. this is really great features of word.

    Thanks for your kind co-operation

    I have resolved issue.

    Here is my code

    //Object of missing "null value"  
                object oMissing = System.Reflection.Missing.Value;
                //Objects of false and true  
                object oTrue = true;
                object oFalse = false;
                object password = "pass";
                object oPath = textBox1.Text;//@"E:\Prasad\R_D\Disable copy paste in word\4168.docx"; //path to save newly created file  
                object oSaveChanges = true;
                //Creating objects of word and document  
                word.Application objApp = new word.Application();
                word.Document objDoc = new word.Document();
                word.Documents objDocs;
                objDocs = objApp.Documents;
                objDoc = objDocs.Open(ref oPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                foreach (word.Section oSec in objDoc.Sections)
                {
                    oSec.ProtectedForForms = true;
                    oSec.ProtectedForForms = true;
                    oSec.ProtectedForForms = true;
                }
                objDoc.Protect(word.WdProtectionType.wdAllowOnlyFormFields, ref oMissing, ref  password, ref oMissing, ref oMissing);
                objDoc.Save();
                //Dispose objects  
                objDoc.Close(ref oSaveChanges, ref oMissing, ref oMissing);
                objApp.Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                objDoc = null;
                objApp = null;
                MessageBox.Show("Docuemnt Locked successfully");
    its in C#
    • Marked as answer by koolprasadd Wednesday, May 7, 2014 2:08 PM
    Wednesday, May 7, 2014 2:08 PM

All replies

  • You cannot prevent someone copying the content of a Word document. Word simply does not offer than kind of protection. If a user can open a document, they can copy it. All anyone needs to do to subvert your macro code is to disable macros. And, if you put the code in a doc or docm file, all they need do is save it as a docx file to permanently delete your macro code.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 7, 2014 7:42 AM
  • Thanks for reply Paul

          I have macro code with me, which works fine if I copy it to any word document

    just I want to add it dynamically to word file using vb.net

    here is my Macro code

    Sub EditCopy()
    MsgBox “Forbidden”, vbExclamation
    End Sub
    
    Sub EditPaste()
    MsgBox “Forbidden”, vbExclamation
    End Sub
    Wednesday, May 7, 2014 9:03 AM
  • To add the code programmatically, you would need to have trusted access to the VBA object model. You cannot obtain that access programmatically - the user would have to set it before your code runs... In any event, as I've already said, your attempt to secure the document is easily defeated. Doing so deliberately is a trivial exercise (e.g. just choose 'no' at the macro prompt) and may even be the default setting for many users (macro security:high).

    If/when you resolve the trusted access issue, you could use VBA code like:

    Sub AddModule()
    Dim VBComp As VBComponent
    Set VBComp = ThisDocument.VBProject.VBComponents.Add(vbext_ct_StdModule)
    VBComp.Name = "NewModule"
    Application.Visible = True
    End Sub

    to create a new code module, followed by:

    Sub AddProcedure()
    Dim VBCodeMod As CodeModule, LineNum As Long
    Set VBCodeMod = ThisDocument.VBProject.VBComponents("NewModule").CodeModule
    With VBCodeMod
      LineNum = .CountOfLines + 1
      .InsertLines LineNum, vbCr & _
        "Sub EditCopy()" & vbCr & vbTab & _
        "MsgBox " & Chr(34) & "Forbidden" & Chr(34) & ", vbExclamation" & vbCr & _
        "End Sub" & vbCr _
        & vbCr & _
        "Sub EditPaste()" & vbCr & vbTab & _
        "MsgBox " & Chr(34) & "Forbidden" & Chr(34) & ", vbExclamation" & vbCr & _
        "End Sub"
    End With
    End Sub

    to add the macros to that code module.

    To use the above procedures, you need to set a reference to Microsoft Visual Basic For Applications Extensibility.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 7, 2014 9:29 AM
  • Thanks for reply Paul

        I have found other way to accomplish my task, I have protect my document for 'Edit restriction for filling in forms' and it will disable copy/paste option of my document.

    WOW.. this is really great features of word.

    Thanks for your kind co-operation

    I have resolved issue.

    Here is my code

    //Object of missing "null value"  
                object oMissing = System.Reflection.Missing.Value;
                //Objects of false and true  
                object oTrue = true;
                object oFalse = false;
                object password = "pass";
                object oPath = textBox1.Text;//@"E:\Prasad\R_D\Disable copy paste in word\4168.docx"; //path to save newly created file  
                object oSaveChanges = true;
                //Creating objects of word and document  
                word.Application objApp = new word.Application();
                word.Document objDoc = new word.Document();
                word.Documents objDocs;
                objDocs = objApp.Documents;
                objDoc = objDocs.Open(ref oPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                foreach (word.Section oSec in objDoc.Sections)
                {
                    oSec.ProtectedForForms = true;
                    oSec.ProtectedForForms = true;
                    oSec.ProtectedForForms = true;
                }
                objDoc.Protect(word.WdProtectionType.wdAllowOnlyFormFields, ref oMissing, ref  password, ref oMissing, ref oMissing);
                objDoc.Save();
                //Dispose objects  
                objDoc.Close(ref oSaveChanges, ref oMissing, ref oMissing);
                objApp.Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                objDoc = null;
                objApp = null;
                MessageBox.Show("Docuemnt Locked successfully");
    its in C#
    • Marked as answer by koolprasadd Wednesday, May 7, 2014 2:08 PM
    Wednesday, May 7, 2014 2:08 PM
  • Hi Koolprasad

    If you want to prevent copying a document I recommend you look into using Information Rights Management (IRM) in combination with Active Directory services. See

    http://office.microsoft.com/en-001/word-help/about-information-rights-management-HP006220859.aspx
    http://technet.microsoft.com/en-us/library/cc179103(v=office.15).aspx

    As Paul says, trying to use a macro is simply not going to be secure...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 7, 2014 2:11 PM
    Moderator
  • That process, while it prevents someone selecting content, doesn't prevent them from making a copy of the entire document (e.g. via Windows Explorer), or linking to it via another document using an INCLUDETEXT field and copying the content from the link. It is also very easy to break this protection.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 7, 2014 10:18 PM