none
Old 2003 code for 2007/2010 RRS feed

  • Question

  • I have 2003 code for a macro that runs a template.  However, the templates used to live nicely in c:\program files etc.  Now that Windows 7 and XP machines do things differently, I want to place these templates in the trusted location in application data, microsoft templates.

    My old 2003 code for a macro is to run a template.

     Documents.Add Template:= _
    "C:\Program Files\Microsoft Office\Templates\Admin\Letter.dot", _
    NewTemplate:=False, DocumentType:=0


    I want to place these templates in C:\Users\Username\AppData\Roaming\Microsoft\Templates

    I tried "%appdata%\%microsoft%\ but that is not working.

    Can anyone please help me with this code?

     Thanks for any help

    CLB


    Christine Garrett
    Thursday, October 20, 2011 3:25 PM

Answers

  • Hi Christine,

    You can solve this by using the Environ() Function retrieving the UserName profile path, with something like this:

    Environ("USERPROFILE") + "\AppData\Roaming\Microsoft\Templates"
    

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Thursday, October 20, 2011 10:53 PM
    Moderator

All replies

  • I am a novice at best. 

    My old 2003 code for a macro is to run a template.

     Documents.Add Template:= _
    "C:\Program Files\Microsoft Office\Templates\Admin\Letter.dot", _
    NewTemplate:=False, DocumentType:=0

    I want to place these templates in C:\Users\Username\AppData\Roaming\Microsoft\Templates

     

    What is the code for that?

    I tried %appdata%\%microsoft% and it doesn't work

    HELP

    thanks for any help


    Christine Garrett
    • Merged by Kee Poppy Monday, October 24, 2011 7:47 AM duplicate thread
    Thursday, October 20, 2011 6:27 PM
  • If this is VBA code, try this forum: http://social.msdn.microsoft.com/Forums/en-US/worddev/threads

    This forum is for the standalone .Net based versions.


    Armin
    Thursday, October 20, 2011 7:23 PM
  • What am I doing wrong in this forum??  I keep going into VBA general so where should this go???
    Christine Garrett

    Thursday, October 20, 2011 7:57 PM
  • Pardon?
    Armin
    Thursday, October 20, 2011 8:13 PM
  • Hi Christine,

    You can solve this by using the Environ() Function retrieving the UserName profile path, with something like this:

    Environ("USERPROFILE") + "\AppData\Roaming\Microsoft\Templates"
    

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Thursday, October 20, 2011 10:53 PM
    Moderator
  • Thanks so much.  I greatly appreciate folks like you.
    Christine Garrett
    Friday, October 21, 2011 2:29 AM
  • Hi Chrisitine,

    You are very welcome, glad to help! :)

     

    Cheers,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Friday, October 21, 2011 2:30 AM
    Moderator
  • Ok, so I am having trouble getting this code to work but it is probably because I have something wrong.

     

    This template is macro enabled because it is developed to create a letter.  That part I did a while ago and works when using the template manually.

     

    However, calling for the template (which should seem simple to most folks) is what I am having trouble with.  I am on Windows 7, Word 2007/2010.  My UAC is turned off and macros are enabled and trusted publishers as well.

    Here was the simple line that it took to create a new document based on the template in 2003 when trusted templates did not exist. 

    Documents.Add Template:= _
    "C:\Program Files\Microsoft Office\Templates\Admin\Letter.dot", _
    NewTemplate:=False, DocumentType:=0

    Now the templates live in the user template directory of

    User appdata\roaming\microsoft\templates

    The code I am trying is as follows: 

     Documents.Add Template:= _
            Environ("USERPROFILE") + "\AppData\Roaming\Microsoft\Templates\Admin\Letter.dot", _
            NewTemplate:=False, DocumentType:=0

    It pulls the following error with the + or the & symbol

    Runtime error 5151 word cannot read the file or the file is corrupt.  However, I can manually create a document based on this template.

    Very frustrating!

    Christine

     


    Christine Garrett
    Saturday, October 22, 2011 12:42 AM
  • Hi Christine,

    Just to make sure it returns a String for the UserProfile path, you might convert to a String, alltough I doubt that will resolve the problem:

      Documents.Add Template:= _
           CString(Environ("USERPROFILE")) + "\AppData\Roaming\Microsoft\Templates\Admin\Letter.dot", _
     NewTemplate:=False, DocumentType:=0
    

    Else check this thread as well:

    http://theether.net/kb/100120

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Saturday, October 22, 2011 1:25 AM
    Moderator
  • Yes, you are right that it does not resolve the problem, in fact throws a syntax error after dim cstring

    Thanks about the thread.  I had fallen into that information this afternoon but it does not instruct how to use the usertemplates directory instead it instructs on how to access the c: drive.  I don't want to do that.  I want the user templates.

     

    Thanks,

     

    Christine


    Christine Garrett
    Saturday, October 22, 2011 1:38 AM
  • Hi Christine,

     

    That code line above was incorrect, that must be CStr():

      Documents.Add Template:= _
           CStr(Environ("USERPROFILE")) + "\AppData\Roaming\Microsoft\Templates\Admin\Letter.dot", _
     NewTemplate:=False, DocumentType:=0
    
    


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Saturday, October 22, 2011 1:43 AM
    Moderator