MS Excel VBA Macro to Open a MS Word File, Save the File and then Close the File

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Hello all and thanks in advance for any assistance you may provide.

I would like to update the following lines of the MS Excel Code Provided.

VBA Code:
'Code to save any changes made only to the file that was open with this code

VBA Code:
 'Code to only close the file open with this code (do not close MS Word)

I obtained this from another site or post....cannot recall where. I have found several different versions, but I cannot seem to go those to work, so I am working on completing the following.

VBA Code:
Sub Open_Word_Document()

'Opens a Word Document from Excel


 '_______________________________________________________________________________________________________
 'Turn off alerts, screen updates, and automatic calculation
    'Application.DisplayAlerts = False
    'Application.ScreenUpdating = False
    'Application.Calculation = xlManual

            
            
  '_______________________________________________________________________________________________________
  'Dimensioning
    

    'Dimensioning
     Dim objWord As Object
     Dim aFilePath As String
     Dim aFileName As String
     Dim aFileName_N_Path As String



    'Settting Object
     Set objWord = CreateObject("Word.Application")



    'Makes MS Word Visible
     objWord.Visible = True



    'Change the directory path and file name to the location of your document
        
        'Setting file name and path
         aFilePath = "J:\MPF\00. Test"
         aFileName = "Application - Tasks 2017-08-07.docx"
        
        'If Directory does not have a "\" at the end, adds one
         If Right(aFilePath, 1) <> "\" Then aFilePath = aFilePath & "\"
         
        'Combines file path and file name
         aFileName_N_Path = aFilePath & aFileName



    'Opens the word document
     objWord.Documents.Open aFileName_N_Path
    
    
    'Code to make changes
    
    
    'Code to save any changes made only to the file that was open with this code
     
    
    'Code to only close the file open with this code (do not close MS Word)
    

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hello all and thanks in advance for any assistance you may provide.

I would like to update the following lines of the MS Excel Code Provided.

VBA Code:
'Code to save any changes made only to the file that was open with this code

VBA Code:
 'Code to only close the file open with this code (do not close MS Word)

I obtained this from another site or post....cannot recall where. I have found several different versions, but I cannot seem to go those to work, so I am working on completing the following.

VBA Code:
Sub Open_Word_Document()

'Opens a Word Document from Excel


 '_______________________________________________________________________________________________________
 'Turn off alerts, screen updates, and automatic calculation
    'Application.DisplayAlerts = False
    'Application.ScreenUpdating = False
    'Application.Calculation = xlManual

           
           
  '_______________________________________________________________________________________________________
  'Dimensioning
   

    'Dimensioning
     Dim objWord As Object
     Dim aFilePath As String
     Dim aFileName As String
     Dim aFileName_N_Path As String



    'Settting Object
     Set objWord = CreateObject("Word.Application")



    'Makes MS Word Visible
     objWord.Visible = True



    'Change the directory path and file name to the location of your document
       
        'Setting file name and path
         aFilePath = "J:\MPF\00. Test"
         aFileName = "Application - Tasks 2017-08-07.docx"
       
        'If Directory does not have a "\" at the end, adds one
         If Right(aFilePath, 1) <> "\" Then aFilePath = aFilePath & "\"
        
        'Combines file path and file name
         aFileName_N_Path = aFilePath & aFileName



    'Opens the word document
     objWord.Documents.Open aFileName_N_Path
   
   
    'Code to make changes
   
   
    'Code to save any changes made only to the file that was open with this code
    
   
    'Code to only close the file open with this code (do not close MS Word)
   

End Sub

I think this post works to do what I need: How do I change field property in Word from Excel VBA?
 
Upvote 0
Solution

Forum statistics

Threads
1,223,847
Messages
6,174,992
Members
452,598
Latest member
jeffreyp

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