WordApp.Run

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have inherited some code that I am trying to get to work. There's a lot of code so will give what I think is relevant.
I get the error because the pdf file isn't there!!

Rich (BB code):
Sub SavePDF()

Ben_Table_Filename = sPDFDir & sCompanyName & " - " & sQuoteName & " - " & dYearNow & "." & dMonthNow & "." & dDayNow & " - " & sBDM & ".pdf"
T_Cs_Filename = sPDFDir & sCompanyName & " -  Bespoke Terms - " & dYearNow & "." & dMonthNow & "." & dDayNow & " - " & sBDM & ".pdf"
Scheme_Filename = sPDFDir & sCompanyName & " - " & sQuoteName & " - " & dYearNow & "." & dMonthNow & "." & dDayNow & " - " & sBDM & " - " & sVerMarker & ".pdf"

Call DetailsToWord(Ben_Table_Filename, T_Cs_Filename, Scheme_Filename)

ActiveWorkbook.FollowHyperlink Scheme_Filename  ERRORS HERE  'Cannot open the specified file.

End Sub

Rich (BB code):
Sub DetailsToWord(Ben_Table_Filename, T_Cs_Filename, Scheme_Filename)

Dim Policy_Info As Variant, Benefits_Included As Variant, Colour_Info As Variant

Policy_Info = Array(Ben_Table_Filename, T_Cs_Filename, Scheme_Filename, Plan_Name, Reference, Children, Children_Shared, XS_Covered, Dental_Percent, Dental_Acc_Percent, Optical_Percent, _
                    Physio_Percent, SC_Percent, MRI_Percent, Chiro_Percent, HandW_Percent, HS_Percent, Flu_Percent, HearAid_Percent, HomeHelp_Percent, Optical_TwoYear, HS_TwoYear, _
                    HW_HS_Combined, Chir_Physio_Combined, Flu_or_Vacc, Direct_Debit, Save_Intermediate_Files, Multiple_CP_Levels, Onsite_HS)

Benefits_Included = Array(Dental, Dental_Accident, Optical, Hospital, P_Hospital, Maternity, HearAid, HomeHelp, Physio, Spec_Cons, MRI, Chiropody, HandW, HS, Personal_Acc, _
                        Prescription, Flu_Jabs, Home_Assist, Hide_MyWellness, Prestige_Choice)

Colour_Info = Array(ColourName, PrimaryColourRed, PrimaryColourGreen, PrimaryColourBlue, SecondaryColourRed, SecondaryColourGreen, SecondaryColourBlue, PlanType)

'SEND TO WORD
'Open Word T&Cs template, if not already open. Check if Word is open, then if the document is open, and give a message if going to overwrite an open document.
Template_Filepath = Range("T_C_Template_Filepath").Value
Template_Name = Right(Template_Filepath, Len(Template_Filepath) - InStrRev(Template_Filepath, "\"))

AppOpenedByMac = False
DocOpenedByMac = False
On Error Resume Next

ProgMsg.ProgText = "Initialising Microsoft Word..." & vbCr & vbCr & "Time taken: " & Second(Now - TimeNow) & " seconds"
ProgMsg.ProgBar.Width = 160

Set WordApp = GetObject(, "Word.Application")

If WordApp Is Nothing Then
    Set WordApp = CreateObject("Word.Application")
    AppOpenedByMac = True
    Set WordDoc = WordApp.Documents.Open(Template_Filepath)
    DocOpenedByMac = True
    GoTo OpenedByMacro
Else
    On Error GoTo NotOpen
    Set WordDoc = WordApp.Documents(Template_Name)
    GoTo OpenAlready
End If

NotOpen:
    Set WordDoc = WordApp.Documents.Open(Template_Filepath)
    DocOpenedByMac = True
    GoTo OpenedByMacro

OpenAlready:
    On Error GoTo 0
    response = MsgBox("Word template for Terms and Conditions is already open. It may not be possible to undo changes." & vbCr & vbCr & "Continue?", vbOKCancel, "Word template already open")
    If response = vbCancel Then
        ProgMsg.Hide
        Exit Sub
    End If

OpenedByMacro:

ProgMsg.ProgText = "Microsoft Word is working..." & vbCr & vbCr & "Time taken: " & Second(Now - TimeNow) & " seconds"
ProgMsg.ProgBar.Width = 200

'Call Word macro to set T&C's
WordApp.Run "Set_T_Cs", Policy_Info, Benefits_Included, TUs_Info, Colour_Info

Application.Wait (Now + TimeValue("00:00:02"))

If DocOpenedByMac = True Then
    WordDoc.Close (wdDoNotSaveChanges)
End If

If AppOpenedByMac = True Then WordApp.Quit SaveChanges:=wdDoNotSaveChanges

Err.Number = 0

End Sub
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

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