I have code to create a new Word document then copy and paste information from Excel.
It then needs to check if a file exists with a specific filename from variables from the Excel sheet and if not then save in the user's OneDrive with that filename.
I get the Word document and the information is pasted but it hangs on .SaveAs2 giving an error message
Run-time error '-2147023170 (800706be)':
Automation Error
The remote procedure call failed.
Sub SubmitFormB()
'
' Submit Macro
Application.ScreenUpdating = True
Range("A1:D37").Select
Selection.Copy
Dim objWord
Dim objDoc
Dim FullPath As String
Dim olApp As Outlook.Application
Dim OutMail As Outlook.Mailitem
'sets word doc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
'gets PID
Set objNetwork = CreateObject("WScript.network")
getUserPID = objNetwork.UserName
'sets Email
Set olApp = New Outlook.Application
Set OutMail = olApp.createitem(olMailitem)
'set File Path
FullPath = Environ("UserProfile") + "\OneDrive\" + Sheets("Form B").Range("C10").Value + " Facilities.docx"
pathexists = Dir(FullPath)
If pathexists = "" Then
'copies details to word
objWord.Visible = True
objWord.Activate
objWord.Selection.PasteSpecial DataType:=wdKeepSourceFormatting
'saves word doc
with objDoc
.SaveAs2 (FullPath)
.Close
End with
Else
MsgBox "This file already exists at " + FullPath + " Please delete and try again"
Exit Sub
End If
End Sub
The debugger shows the full file path and as far as I can see it is correct. I previously had the full file path typed out and had the same issue.

Environ("UserProfile") + "\OneDrive\"exists as a path?