create a new word document based on a specific model (template)

  • Hi all!


    I am trying to create a new word document with the name of the active cell, and based on a specific model (c:\papel carta.dotx).


    Can someone help me with a macro for this?


    Thank you very much in advance!!

  • Re: create a new word document based on a specific model (template)


    (Code snippet - needs revising)

    Code
    If Dir("c:\papel carta.dotx") = vbNullString Then
          Err.Raise 30010
       End If
       
       Set wd = New Word.Application
       
       With wd
          .Documents.Add Template:="c:\papel carta.dotx", Visible:=True
          ...
          ...


    The only way to name the document is when saving - just use ActiveCell.Text as the file name.

  • Re: create a new word document based on a specific model (template)



    Hi Hfalstom,


    Please see if the attachment helps

  • Re: create a new word document based on a specific model (template)


    Quote from cytop;726913

    (Code snippet - needs revising)

    Code
    If Dir("c:\papel carta.dotx") = vbNullString Then
          Err.Raise 30010
       End If
       
       Set wd = New Word.Application
       
       With wd
          .Documents.Add Template:="c:\papel carta.dotx", Visible:=True
          ...
          ...


    The only way to name the document is when saving - just use ActiveCell.Text as the file name.


    Hi Cytop,


    Thank you very much for your help!!!!


    I could not make you code work, it comes back with an error about the object being not defined by user.


    I am very new to this kind of code, so I have no idea on how to correct this.


    I am "old school", I know only (and only a few) commands from the original Basic.


    I will keep trying, if you can help me a little more I will appreciate!


    Thanks and regards,

  • Re: create a new word document based on a specific model (template)


    Quote from gn00588950;726916

    Hi Hfalstom,


    Please see if the attachment helps


    Hi GN!


    Thank you very much for your help.


    I tried to run your code, but I get an error message when imputing the file path. (invalid reference). This file path is where I want to save the file?


    I will keep trying to fix this.


    The save path will be the same as the template, does it help?


    If you can give me a hint of a little more help, I will appreciate a lot!


    Once again, thank you very much and best regards,

  • Re: create a new word document based on a specific model (template)


    Quote from gn00588950;726916

    Hi Hfalstom,


    Please see if the attachment helps



    Tony,


    I got it to work, by assigning a cell to "path", but it does not save any file...


    Any tips?


    Thank you again and best regards,


    Henrique

  • Re: create a new word document based on a specific model (template)



    Hi Henrique,


    Sorry I've missed the steps.


    1. Place your Template in C Drive. (Which the whole path should be C:\papel carta.dotx)


    If your template is in other folder, change the path with the following line:


    Code
    Sub CreateFromTemplate()
    ...
    ...
    If IsFileOpen("C:\papel carta.dotx") Then Exit Sub 'Change Your Template Path If Needed
    Set ObjDoc = ObjWord.Documents.Open("C:\papel carta.dotx") 'Change Your Template Path If Needed
    ...
    ...
    End Sub


    2. Input the new Filename you want in Column A
    The Filename must contains folder path and file type extension: E.g.: C:\Document1.docx


    If more than 1 file to be generated, format should be like this:
    A2 = C:\Document1.docx
    A3 = C:\Document2.docx
    A4 = D:\TestABC.docx
    A5 = E:\ABCDocument.docx
    ...
    A(N) = C:\DocumentN.docx


    3. Run the code, an application inputbox will be prompt. Select all inputted Filename (which should be A2:A(N)) and click OK. And file should be generated accordingly.

    Best Regards,


    Tony


    [SIZE=1]A Peaceful Mine Is A Blessing[/SIZE]

  • Re: create a new word document based on a specific model (template)



    Tony, thank you very much!


    I will try it again later!


    The word template was in drive c:, and the cell had the right path and filename, but it did not work (no error message, but nothing was created). I even made a full search for new files, no results....


    I let you know.


    Once again, thank you very much for your help.


    Best regards,


    Herique

  • Re: create a new word document based on a specific model (template)


    Hi Herique,


    May I know is anything returned in "Result Column"? It should either return "File Already Exist." or "File Created."


    If you can see this in Result column, you may need to debug it In Step-By-Step Mode (Run with F8)


    First of all, change the following and run with F8.


    Code
    ObjWord.Visible = True


    This will let you able to see if the code has opened MS Word or not.


    If it dose open MS Word, then check this line to see if it saved the document


    Code
    ObjDoc.SaveAs FileName:=Cell.Value, FileFormat:=12


    If the above line cannot save any files, try the following:

    Code
    ObjWord.ActiveDocument.SaveAs FileName:=Cell.Value, FileFormat:=12
    
    
    'FileFormat:=12 means save as docx. If you want .doc, you can change to FileFormat:=8 (Don't forget to change your File Name as well. I.e.: C:\Test.doc)


    Hope this help.

    Best Regards,


    Tony


    [SIZE=1]A Peaceful Mine Is A Blessing[/SIZE]

  • Re: create a new word document based on a specific model (template)




    Tony, it did not open Word.

  • Re: create a new word document based on a specific model (template)




    Tony,


    No results are shown in the result column.


    I ran the code with F8. It goes line by line to "If IsFileOpen("C:\papel carta.dotx") Then Exit Sub 'Change Your Template Path If Needed", then it jumps to "Function IsFileOpen(FileName As String)", goes line by line and comes back to "If IsFileOpen...." and then it exits.


    Word is closed, the path is OK, but nothing happens.


    Do I have to open the template first?

  • Re: create a new word document based on a specific model (template)


    Opps!


    I'm sorry!


    I've forgot to remine you not to open the template


    Template must be closed before you run the code.


    Please replace this: (Or you can remove this line & the Function IsFileOpen if you don't mine the Template is open when you creating document.)


    Code
    If IsFileOpen("C:\papel carta.dotx") Then Exit Sub 'Change Your Template Path If Needed


    To this as a reminder...(My apologies again :(!) [If you removed the above line, no need to add this]:


    Code
    If IsFileOpen("C:\papel carta.dotx") Then 'Change Your Template Path If Needed
    MsgBox ("Please Close The Template Before Execution.")
    Exit Sub
    End If


    Now you should able to create the document.


    Don't forget to change back the following:


    Code
    ObjWord.Visible = False


    The will helps the performance.

    Best Regards,


    Tony


    [SIZE=1]A Peaceful Mine Is A Blessing[/SIZE]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!