VBA Word - Save As dialog with initial filename - vba

I have a vba macro that makes some changes to the current document and determines a filename that should be used for it - if the document isn't saved as that filename yet the user should be prompted to do so (but should be able to alter the default setting).
I found two possibilities that both are not perfect (I'd need a mix of those two).
First approach:
Application.Dialogs(wdDialogFileSaveAs).Show
Opens the Save As dialog and lets you change the format and name of the file, but the default file name is the old filename or the title (up to the first special character like blank or -) of the document (in case it wasn't saved yet - changing the title of the document is of little help as the suggested filename will contain -). Is it possible to change the initial filename shown in the Save As dialog?
Second approach:
Application.FileDialog(msoFileDialogSaveAs).InitialFileName = filename
Dim choice As Integer
choice = Application.FileDialog(msoFileDialogSaveAs).Show
If choice <> 0 Then
filename = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
Call ActiveDocument.SaveAs(filename:=filename, FileFormat:=wdFormatDocumentDefault)
End If
The FileDialog will choose a filename only, so we have to save it explicitely.
This approach will show the filename I want, but if the user changes the suffix to e.g .pdf the file will still be saved in the .docx format (using the suffix .pdf). I didn't plan to have huge distinction of cases here for the rare case the user needs a different format than .docx. Is there an easy way to save the file in the correct format using this second approach?

Have you tried replacing the Call ActiveDocument.SaveAs line with
Application.FileDialog(msoFileDialogSaveAs).Execute

Window's setting "View tab - File name extension" maybe affecting this.
Turn it on to see if it will fix it.

Related

Preserve settings in the file

I am creating my excel add-in that saves the current file as csv into user-specified folder. I would like my program to ask for the folder path the first time the program is launched and to remember that folder in the future. I am wondering is there any way to preserve the data within the program? I figured that I could write the path into .txt-file but that feels a little hack-like solution and would clutter the addin folder.
I use the GetSetting and SaveSetting functions in my VB 6 apps. Rather than cover them in detail, take a look at this excellent web page that illustrates how to use in with Excel
Excel Tips From John Walkenbach
Create a Worksheet, and store the values in cells. Then in the VBA Editor find the Worksheet in the Project Explorer (Ctrl + R) and set "Visible" to "2 - xlSheetVeryHidden" in the Properties Pane (F4) so that it is not readily visible to users.
You can then set/retrieve the data in with code in the format SheetName.Cells(row,column).Value, e.g.
MyPath = Sheet1.Cells(1,2).Value 'Get data from cell B1
Sheet1.Cells(2,2).Value = NewPath 'Set data in cell B2
There are multiple ways to approach this. Besides the hidden sheet approach, already described, you can
Write the information to a CustomXMLPart. This is a xml file stored in the workbook's ZIP file where information can be stored.
For something as short and simple as a file path, you could also use a CustomDocumentProperty

Creating a file with name given in code in LiveCode

I'm trying to build a program that gets users name at first. This user names are kept in a text file. After user logins, according to the user's name, I want user to be lead his/her specific informations. I figured out that I can only do it with a file that is created when he sign up for an account which I direct him with my sign up button in Livecode. While he/she create his account I want to create a specific file for his/her. Can you help me with it please?
PS: I don't want to do it with a database right now. I just want to learn how to create a file without a specific name like
put specialFolderPath("documents")&"/userLoginCridentials.txt" into tFile put URL("file:"&tFile) into myFile
Instead of this "userCridentials.txt" I want something user can create with his own name :)
Having a little problem to understand your question. Are you targeting mobile or desktop? Are you having problem saving or reading the data?
If saving is you problem
On desktop you can use:
ask file "Save file as:"
then you get the filename back in it so you can use:
if it is not empty then
# We have a complete file path in 'it'
put it into tFile
put tData into url ("file:" & tFile)
end if
If you targeting mobile and would like to save into the specialFolderPath("Documents")you can get the filename from a field and then save to that file. E.g. if you have a field named 'fileName' you can use something like:
put tData into url("file:" & specialFolderPath("Documents") & "/" & field "fileName"
Of course you should do some error checking to ensure that a user don't overwrite existing files without at least asking for permission, etc.
You can of course use a variable instead of a field...
If reading data is your problem
On desktop you can use:
answer file "Open File:"
Same as above but you now read data instead:
if it is not empty then
# We have a complete file path in 'it'
put it into tFile
put url ("file:" & tFile) into tData
end if
on mobile you probably would like to present a list with the user-created files. In LiveCode you can list all files in the defaultFolder with the files. But you must set the defaultFolder to the folder you want to list.
set the defaultFolder to specialFolderPath("Documents")
put the files into tFiles
Now tFiles contains every file in that folder and you can filter it, display it in a list etc. E.g:
filter tFiles with "*.txt"
put tFiles into
If your problem is how to remember the "current" file name
Whenever you restart your app every variable is reset. So everything you want to remember between runs needs to be saved before your app quits. And for that you need a predefined filename. SO then your procedure will be:
Read in the predefined file.
Grab the file name from within that file
Read the file
If your problem is something else
Sorry, then I misunderstood your question...

Save a textfile as current time and date

So I am using VB.NET and I got this problem where I wanna save something (this works) but the one thing I can't get to work is that is automatically saves (title) as current time and date (like this as title: 06/04/2015 10h30min25s)
I am using the SaveFileDialog to save my text.
Rather than using the file path as returned by the dialog, you can simply build the path yourself using the current date and time. For instance:
Dim filePath As String = Date.Now.ToString("MM-dd-yyyy HH\hmm\minss\s")
Change your slashes to dashes. You can't have slashes in a file name.

VBA Save a String across submodule calls

I am making an "intelligent save" button for word and excel files.
The first time it is run from a file, it will prompt user to navigate to the correct folder. The important part is the selected path will be saved for that file and automatically referenced the next time someone uses the macro. Then the user can specify pdf vs. docx/xlsx file type, then save the file.
Is the bolded part possible, and what kind of technique/functions can I use to do this?
Posted as an answer, with a bit of example code and more detail:
For such a small amount of data, why not use VBA's SaveSetting/GetSetting commands to put the needed info in the registry?
Sub SaveGetSettingExample()
' Saves string values to:
' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\AppName
SaveSetting "AppName", "Section", "Key", "Value"
' Displays the just-stored value: Value
MsgBox GetSetting("AppName", "Section", "Key", "Default Value")
End Sub
I'm assuming you're embedding the code in the workbook (and not in the Personal code folder). If so, I've had success writing the file path to a cell in a hidden column (usually out to far right of view) in Excel. Your code can reference this default location as needed when loading the next time.
I'm not sure about Word, our Word documents stay pretty simple.

Inserting a picture at a bookmark (OR how to tell if any list box items have been selected)

I'm new to VBA and still struggling a lot.
I have a list object on a useform that is populated with the file-names of the contents of the relative ".\logos\" directory. I want to insert the picture at a bookmark named bmLogo, but the code I've written (see below) doesn't do the trick.
If ListLogo <> Null Then
ActiveDocument.Bookmarks("bmLogo").Range _
.InlineShapes.AddPicture FileName:=ThisDocument.Path & "\logos\" & ListLogo
End If
Any tips? Also, if I could set a height and have the image scale to it without changing the aspect ratio that would be very useful!
Thanks,
Louis
EDIT 1: Right, so, bmLogo is the correct name of the bookmark, so that's not the problem.
I just used a msgbox to display '"path: " & ThisDocument.Path & "\logos\" & ListLogo' and it looks like the correct path. I'm using ThisDocument.Path as I want it to be relative so the document is more portable. I think I'll try with an absolute path for the time being and seeing if that work, if nothing else it should hint at where the bug is not.
EDIT 2: It works with an absolute path outside the IF statement and when I get a msgbox to print both the absolute and relative path they are identical. After commenting out the IF statement the relative method works find. I'm glad that it's working now but can anyone tell what the issue is with the IF?
EDIT 3: It turns out that my method for checking if something has been selected in the list box doesn't work at all. Instead I shall be iterating through each item in the list and checking if that one is selected. It's a pretty crude method but it'll do until I can find a better one.
For the record, this works for me in 2010, so I'm guessing there's either a problem with your filename (ListLogo), your bookmark (bmLogo), or else the filepath (ThisDocument.Path).
1:
Does the filename match the format you expect? Is it just the filename, or a full path? Does it include the proper extension?
2:
Does the bookmark exist in your document?
3:
Does ThisDocument refer to what you think it does? The simplification below works for me.
ActiveDocument.Bookmarks("TEST").Range.InlineShapes.AddPicture FileName:="P:\test.png"
I saved the .docx file in my P:\ path, so the following should have worked:
ActiveDocument.Bookmarks("TEST").Range.InlineShapes.AddPicture FileName:=ThisDocument.Path & "\test.png"
However, the path returned was my AppData directory. When I watched the ThisDocument object, I saw that it actually pointed to the Normal.dotm template, where the code was created when I recorded a macro to test this out.
Can you please verify that each of these three items are correct and what you expect?
ListLogo
bmLogo
ThisDocument