Solved

Microsoft Word Macros and VBA Scripts

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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