Solved

Word VBA File Names

Posted on 2013-01-07
5
419 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

773 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