none
C# Create word document from template RRS feed

  • Question

  • I need to add custom template to my document but my template has a password.I try to use Documents.Open() but it return "Command failed".Pls help me
    Monday, May 2, 2011 5:44 AM

Answers

  • Yes, but are you able to open it as an end-user? Are you able to start Word and open the template (no macros)?
    Cindy Meister, VSTO/Word MVP
    Monday, May 2, 2011 10:19 AM
    Moderator

All replies

  • Please provide more information

    1. Version of Word involved

    2. The code you tried to use to "add custom template to my document"

    3. The error message you got with the code used for (2)

    4. The complete code you used for "Documents.Open", including parameters


    Cindy Meister, VSTO/Word MVP
    Monday, May 2, 2011 5:56 AM
    Moderator

  • Please provide more information

    1. Version of Word involved

    2. The code you tried to use to "add custom template to my document"

    3. The error message you got with the code used for (2)

    4. The complete code you used for "Documents.Open", including parameters


    Cindy Meister, VSTO/Word MVP

    I use Word 2010 and this is an error message http://thebighead.co.cc/file/2-5-2554%2013-00-46.png

     

     

          Word._Application oWord;
          Word._Document oDoc;
          oWord = new Word.Application();
          oWord.Visible = false;
          object fileName = @"G:\Project\Questionnaire\QN1.dotx";
          object missing = System.Reflection.Missing.Value;
          object passwd = "QuestionNaireProject";
          oDoc = oWord.Documents.Open(ref fileName, ref missing, ref missing,
    ref missing, ref passwd, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    

     

    Monday, May 2, 2011 6:06 AM
  • OK, I don't see anything here that has to do with a template - you're just opening a document with this code. So...

    Are you able to open this document in Word as an end-user, with the same password?

    If yes, try putting this code in a try...catch block to see if that gives you a more detailed error message.

    And also try recording opening the document in a macro, in Word. Then compare the parameters the recorded macro uses with those you use.


    Cindy Meister, VSTO/Word MVP
    Monday, May 2, 2011 6:10 AM
    Moderator
  • I try to open that template via macro and run it.It return error "Run-time error'4198' Command Failed".and i try to set wrong password to parameter and i run agian.It return error "can't open document.wrong password"
    this is macro code

    Sub QN()
    Dim wdApp As Object
     Dim wdDoc As Object
     Set wdApp = CreateObject("Word.application")
     Set wdDoc = wdApp.Documents.Open _
      (Filename:="G:\Project\Questionnaire\QN1.dotx", PasswordDocument:="QuestionNaireProject")
    
    ' put your code here for working with Word
    ' This is Word VBA code, not Excel code
    
     wdDoc.Close savechanges:=False
     Set wdDoc = Nothing
     wdApp.Quit
     Set wdApp = Nothing
    End Sub
    
    

     


    Monday, May 2, 2011 6:41 AM
  • Yes, but are you able to open it as an end-user? Are you able to start Word and open the template (no macros)?
    Cindy Meister, VSTO/Word MVP
    Monday, May 2, 2011 10:19 AM
    Moderator