Importing doc variables from a Word document into another - vba

I have a Word document with a userform that allows users to complete a series of questions (filing out textboxes) that populates a Word document. The userform utilizes doc variables to mark where in the document I want answers to be populated. The code looks like:
Dim TextBox13 As Range
ActiveDocument.Variables("dvmdy").Value = Me.TextBox13.Value
Me.TextBox13.Value = Me.TextBox13.Text
With ActiveDocument
.Fields.Update
End With
I also have a command button that works to open another document but am trying to make it so it reads the docvariables in the second document and writes them to the first. The code below is for the file selector setup I put together:
Private Sub CommandButton1_Click()
Dim fDialog As FileDialog
Dim strFile1 As String
Dim strFile2 As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Filters.Clear
.Filters.Add "Word Documents", "*.docm*"
.InitialFileName = "C:\"
.Title = "Select the First Document"
If .Show = -1 Then
strFile1 = .SelectedItems(1)
strFile1 = .SelectedItems(1)
MsgBox (strFile1)
Label8v = strFile1
Else
Exit Sub
End If
End With
With fDialog
.Filters.Clear
.Filters.Add "Word Documents", "*.docm*"
.InitialFileName = "C:\"
.Title = "Select the Second Document"
If .Show = -1 Then
strFile2 = .SelectedItems(1)
strFile2 = .SelectedItems(1)
MsgBox (strFile2)
Label9v = strFile2
Else
Exit Sub
End If
End With
Set Doc1 = Documents.Open(strFile1)
Set Doc2 = Documents.Open(strFile2)
End Sub
Basically I want to make it so it allows users to open previously filled out Word documents with the same doc variables so that the previously filled out answers could be used to populate the new document/userform.

Related

After using Application.FileDialog(msoFileDialogFilePicker), how can I save that selected file to a predetermined path?

I have code in MS Access to select a PDF file. What I cannot figure out is how to then save the selected PDF file to a predetermined path, in this case "mypath". I also need to rename the selected file to the "MyFileName3" string. How can I go about this?
Sub Browse()
Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim SourceFileName As String
Dim mypath As String
Dim MyFileName3 As String
mypath = "\\Improvement Processes\ClaimsHolding\Local\"
MyFileName3 = & "-03-DealSheet.pdf"
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Please select your PDF file"
.Filters.Clear
.Filters.Add "PDF File", "*.pdf"
If .Show = True Then
For Each varFile In .SelectedItems
SourceFileName = varFile
Next
Else
MsgBox "You clicked Cancel."
End If
End With
MsgBox SourceFileName
End Sub
You can do it easily with FileSystemObject
Here is an example of how to implement it in your code:
Dim FSO As Object, SourceFile As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFile = FSO.GetFile(SourceFileName)
SourceFile.Copy Destination:=mypath
FSO.GetFile(mypath & SourceFile.Name).Name = MyFileName3
Make sure that MyFileName3 includes the file extension .pdf or you will create an untyped file when you rename it.

Showing path of selected files in ListBox

I would like to see names or paths of selected files in VBA using windows explorer.
Here's what I have done so far, it opens the explorer and allows to select files but lacks displaying the names/path of the files.
Private Sub CommandButton1_Click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", "*.*"
If .Show = True Then
///
///here comes the part with showing the names in ListBox
///
End If
End With
End Sub
Here is the completed code:
Public Sub CommandButton1_Click()
Dim i As Integer
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", "*.*"
If .Show = True Then
For i = 1 To fd.SelectedItems.Count
ListBox1.AddItem fd.SelectedItems(i)
Next
End If
End With
End Sub

PowerPoint VBA Save As Dialog file filter

How do I set 1 or more Save As Dialog file filter in PowerPoint VBA? Here is my code:
Private Sub save_file_Click()
Dim fd As FileDialog
Dim clicked As Boolean
Set fd = Application.FileDialog(msoFileDialogSaveAs)
With fd
.InitialFileName = ActivePresentation.path & "\*.note"
.AllowMultiSelect = False
.ButtonName = "Save"
.Title = "Save File"
'-- Normal Filters are not allowed on FileDialogSaveAs --
'.Filters.Clear
'.Filters.Add "Note Files", "*.note"
'.Filters.Add "Text Files", "*.txt"
'.Filters.Add "All Files", "*.*"
End With
clicked = fd.Show
If clicked Then
Open fd.SelectedItems(1) For Input As #1
Print #1, text_box.Text
Close intFileNum
End If
End Sub
Any Ideas?
In case it makes a difference: I'm not trying to make an ppt export to HTML, but save a file that's custom (.note) in a User Form (window).

Open a workbook using FileDialog and manipulate it in Excel VBA

I am learning how to use Excel macros and I found this code:
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select the file to kill his non colored cells"
.Filters.Add "Excel", "*.xls"
.Filters.Add "All", "*.*"
If .Show = True Then
txtFileName = .SelectedItems(1)
End If
End With
This code opens the FileDialog. How can I open the selected Excel file without over-writing the previously opened?
Thankyou Frank.i got the idea.
Here is the working code.
Option Explicit
Private Sub CommandButton1_Click()
Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "Excel 2003", "*.xls?"
If .Show = True Then
fileName = Dir(.SelectedItems(1))
End If
End With
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open (fileName)
For Each sheet In Workbooks(fileName).Worksheets
total = Workbooks("import-sheets.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy _
after:=Workbooks("import-sheets.xlsm").Worksheets(total)
Next sheet
Workbooks(fileName).Close
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Unless I misunderstand your question, you can just open a file read only.
Here is a simply example, without any checks.
To get the file path from the user use this function:
Private Function get_user_specified_filepath() As String
'or use the other code example here.
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
fd.Title = "Please select the file."
get_user_specified_filepath = fd.SelectedItems(1)
End Function
Then just open the file read only and assign it to a variable:
dim wb as workbook
set wb = Workbooks.Open(get_user_specified_filepath(), ReadOnly:=True)

Excel User forms in vba

I'm developing VBA code using Forms. I have a certain button option to let the user select the workbook using FileDialog. The workbook file may contain 4 or 5 sheets itself. I have combobox with empty.
I need to have the sheetnames of the user selected workbook listed in the combobox automaticaly without opening the workbook.
I tried with the following code, but got the names of already opened workbooks.
Private Sub CommandButton2_Click()
Set myfile = Application.FileDialog(msoFileDialogOpen)
With myfile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
Fileselected = .SelectedItems(1)
End With
With Fileselected
For i = 1 To Sheets.Count
ComboBox2.AddItem Sheets(i).Name
Next i
End With
At the moment Sheets refers to the current workbook as you do nothing with Fileselected.
You must open the selected file, otherwise how can you examine its contents?
Set myfile = Application.FileDialog(msoFileDialogOpen)
With myfile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
Fileselected = .SelectedItems(1)
End With
Dim doc As Workbook
Set doc = Application.Workbooks.Open(Fileselected)
With doc
For i = 1 To .Sheets.Count
ComboBox2.AddItem .Sheets(i).Name
Next i
.Close
End With