Solved

Microsoft Word Macros and VBA Scripts

Posted on 2016-10-17
3
52 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

778 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