Solved

Word VBA File Names

Posted on 2013-01-07
5
396 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

771 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

10 Experts available now in Live!

Get 1:1 Help Now