Solved

Word VBA File Names

Posted on 2013-01-07
5
429 Views
Last Modified: 2013-01-07
I had a document that was created in Word 2003 (vba 6.5)/Windows XP.  One of the operations of this document was to open another document that the user selects when clicking on a command button.  This was done through the following code.

Documents.Open fileName:=matrixdoc, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, Visible:=False

Open in new window


We have now moved to a new environment; Word 2010 (VBA 7)/Windows 7.  

Since this we have had problems with this code.  If the files have been stored on a local drive, when the user selects a file “ “ is added to the file name e.g “”c:\file.docx””.  If the file is stored on a network drive, this isn’t the case, so the selected file will be “c:\file.docx”.  

Does anyone know why this is happening and whether there is a quick fix rather than having to do an IF statement to see if “” has been added to the file name and deleting if it has?  

It is causing a problem because these files can be stored either on a local or network drive so I need a system that works in both scenarios.
0
Comment
Question by:jdc1944
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38750547
If you are saying that the value of the variable matrixdoc is incorrect, we will need to see the code that produces it.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38750561
How is matrixdoc  defined? ... essentially ensure it is defined as a string and then this shouldn't happen ... not that I understand how it is happening but I have no such issues when using a string type variable.

Chris
0
 
LVL 2

Author Comment

by:jdc1944
ID: 38750728
Sorry, the code that produces the variable is as follows:

Dim dlg As Dialog
Dim matrixdoc As String
Set dlg = Dialogs(wdDialogFileOpen)
   If dlg.Display = -1 Then
        matrixdoc = dlg.Name
   End If

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38750866
I'm not in a position to test it at the moment, but you could always try using Replace:

matrixdoc = Replace(dlg.Name, """","")
0
 
LVL 2

Author Closing Comment

by:jdc1944
ID: 38751052
This seems to have sorted it, thanks.  

I'm fairly new to VB and wasn't aware of replace.  I'm sure it will come in handy in the future.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

830 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