Word 2010 VBA - Global Variables in VBA and using them in VBA Code

Hiya

I need some help.  I have the following code that takes a word document, converts it to pdf and saves it to the local temp folder and attaches it to an email.  Up until now the filename for the file has been hard coded.

I have now created a userform that allows the user to enter a filename for the document and I have created a global variable for the filename to be stored.

What I want to be able to do is pick up this filename from the global variable and add it to filepath in the code.  At the moment it just saves the document as Mytitle which is what the variable is called.

Here is my code, can I please have some help as I am a little lost, this is the first time I have created something so complicated!!

Public Mytitle As String

Private Sub CommandButton1_Click()

ConvertandSave

Createemailandattach

End Sub

Sub ConvertandSave()

'converts to pdf and save locally using filename from userform

ActiveDocument.ExportAsFixedFormat OutputFileName:="C:\temp\Mytitle.pdf", _

ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _

wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _

Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _

CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _

BitmapMissingFonts:=True, UseISO19005_1:=False

End Sub

Sub Createemailandattach()

'creates email and attaches to the file

Dim oOutlookApp As Outlook.Application

Dim oItem As Outlook.Mailitem

Dim MyFileName As String

On Error GoTo SendCancelled

MyFileName = "c:\temp\Mytitle.pdf"

Set oOutlookApp = GetObject(, "Outlook.Application")

If Err <> 0 Then

    Set oOutlookApp = CreateObject("Outlook.Application")

End If

Set oItem = oOutlookApp.CreateItem(ofMailitem)

Set objRecip = oItem.Recipients.Add("   email address goes in here")

obj.Type = olTo

oItem.Display

'oItem.Subject = "My Profile - "& UCase(LastName) & " " & FirstName

oItem.Attachments.Add (MyFileName)

Exit Sub

SendCancelled

    MsgBox "This macro was cancelled"

End Sub

Any help here would be very much appreciated

April 22nd, 2015 12:42pm

Assuming that you have other code to set the value of Mytitle: change all instances of "c:\temp\Mytitle.pdf" to

"c:\temp\" & Mytitle & ".pdf"

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 2:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics