Open file, if file is not there open in a different directory

Status
Not open for further replies.

antonismouf

Board Regular
Joined
Aug 18, 2015
Messages
64
So, I am using a code to open a word file and paste there some data from excel and save it, but I have a problem.

The file is in dropbox and using it from different pc it exists is a different directory:
1)C:\Users\user\Dropbox
2)C:\Users\pc\Dropbox

I cannot change the folder name because it conflicts with so many different things and creates issues with other programs.

So I need a code to try and open the file in one location and if it is not there open it from the second one (and also save in the other one). Below is my code:

Code:
Sub WordUp()


Dim WdObj As Object, fname As String


fname = Sheets(2).[b1].Value


If fname <> "" Then 'make sure fname is not blank


    Set WdObj = CreateObject("Word.Application")
    
    WdObj.Visible = True
    
    finalrow = Sheets("Prosfora").Range("A9999").End(xlUp).Row
    Sheets("Prosfora").Range("A2:A" & finalrow).Copy
    
    WdObj.Documents.Open Filename:="C:\Users\User\Dropbox\ÐÑÏÃÑÁÌÌÁÔÁ\prosfora template1.docx"
    
    
    With WdObj
    
        .ChangeFileOpenDirectory "C:\Users\User\Dropbox\ÐÑÏÃÑÁÌÌÁÔÁ" 'save Dir
    
        .ActiveDocument.SaveAs Filename:="Ðñïóöïñá " & fname & ".doc"
        
    End With
    
    WdObj.Selection.PasteSpecial Link:=False ', DataType:=wdPasteText , Placement:=wdInLine, DisplayAsIcon:=False
    
    Application.CutCopyMode = False
    
    
    WdObj.ActiveDocument.Save
    Else:
    
    MsgBox ("ÐñïóèÝóôå ¼íïìá ÐåëÜôç")


End If

Set WdObj = Nothing


End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Status
Not open for further replies.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top