PDA

View Full Version : permanently saving a filename in a macro



JamesT
12-14-2016, 08:10 PM
Hello,

My macro asks the user, via a dialog box, to select a folder to find where standard figures are saved, this happens every time the macro is opened - is there a way to run this macro once and 'save' the folder location in the macro to be used the next time the macro is executed (to bypass the user input section)? and if so, I need to expand this macro to identify if there is an error with opening this folder and re-run the 'select folder' macro if there is an error.

My code is here:

-----

Private Sub CommandButton1_Click()


Dim acontrol As Control
Dim intResult As Integer
Dim strPath As String
Dim a As String
Dim i As Long
Dim x As Variant
Dim m As String
Dim d As Integer


'Selection.EndKey Unit:=wdStory
'Selection.InsertBreak Type:=wdSectionBreakNextPage


Application.FileDialog(msoFileDialogFolderPicker).ButtonName = "Select Folder"
Application.FileDialog(msoFileDialogFolderPicker).Title = "Find the folder where the standard drawings are kept"
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
a = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
End If




x = 1
For i = 1 To 26
For Each acontrol In Me.Controls
If acontrol.Name = "CheckBox" & i Then
If acontrol.Value = True Then
m = a & "\Checkbox" & x & ".jpg"
MsgBox m
Selection.EndKey Unit:=wdStory
Selection.InlineShapes.AddPicture FileName:=m
x = x + 1
Else
x = x + 1
End If
End If
Next acontrol
Next i

AddStandardDrawings.Hide


End Sub

gmayor
12-14-2016, 10:26 PM
You can store the path in a variable in the template (which will then need to be saved - function included below) and check that the path exists before running the code. If the path or the file don't exist the variable is deleted and the prompt appears next time.



Option Explicit

Private Sub CommandButton1_Click()

Dim acontrol As Control
Dim intResult As Integer
Dim strPath As String
Dim i As Long
Dim x As Variant
Dim strName As String
Dim d As Integer
Dim oVar As Variable
Dim bVar As Boolean
Dim fso As Object
Dim oRng As Range

Set fso = CreateObject("Scripting.FileSystemObject")

bVar = False
For Each oVar In ThisDocument.Variables
If oVar.Name = "varPath" Then
strPath = oVar.Value
bVar = True
Exit For
End If
Next oVar

If Not bVar Or Not (fso.FolderExists(strPath)) Then
Application.FileDialog(msoFileDialogFolderPicker).ButtonName = "Select Folder"
Application.FileDialog(msoFileDialogFolderPicker).Title = "Find the folder where the standard drawings are kept"
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancelled the dialog
If intResult <> 0 Then
strPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
ThisDocument.Variables("varPath").Value = strPath
UpdateTemplate
End If
End If

x = 1
For i = 1 To 26
For Each acontrol In Me.Controls
If acontrol.Name = "CheckBox" & i Then
If acontrol.Value = True Then
strName = strPath & "\Checkbox" & x & ".jpg"
MsgBox m
If fso.FileExists(strName) Then
Set oRng = ActiveDocument.Range
oRng.Collapse 0
ActiveDocument.InlineShapes.AddPicture FileName:=strName, Range:=oRng
x = x + 1
Else
MsgBox "File not found. Run the macro again"
ThisDocument.Variables("varPath").Delete
UpdateTemplate
GoTo lbl_Exit
End If
Else
x = x + 1
End If
End If
Next acontrol
Next i
AddStandardDrawings.Hide
lbl_Exit:
Set oRng = Nothing
Set fso = Nothing
Exit Sub
End Sub

Sub UpdateTemplate()
'Graham Mayor - http://www.gmayor.com
Dim bBackup As Boolean
bBackup = Options.CreateBackup
Options.CreateBackup = False
ThisDocument.Save
Options.CreateBackup = bBackup
lbl_Exit:
Exit Sub
End Sub