Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Microsoft Word Macros and VBA Scripts

Posted on 2016-10-17
3
Medium Priority
?
112 Views
Last Modified: 2016-10-18
I have a Word macro that isn't working and I hope someone can help me with it.  It's probably something simple I am overlooking but I have been staring at it too long to see it.  

I have a Word 2013 doc.dotm file that has a customUI with iRibbonControl that launches several different custom macros.  I have all macros working except for one.  The macro needs to create a new Word document based on a custom template that is in a shared folder on the network.  There are multiple custom templates in the repository so the user needs the ability to select the correct custom template when launching the macro.  I am using the Documents.Add Template:="doc.dotx" Method to create the new document.  The method works perfectly if I specify the full path and document name within the quotes.  

But I need to pass a variable to the Documents.Add method so I tried the following code.  I can make the file selection dialog come up correctly but after selecting a file and clicking ok, VBA reports that the file couldn't be found and it is blank where the incorrect path would normally be referenced in the error message.  I'm only using MS Word VBA Editor and I can't step through and see the variable values since the code is launched by an iRibbonControl, not a standard macro.  

Can someone review my code and see if I'm missing something obvious?  Thanks in advance for your help!!

Full Disclosure:  I got the code snippet for the FileDialog function from http://analystcave.com/vba-application-filedialog-select-file/



' This line works perfectly.
' Documents.Add Template:="Path+doc.dotx"

'This section prompts for a file selection but doesn't
'pass the result to the Documents.Add method.  

   Dim fDialog As FileDialog, result As Integer
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
   fDialog.AllowMultiSelect = False
   fDialog.Title = "Select a Template"
   fDialog.InitialFileName = "PathtoTemplates"
   fDialog.Filters.Clear
   fDialog.Filters.Add "Word Template Files", "*.dotx"
   
   'Show the dialog. -1 means success
   If fDialog.Show = -1 Then
       Debug.Print fDialog.SelectedItems(1)
   End If

   Documents.Add Template:="fDialog.SelectedItems(1)"
0
Comment
Question by:JDBTech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Norie earned 2000 total points
ID: 41847788
Try removing the quotes.
Documents.Add Template:=fDialog.SelectedItems(1)

Open in new window

0
 

Author Comment

by:JDBTech
ID: 41847793
That fixed it!!  Thanks so much Norie!  I can sleep now.  Have a good night!
0
 

Author Closing Comment

by:JDBTech
ID: 41847795
Thanks for the quick response.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question