Solved

Microsoft Word Macros and VBA Scripts

Posted on 2016-10-17
3
71 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 33

Accepted Solution

by:
Norie earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

756 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