VBA code to create a word document, link it to a cell and save the document using the cell value

cmannp

New Member
Joined
Dec 20, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a Macros that will create a word document(for notes) and link it to a cell in the row and same the document using the value in the cell.
My code is able to do just that for one cell but I want it to be able to run through the different cells in the row and save each document created with the value of each cell. my code gives me an erro saying document exist if I try to run it on other cells. PLEASE help.


Sub Macro1()
'
' Macro1 Macro
'

'


Dim NumRows As String
SaveName = Range("A3", Range("A3")).End(xlDown).Value
Range("A3").Activate

For Each A In Range("A3", Range("A3")).End(xlDown)

ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
"\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\"
ActiveSheet.Hyperlinks(ActiveSheet.Hyperlinks.Count).CreateNewDocument _
Filename:="\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\ " & SaveName & ".docx", _
EditNow:=True, Overwrite:=False
Next A

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'


Dim NumRows As String

Range("A3").Activate

For Each A In Range("A3", Range("A3")).End(xlDown)
SaveName = A.Value
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
"\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\"
ActiveSheet.Hyperlinks(ActiveSheet.Hyperlinks.Count).CreateNewDocument _
Filename:="\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\ " & SaveName & ".docx", _
EditNow:=True, Overwrite:=False
Next A

End Sub
 
Upvote 0
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'


Dim NumRows As String

Range("A3").Activate

For Each A In Range("A3", Range("A3")).End(xlDown)
SaveName = A.Value
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
"\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\"
ActiveSheet.Hyperlinks(ActiveSheet.Hyperlinks.Count).CreateNewDocument _
Filename:="\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\ " & SaveName & ".docx", _
EditNow:=True, Overwrite:=False
Next A

End Sub
I still get error saying the file exist when I go to the next cell. the code is not picking up the value in the next cell.

1703169812593.png
 
Upvote 0
Sub CreateWordDocumentAndLink()
Dim wordApp As Object
Dim doc As Object
Dim ws As Worksheet
Dim path As String
Dim cellValue As String
Dim i As Integer

Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True

Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your actual sheet name

For i = 1 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
cellValue = ws.Cells(i, 1).Value
path = "\\rqhdata4\Quality Impr\PERSONAL FILES\Chinedu\Note\" & cellValue & ".docx"

If Dir(path) = "" Then
Set doc = wordApp.Documents.Add
doc.SaveAs2 path
ws.Hyperlinks.Add Anchor:=ws.Cells(i, 1), Address:=path
doc.Close
End If
Next i

wordApp.Quit
Set wordApp = Nothing
End Sub
```

This worked
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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