Save the created Word doc with the content of a cell from the Excel table

Status
Not open for further replies.

fabcat1

New Member
Joined
May 20, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi everyone
I'm a beginner

I've created a little macro to save content of cells from an Excel sheet into bookmarks of a Word document created from the Macro
I'd like to name the created Word document with the content of a cell from the Excel file (cellb2.docx)

Can someone please help me ?

thanks a lot
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Is the VBA code written in Excel or in Word?

Either way you will have the word Document object

What you want to do is fill a string variable with the contents of B2 (after checking it's not empty), then do a Document.saveas with the string variable as parameter.

something like(assuming the VBA is in Excel):
VBA Code:
    Dim dDoc As Document    '<<<< The document object may be differtently named in your code, _
                                    so use your name in the code below!
    Dim sFName As String
    
    sFName = Range("B2").Value
    If Len(sFName) = 0 Then
        MsgBox prompt:="Cell B2 is empty, File not saved", _
                buttons:=vbExclamation + vbOKOnly, _
                Title:="Error saving file"
        Exit Sub
    End If
    
    dDoc.SaveAs2 sFName
 
Upvote 0
Is the VBA code written in Excel or in Word?

Either way you will have the word Document object

What you want to do is fill a string variable with the contents of B2 (after checking it's not empty), then do a Document.saveas with the string variable as parameter.

something like(assuming the VBA is in Excel):
VBA Code:
    Dim dDoc As Document    '<<<< The document object may be differtently named in your code, _
                                    so use your name in the code below!
    Dim sFName As String
   
    sFName = Range("B2").Value
    If Len(sFName) = 0 Then
        MsgBox prompt:="Cell B2 is empty, File not saved", _
                buttons:=vbExclamation + vbOKOnly, _
                Title:="Error saving file"
        Exit Sub
    End If
   
    dDoc.SaveAs2 sFName
Thanks Sijpie

Problem fixed
 
Upvote 0
Good to hear you got the solution.

If you would like to post the solution then it is perfectly fine to mark your post as the solution to help future readers. Or if any post in this thread solved your question then please mark it as the solution. Otherwise, please do not mark a post that doesn't contain a solution.
 
Upvote 0
In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,213,532
Messages
6,114,176
Members
448,554
Latest member
Gleisner2

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