Solved

Microsoft Word Macros and VBA Scripts

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now